Lang class

Class used to translate strings with the currently set locale.

This class is the main entry point to translate strings with the currently set locale. It uses a LangProvider to load and access the translations.

The LangProvider can be changed with the use method. This is useful in tests or advanced use cases where a different provider is needed.

The current request locale can be changed with the setRequestLocale method. This method changes the locale for all future calls to t and getField.

The t method translates a key with optional parameters. The choice method handles pluralization. The getField method translates a field label.

Advanced Features

  • Parameter replacement with :param syntax
  • Pluralization with count-based choices
  • Namespace support for packages
  • Fallback locales for missing translations
  • Custom parameter replacers for advanced formatting
  • Request-scoped locales for per-request localization

Basic Usage

// Set global locale
Lang.setGlobalLocale('en');
Lang.setFallbackLocale('en');

// Basic translation
String greeting = Lang.t('messages.greeting');

// Translation with parameters
String welcome = Lang.t('messages.welcome', parameters: {'name': 'Alice'});

// Pluralization
String items = Lang.choice('items.count', 5);

// Field translation
String label = Lang.getField('email');

Request-Specific Localization

// Set locale for current request
Lang.setRequestLocale('fr');

// All subsequent calls in this request will use French
String message = Lang.t('messages.hello'); // Uses French if available

Namespace Support

// Load package translations
Lang.loadNamespace('package:auth', 'en', {'login': 'Sign In'});

// Use namespaced translation
String login = Lang.t('login', namespace: 'package:auth');

Custom Parameter Replacers

// Add custom replacer for date formatting
Lang.addParameterReplacer((key, value, params) {
  if (key == 'date' && value is DateTime) {
    return value.toString(); // Custom date formatting
  }
  return ':$key'; // Default replacement
});

Constructors

Lang()

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 Methods

addParameterReplacer(String replacer(String key, dynamic value, Map<String, dynamic> parameters)) → void
Adds a custom parameter replacer.
choice(String key, int count, {Map<String, dynamic>? parameters, String? locale, String? namespace}) String
Translates with pluralization based on count.
clearCache() → void
Clears the translation cache.
get(String key, {String? locale, String? namespace}) String?
Gets the raw translation value.
getAvailableLocales() List<String>
Gets all available locales.
getFallbackLocale() String
Gets the fallback locale.
getField(String field, {String? locale, String? namespace}) String
Translates a field label.
getGlobalLocale() String
Gets the current global 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 namespace.
setFallbackLocale(String locale) → void
Sets the fallback locale.
setGlobalLocale(String locale) → void
Sets the global locale.
setRequestLocale(String locale) → void
Sets the current request locale.
t(String key, {Map<String, dynamic>? parameters, String? locale, String? namespace}) String
Translates a key with optional parameters and namespace.
use(LangProvider provider) → void
Change the LangProvider used.