translatedText static method

String translatedText(
  1. String key, {
  2. Map<String, dynamic>? arguments,
  3. bool replaceAll = false,
})

Utils function to retrieve the dynamic translated text in base of any key

If arguments is provided all the keyworks will be substituted, for example: if string 'foo' is "This is my {{adjective}} string! {{after_string}}".

Providing {"adjective": "Awesome", "after_string": "Save this post"} as arguments, (with key == 'foo!) will return "This is my awesome string! Save this post"

arguments have dynamic as type of value! Be carefull, .toString() will be called to the object if it isn't already a String

If replaceAll is true all the occurences will be replaced, only the first one instead

Implementation

static String translatedText(String key, {Map<String, dynamic>? arguments, bool replaceAll = false}) {
  if (arguments == null || arguments.isEmpty) return Utils.getIt<LocalizationClass>().dynamicValue(key);

  String _string = Utils.getIt<LocalizationClass>().dynamicValue(key);

  for (String key in arguments.keys) {
    if (replaceAll)
      _string = _string.replaceAll('{{$key}}', arguments[key]!.toString());
    else
      _string = _string.replaceFirst('{{$key}}', arguments[key]!.toString());
  }

  return _string;
}