addTranslation method

void addTranslation({
  1. required String locale,
  2. required String translationKey,
  3. required String stringTranslated,
})
inherited

Add a translationKey/stringTranslated pair to the translations. You must provide non-empty locale and translationKey, but the stringTranslated may be empty (for the case when some text shouldn't be displayed in some language).

If locale or translationKey are empty, an error is thrown. However, if both the translationKey and stringTranslated are empty, the method will ignore it and won't throw any errors.

Implementation

void addTranslation({
  required String locale,
  required String translationKey,
  required String stringTranslated,
}) {
  if (locale.isEmpty) throw TranslationsException("Missing locale.");
  if (translationKey.isEmpty) {
    if (stringTranslated.isEmpty)
      return;
    else
      throw TranslationsException("Missing key.");
  }

  // ---

  Map<StringLocale, StringTranslated>? _translations =
      translationByLocale_ByTranslationKey[translationKey];

  if (_translations == null) {
    _translations = {};
    translationByLocale_ByTranslationKey[translationKey as TKEY] = _translations as ADDEDMAP;
  }
  _translations[locale] = stringTranslated;
}