translateWithArgs method

String translateWithArgs(
  1. String key, [
  2. Map<String, String>? args
])

Translate key to this locale. If key is not translated, key is returned 'as-is'. If args is provided, it replaces placeholders in the translation. Example:

translateWithArgs('greeting', {'name': 'John'});

If the translation is "Hello, {name}!", it will return "Hello, John!".

Implementation

String translateWithArgs(String key, [Map<String, String>? args]) {
  /// Translate [key] to this [locale].
  /// /// If [key] is not translated, [key] is returned 'as-is'.
  if (translations.containsKey(key)) {
    String translation = translations[key]!;
    if (args != null) {
      args.forEach((argKey, argValue) {
        translation = translation.replaceAll('{{$argKey}}', argValue);
      });
    }
    return translation;
  }
  return key;
}