Translations<TKEY, TRANbyLOCALE extends Map<StringLocale, StringTranslated>, TRANbyTKEY extends Map<TKEY, StringTranslated>, ADDEDMAP> class abstract

A Translations object is where you provide the translated strings. Given a "translation-key", it returns a map of translations: { locale : translated strings }. The translation-key may be the string itself that you want to translate, or an identifier.

Glossary:

  • translatable string: The string you want to translate.
  • translation-key: The key that represents the translatable string (may be the string itself).
  • locale: A valid BCP47 language tag, usually language-script-country, like 'en-US', 'pt-BR', or 'zh-Hans-CN'.
  • translated string: The translated strings for a given translation-key.
  • identifier: An immutable variable that you may use as a translation-key, instead of the string itself.

The options are:


Translations.byText example:

var t = Translations.byText('en-US') +
      {
        'en-US': 'i18n Demo',
        'pt-BR': 'Demonstração i18n',
      };

Translations.byLocale example:

var t = Translations.byLocale('en-US') +
  {
     'en-US': {
       'Hi.': 'Hi.',
       'Goodbye.': 'Goodbye.',
     },
     'es-ES': {
       'Hi.': 'Hola.',
       'Goodbye.': 'Adiós.',
     }
  };

Translations.byId example:

var t = Translations.byId<MyColors>('en-US', {
  MyColors.red: {
      'en-US': 'red',
      'pt-BR': 'vermelho',
  },
  MyColors.green: {
      'en-US': 'green',
      'pt-BR': 'Verde',
  });

Translations.byFile example:

var t = Translations.byFile('en-US', dir: 'assets/translations');

Translations.byHttp example:

var t = Translations.byHttp('en-US', url: 'https://example.com/translations.json');

ConstTranslations.new example:

const t = ConstTranslations(
   'en-US',
   {
     'i18n Demo': {
       'en-US': 'i18n Demo',
       'pt-BR': 'Demonstração i18n',
     }
   },
);

IMPORTANT: You may create your own translation classes, as long as they implement this interface.

This class is visible from both i18_exception and i18_exception_core packages.

Available extensions

Constructors

Translations.gen({required Map<TKEY, TRANbyLOCALE> translationByLocale_ByTranslationKey, required StringLocale defaultLocaleStr})
Generative constructor.
const

Properties

defaultLanguageStr String
To extract the language code from a locale identifier, we typically parse the identifier and take the first part before any underscore. The language code is always at the beginning of the locale identifier and is separated from any subsequent parts (like country/region or script) by an underscore.
no setter
defaultLocaleStr → StringLocale
The default locale, as a String.
final
hashCode int
The hash code for this object.
no setterinherited
length int
Returns the number of translation-keys. For example, if you have translations for 'Hi' and 'Goodbye', this will return 2.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
translationByLocale_ByTranslationKey Map<TKEY, TRANbyLOCALE>
Map of translations by locale, by translation-key. It's something like this:
final

Methods

load([Duration? timeout, VoidCallback? callback]) Future<void>

Available on Translations<dynamic, Map<StringLocale, StringTranslated>, Map<dynamic, StringTranslated>, dynamic>, provided by the I18nTranslationsExtension extension

Calling load starts the process of loading the translations. It returns a future which you can use to wait for the translations to be loaded.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator *(Translations<TKEY, TRANbyLOCALE, TRANbyTKEY, dynamic> translationsObj) Translations<TKEY, TRANbyLOCALE, TRANbyTKEY, ADDEDMAP>
Add a translationsObj object to another Translations object.
operator +(ADDEDMAP addedMap) Translations<TKEY, TRANbyLOCALE, TRANbyTKEY, ADDEDMAP>
Add a addedMap (of type Map) to a Translations object.
operator ==(Object other) bool
The equality operator.
inherited
operator [](TKEY key) → TRANbyLOCALE?
Given a translations key, returns a Map of translations for that key. The map will have the locale as the key, and the translation as the value.

Static Properties

loadByFile Future<void> Function(TranslationsByLocale<String, Map<StringLocale, StringTranslated>, Map<String, StringTranslated>, Map<StringLocale, Map<String, StringTranslated>>> translations)
The load-by-file process can be set by a third-party package. It should modify/mutate the translation map in some way, like loading it from a file, or from a database, and then rebuild the widgets.
no getter
loadByHttp Future<void> Function(TranslationsByLocale<String, Map<StringLocale, StringTranslated>, Map<String, StringTranslated>, Map<StringLocale, Map<String, StringTranslated>>> translations)
The load-by-http process can be set by a third-party package. It should modify/mutate the translation map in some way, like loading it from a https address, and then rebuild the widgets.
no getter
missingKeyCallback ↔ void Function(Object? key, StringLocale locale)
Replace this to log missing keys.
getter/setter pair
missingKeys Set<TranslatedString>
All missing keys and translations will be put here. This may be used in tests to make sure no translations are missing.
getter/setter pair
missingTranslationCallback MissingTranslationCallback
The default implementation of missingTranslationCallback prints missing translations to the console. If supportedLocales is provided, it will only consider missing translations for locales that are supported. If the supported locales is empty (not provided) it will log everything.
getter/setter pair
missingTranslations Set<TranslatedString>
getter/setter pair
recordMissingKeys bool
If true, records missing translations keys.
getter/setter pair
recordMissingTranslations bool
If true, records missing translations.
getter/setter pair
supportedLocales Iterable<String>
Contains the locales that are supported, as BCP47 language tags. For example: ['en-US', 'pt-BR', 'es', 'zh-Hans-CN']
getter/setter pair

Static Methods

byFile(StringLocale defaultLocaleStr, {required String dir}) Translations<dynamic, Map<StringLocale, StringTranslated>, Map<dynamic, StringTranslated>, dynamic>
The byFile constructor allows you to read all locale translations from files. Note this works from i18n_extension, but not from i18n_extension_core.
byHttp(StringLocale defaultLocaleStr, {required String url, required Iterable<String> resources}) Translations<dynamic, Map<StringLocale, StringTranslated>, Map<dynamic, StringTranslated>, dynamic>
The byHttp constructor allows you to read all locale translations from the web. Note this works from i18n_extension, but not from i18n_extension_core.
byId<TKEY>(StringLocale defaultLocaleStr, Map<TKEY, Map<StringLocale, StringTranslated>> translationByLocale_ByTranslationKey) Translations<dynamic, Map<StringLocale, StringTranslated>, Map<dynamic, StringTranslated>, dynamic>
The byId constructor allows you to provide all locale translations related to a first identifier, then for a second identifier, and so on. Identifiers may be any immutable object, such as a String, int, enum, or any object you create.
byLocale(StringLocale defaultLocaleStr) Translations<dynamic, Map<StringLocale, StringTranslated>, Map<dynamic, StringTranslated>, dynamic>
The Translations.byLocale constructor allows you to provide, for each locale, all translations together:
byText(StringLocale defaultLocaleStr) Translations<dynamic, Map<StringLocale, StringTranslated>, Map<dynamic, StringTranslated>, dynamic>
The Translations.byText constructor allows you to provide all locale translations of the first translatable string, then all locale translations of the second translatable string, and so on; and then you add translations with the + operator. For example: