Translator class
Core translation class that handles string translations and parameter replacement. This class provides static methods for translating strings and replacing dynamic parameters.
The translator uses a configuration (TranslationsConfig) that defines:
- Supported languages
- Default language
- Translation structure
- Parameter patterns
- Fallback values
Example:
// Configure the translator
Translator.config = MyConfig();
// Translation setup:
// {
// 'greeting': {
// 'en': 'Hello {name}!',
// 'es': '¡Hola {name}!'
// }
// }
// Translate a string
final translated = Translator.translate(
'greeting',
'en',
ParamModel(key: 'name', value: 'John')
);
// Just replace parameters in a string with placeholders
final dynamized = Translator.dynamize(
'Hello {name}!',
ParamModel(key: 'name', value: 'John')
);
Constructors
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
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 ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- config ↔ TranslationsConfig
-
The configuration that will be used for all translations.
You can set this to your own TranslationsConfig implementation,
or modify the default one using DefaultConfig.copyWith.
getter/setter pair
Static Methods
-
dynamize(
String targetText, [ParamModel? param1, ParamModel? param2, ParamModel? param3]) → String -
Replaces parameter placeholders in the text with provided values.
This is a low-level method used internally by translate and the
DynX
extension. -
translate(
String key, String? languageCode, [ParamModel? param1, ParamModel? param2, ParamModel? param3]) → String - Translates a key to the target language and replaces any parameters.