Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

173
Visualizações
How to set i18n.js fallback language?

I am using the i18n-js library.

In order to set its configuration, I am doing the following:

export function setI18nConfig(locale = undefined) {
  // Fallback if no available language fits
  const fallback = DEFAULT_APP_LANGUAGE;
  const language =
    APP_LANGUAGES.find((language) => language.locale === locale) ?? fallback;

  const { locale: _locale, isRTL } = language;

  // Clear the translation cache
  translate.cache.clear();

  // Update the layout direction
  I18nManager.forceRTL(isRTL);

  // Set the i18n-js config
  i18n.locale = _locale;
  i18n.translations = { [_locale]: translationGetters[_locale]() };

  // Set moment config
  moment.locale(_locale);

  // Return the appropiate locale
  return _locale;
}

As you can see, in order to set the translations dictionary, I am calling this getter:

 export const translationGetters = {
  en: () => require("../../../assets/languages/en/translations.json"),
  es: () => require("../../../assets/languages/es/translations.json"),
  fr: () => require("../../../assets/languages/fr/translations.json"),
  it: () => require("../../../assets/languages/it/translations.json"),
  pt: () => require("../../../assets/languages/pt/translations.json"),
};

For establishing the fallback language, I am just trying to find the given locale (function argument) in my array of app languages.

export const APP_LANGUAGES = [
  {
    name: "English",
    locale: "en",
    isRTL: false,
  },
  {
    name: "French",
    locale: "fr",
    isRTL: false,
  },
  {
    name: "Italian",
    locale: "it",
    isRTL: false,
  },
  {
    name: "Portuguese",
    locale: "pt",
    isRTL: false,
  },
  {
    name: "Spanish",
    locale: "es",
    isRTL: false,
  },
];

If it is not in the list, then I use the DAFAULT_APP_LANGUAGE as translation.

export const DEFAULT_APP_LANGUAGE = APP_LANGUAGES[0];

Is this the typical or correct way to set i18n.js fallbacks?

Isn't there any simple way to do that?

I mean, something like:

  i18n.fallbackLanguage = "en";
  i18n.locale = locale;
  i18n.translations = {
     [locale]: translationGetters[locale](), 
     [fallbackLanguage]: translationGetters[fallbackLanguage]() 
  };

Note: I also need to set the isRTL config for the fallback!

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda