trParams method
Translates the string key with parameters using the LocalizationManager from the provided context.
This method uses the LocalizationManager from the nearest LocalizationProvider
in the widget tree to translate the string key with the given parameters. Placeholders in the
translation string are replaced with the values from the params map. For example, if the translation
string is "Hello, {name}!", and params is {'name': 'World'}, the output will be "Hello, World!".
If the key is not found, it returns a predefined "Key not found" message, which can be customized.
Usage example:
Text('greeting'.trParams({'name': 'World'}, context))
-
Parameters:
- params: A map of string keys to dynamic values used to replace placeholders in the translation string.
- context: Optional. The
BuildContextused to locate theLocalizationProviderand access theLocalizationManager.
-
Returns: The translated string with parameters inserted, or a "Key not out found" message if the translation key is not found.
-
Throws: Throws an exception if
LocalizationProvideris not found in the widget tree.
Implementation
String trParams(Map<String, dynamic> namedArgs, [BuildContext? context]) {
return LocalizationProvider.of(context)
.translateWithParams(this, namedArgs);
}