ConstTranslations<TKEY extends String, TRANbyLOCALE extends Map<StringLocale, StringTranslated> , TRANbyTKEY extends Map<TKEY, StringTranslated> , ADDEDMAP extends TRANbyLOCALE> class
The ConstTranslations class allows you to define the translations
as a const object, all at once. This not only is a little bit more
efficient, but it's also better for "hot reload", since a const variable
will respond to hot reloads, while final
variables will not.
Here you provide all locale translations of the first "translatable string", then all locale translations of the second one, and so on.
static const t = ConstTranslations("en_us",
{
"i18n Demo": {
"en_us": "i18n Demo",
"pt_br": "Demonstração i18n",
}
},
);
IMPORTANT: Make sure the defaultLocaleStr you provide is correct (no spaces, lowercase etc). Since this constructor is const, we can't normalize the locale string for you. If you are not sure, call ConstTranslations.normalizeLocale before using it.
This class is visible from both i18_exception
and i18_exception_core
packages.
- Inheritance
-
- Object
- Translations<
TKEY, TRANbyLOCALE, TRANbyTKEY, ADDEDMAP> - ConstTranslations
Constructors
-
ConstTranslations(StringLocale defaultLocaleStr, Map<
TKEY, TRANbyLOCALE> translationByLocale_ByTranslationKey) -
The ConstTranslations constructor allows you to define the translations
as a const object, all at once. This not only is a little bit more
efficient, but it's also better for "hot reload", since a const variable
will respond to hot reloads, while
final
variables will not.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 setterinherited
- defaultLocaleStr → StringLocale
-
The default locale, as a String.
finalinherited
- 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 setterinherited
- 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:
finalinherited
Methods
-
addTranslation(
{required String locale, required String translationKey, required String stringTranslated}) → void -
Add a
translationKey
/stringTranslated
pair to the translations. You must provide non-emptylocale
andtranslationKey
, but thestringTranslated
may be empty (for the case when some text shouldn't be displayed in some language).inherited -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
Prints the translations in a human-readable format.
inherited
Operators
-
operator *(
Translations< TKEY, TRANbyLOCALE, TRANbyTKEY, dynamic> translations) → Translations<TKEY, TRANbyLOCALE, TRANbyTKEY, ADDEDMAP> -
You can't add a
Translation
to aConstTranslations
. Which means operator*
is not supported forConstTranslations
: -
operator +(
ADDEDMAP addedMap) → Translations< TKEY, TRANbyLOCALE, TRANbyTKEY, ADDEDMAP> - You can't add a
-
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.inherited
Static Methods
-
normalizeLocale(
String locale) → String -
- Removes all spaces (and similar chars that are generally removed with the trim method).
- Turns all hyphens into underscores.
- Turns double (or more underscores) into a single underscore.
- Trims leading and trailing underscores.
- Turns the locale into lowercase.
- Throws a TranslationsException if the locale has more than 20 characters or fewer than 2. Examples:
en_us_
becomesen_us
.en__us_
becomesen_us
.en-us
becomesen_us
.en-
becomesen
.