localize method

String localize(
  1. String key,
  2. {num count,
  3. LocalisonGender gender,
  4. List args,
  5. String fallback,
  6. bool quantify}

Returns a translated message matching the specified key.

If gender is provided, then the appropriate message is picked by looking for a key that evaluates to key + separator + gender String value.

If count is provided, then it'll be substituted in the final localization message.

If args is provided, then the localized message is transformed by substituting the placeholder parameters with args in a fashion similar to C's sprintf(). Note that the first argument (%0$s) evaluates to the localized message with the placeholders, so your localizations is expected to start counting at (%1), which points to for the 1st argument.

If the key has not been defined in the JSON file, and fallback is defined, then it will be returned instead. Otherwise, the key itself is returned.


String localize(
  String key, {
  num count,
  LocalisonGender gender,
  List<dynamic> args,
  String fallback,
  bool quantify,
}) {
  assert(null != key, 'Localization key must not be null.');
  assert(key.isNotEmpty, 'Localization key must not be empty.');

  // The effective key to look for.
  final resolvedKey = _resolveKey(key, count, gender, quantify);

  // Are we looking for a pluralized message?
  final dynamic localization =
      null == count ? _messages[resolvedKey] : _pluralize(resolvedKey, count);

  // The final message is guaranteed to never be null.
  final message =
      null == localization ? (fallback ?? key) : localization.toString();

  // If arguments were provided, apply them in the message.
  // Note about the 3rd-party sprintf() implementation for Dart:
  // For some reason the author decided to have %0$s point to the first argument, which in
  // languages inherited from C should point to the string itself. Since this library can exist
  // in other apps, it's decided to follow the C notation here.
  // So we'll plug in the message string as a first argument and the user-supplied ones follow.
  return (true == args?.isNotEmpty
          ? sprintf(message, <dynamic>[message, ...args])
          : message)