Translator class

The Translator Service.

Singleton service that manages translations and provides O(1) lookups. Integrates with DateManager for consistent date localization. Supports runtime locale switching with change notifications.

Usage

The Translator is typically accessed via the Lang facade or trans() helper:

// Via facade
Lang.get('welcome', {'name': 'Magic'});

// Change locale at runtime
await Lang.setLocale(Locale('tr'));

// Detect device locale
Lang.detectLocale();
Inheritance

Properties

fallbackLocale Locale
Get the fallback locale.
no setter
hashCode int
The hash code for this object.
no setterinherited
hasListeners bool
Whether any listeners are currently registered.
no setterinherited
isLoaded bool
Check if translations are loaded.
no setter
locale Locale
Get the current locale.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
sentences Map<String, String>
Get all loaded sentences.
no setter
supportedLocales List<Locale>
Get supported locales.
no setter

Methods

addListener(VoidCallback listener) → void
Register a closure to be called when the object changes.
inherited
choice(String key, int number, [Map<String, dynamic>? replace]) String
Get a translation by key, choosing the form that suits number.
detectAndSetLocale() Future<Locale>
Detect and set the best matching locale from device/browser.
detectLocale() Locale
Detect the best matching locale from device/browser.
dispose() → void
Discards any resources used by the object.
inherited
get(String key, [Map<String, dynamic>? replace]) String
Get a translation by key.
has(String key) bool
Check if a translation exists.
load(Locale locale) Future<void>
Load translations for the given locale.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
notifyListeners() → void
Call all the registered listeners.
inherited
removeListener(VoidCallback listener) → void
Remove a previously registered closure from the list of closures that are notified when the object changes.
inherited
setFallbackLocale(Locale locale) → void
Set the fallback locale.
setLoader(TranslationLoader loader) → void
Set the translation loader.
setLocale(Locale locale) Future<void>
Switch to a new locale at runtime.
setSupportedLocales(List<Locale> locales) → void
Set supported locales.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Properties

instance Translator
Get the singleton instance.
no setter

Static Methods

reset() → void
Reset the translator (for testing).