Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

177
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda