I18n class
Internationalization (i18n) middleware.
Provides translation support with:
- Multiple locale support
- Language detection from Accept-Language header, cookie, or query parameter
- Variable interpolation:
'Hello {name}' - Pluralization support:
{'one': '1 item', 'other': '{count} items'} - Fallback locale
Example
final translations = {
'en': {
'greeting': 'Hello, {name}!',
'items': {'one': '1 item', 'other': '{count} items'},
},
'ja': {
'greeting': 'こんにちは、{name}さん!',
'items': {'other': '{count}個のアイテム'},
},
};
app.use(I18n(translations));
app.get('/').handle((ctx) async {
// Simple translation
final greeting = ctx.t('greeting', {'name': 'John'});
// Pluralization
final items = ctx.t('items', {'count': 5});
await ctx.res.json({'greeting': greeting, 'items': items});
});
- Implemented types
- Available extensions
Constructors
-
I18n(Translations translations, {String defaultLocale = 'en', List<
String> supportedLocales = const [], String? cookieName = 'locale', String? queryParam = 'lang', LocaleResolver? localeResolver, bool detectFromHeader = true}) -
Creates an I18n middleware.
const
Properties
-
Cookie name for locale override.
final
- defaultLocale → String
-
Default locale when detection fails.
final
- detectFromHeader → bool
-
Whether to detect locale from Accept-Language header.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- localeResolver → LocaleResolver?
-
Custom locale resolver. Takes precedence over built-in detection.
final
- queryParam → String?
-
Query parameter name for locale override.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
supportedLocales
→ List<
String> -
Supported locales. If empty, all locales in translations are supported.
final
- translations → Translations
-
Translation messages.
final
Methods
-
handle(
Context ctx, NextFunction next) → FutureOr< void> -
override
-
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