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
: The language and country code, like "en_us" or "pt_br".translated string
: The translated strings for a giventranslation-key
.identifier
: An immutable variable that you may use as a translation-key, instead of the string itself.
You may use this class to
The options are:
- Translations.byText
- Translations.byLocale
- Translations.byId
- ConstTranslations.new which can be made const
Translations.byText example:
var t = Translations.byText("en_us") +
const {
"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",
});
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.
- Implementers
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
-
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
- 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 ↔ void Function(Object? key, StringLocale locale)
-
Replace this to log missing translations.
getter/setter pair
-
missingTranslations
↔ Set<
TranslatedString> -
getter/setter pair
- recordMissingKeys ↔ bool
-
If true, records missing keys.
getter/setter pair
- recordMissingTranslations ↔ bool
-
If true, records missing translations.
getter/setter pair
Static Methods
-
byId<
TKEY> (StringLocale defaultLocaleStr, Map< TKEY, Map< translationByLocale_ByTranslationKey) → Translations<StringLocale, StringTranslated> >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: