LangProvider class abstract
LangProvider contract that defines the interface for a language provider.
The LangProvider contract is used to define the interface for a language provider. The language provider is responsible for providing the translations for the application, with advanced features like pluralization, parameter replacement, and context support.
The LangProvider contract defines the following methods:
- setLocale: Sets the locale for the language provider.
- getLocale: Gets the current locale.
- setFallbackLocale: Sets the fallback locale.
- t: Translates a key with optional parameters and context.
- choice: Translates with pluralization based on count.
- field: Translates a field label.
- has: Checks if a translation key exists.
- get: Gets raw translation value.
- loadNamespace: Loads translations for a namespace (e.g., packages).
- clearCache: Clears any cached translations.
The t method supports advanced parameter replacement using :param syntax. The choice method handles plural forms based on count. Namespaces allow organizing translations for different packages or modules.
Example Usage
class MyLangProvider implements LangProvider {
// Implementation here
}
final provider = MyLangProvider();
provider.setLocale('en');
provider.setFallbackLocale('en');
// Basic translation
String greeting = provider.t('messages.greeting');
// Translation with parameters
String welcome = provider.t('messages.welcome', parameters: {'name': 'Alice'});
// Pluralization
String items = provider.choice('items.count', 5, parameters: {'count': 5});
// Field translation
String label = provider.field('email');
// Namespace support
provider.loadNamespace('package:auth', 'en', {'login': 'Sign In'});
String login = provider.t('login', namespace: 'package:auth');
Translation File Structure
Translation files are typically JSON files organized by locale:
lang/
├── en/
│ ├── messages.json
│ └── validation.json
└── fr/
├── messages.json
└── validation.json
Parameter Replacement
Parameters are replaced using :param syntax:
{
"welcome": "Hello :name, welcome to :app",
"items": "You have :count item|You have :count items"
}
Pluralization
Use | to separate singular and plural forms:
{
"apple": "apple|apples",
"item": "item|items"
}
- Implementers
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
-
addParameterReplacer(
String replacer(String key, dynamic value, Map< String, dynamic> parameters)) → void - Adds a custom parameter replacer function.
-
choice(
String key, int count, {Map< String, dynamic> ? parameters, String? locale, String? namespace}) → String - Translates with pluralization based on count.
-
clearCache(
) → void - Clears any cached translations.
-
field(
String key, {String? locale, String? namespace}) → String - Translates a field label.
-
get(
String key, {String? locale, String? namespace}) → String? - Gets the raw translation value without parameter replacement.
-
getAvailableLocales(
) → List< String> - Gets all available locales.
-
getFallbackLocale(
) → String - Gets the fallback locale.
-
getLocale(
) → String - Gets the current locale.
-
has(
String key, {String? locale, String? namespace}) → bool - Checks if a translation key exists.
-
loadNamespace(
String namespace, String locale, Map< String, String> translations) → void - Loads translations for a specific namespace.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
setFallbackLocale(
String locale) → void - Sets the fallback locale for missing translations.
-
setLocale(
String locale) → void - Sets the locale for the language provider.
-
t(
String key, {Map< String, dynamic> ? parameters, String? locale, String? namespace}) → String - Translates a key with optional parameters, context, and namespace.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited