localizeVersion function

String localizeVersion(
  1. Object modifier,
  2. String key,
  3. ITranslations translations, {
  4. String? locale,
})

Return the localized string, according to key and modifier.

You may pass any type of object to the modifier, but it will to a toString() in it and use that. So make sure your object has a suitable string representation.

Implementation

String localizeVersion(
  Object modifier,
  String key,
  ITranslations translations, {
  String? locale,
}) {
  locale = locale?.toLowerCase();

  String total = localize(key, translations, locale: locale);

  if (!total.startsWith(_splitter1))
    throw TranslationsException("This text has no version for modifier '$modifier' "
        "(modifier: $modifier, "
        "key: '$key', "
        "locale: '${_effectiveLocale(locale)}').");

  List<String> parts = total.split(_splitter1);

  for (int i = 2; i < parts.length; i++) {
    var part = parts[i];
    List<String> par = part.split(_splitter2);
    if (par.length != 2 || par[0].isEmpty || par[1].isEmpty)
      throw TranslationsException("Invalid text version for '$part'.");
    String _modifier = par[0];
    String text = par[1];
    if (_modifier == modifier.toString()) return text;
  }

  throw TranslationsException("This text has no version for modifier '$modifier' "
      "(modifier: $modifier, "
      "key: '$key', "
      "locale: '${_effectiveLocale(locale)}').");
}