getLocalizationByLocale method

String getLocalizationByLocale(
  1. String key,
  2. Locale locale
)

Gets the localized value by a specific locale

Implementation

String getLocalizationByLocale(String key, Locale locale) {
  var entry = entries.firstWhereOrNull((element) => element.key == key);

  if (entry == null) {
    log('No entry found for key $key');
    return '';
  }

  var localizedValue = entry.localizedValues.firstWhereOrNull((element) => element.locale == locale);

  if (localizedValue != null) {
    return localizedValue.localeValue;
  }

  if (localizedValue == null) {
    var fallbackEntry = entry.localizedValues.firstWhereOrNull((element) => element.locale == _fallbackLocale);

    if (fallbackEntry == null) {
      throw Exception(
          'Whether the value for the current locale nor the value for the fallback locale has been found for key $key');
    }

    return fallbackEntry.localeValue;
  }

  throw Exception('Something really bad happened, need to fix this!');
}