How to use the changeLanguage function from i18next

Find comprehensive JavaScript i18next.changeLanguage code examples handpicked from public code repositorys.

i18next.changeLanguage is a JavaScript function that changes the current language of the i18next internationalization library.

82
83
84
85
86
87
88
89
90
91

i18n.changeLanguage = function(languageSelected) {
    if (typeof ConfigStorage !== 'undefined') {
        ConfigStorage.set({'userLanguageSelect': languageSelected});
    }
    i18next.changeLanguage(getValidLocale(languageSelected));
    i18n.selectedLanguage = languageSelected;
    console.log(i18n.getMessage('language_changed'));

};
fork icon6
star icon46
watch icon6

42
43
44
45
46
47
48
49
50
51

updateAvailableLanguages()

emitter.on('set language', (lang) => {
  // console.log('setting language to', lang)
  i18next.changeLanguage(lang, (err, t) => {
    // console.log(err, t)
    state.translation.selectedLanguage = lang
    emitter.emit('render')
  })
fork icon228
star icon0
watch icon62

+ 3 other calls in file

How does i18next.changeLanguage work?

i18next.changeLanguage works by taking a string representing the new language code as input and changing the current language of the i18next library to the specified language. The function updates the internal state of the i18next library, such as the language code and the translations, to match the new language. This allows developers to dynamically change the language of the application without having to reload the page or restart the application. By providing a convenient and reliable way to change the language of the i18next library, i18next.changeLanguage can help to simplify and streamline the internationalization process in multilingual applications.

33
34
35
36
37
38
39
40
41
42
                },
        });

(async () => {
        if (isDevelopment && config.has('debug_forcedLanguage')) {
                i18next.changeLanguage(config.get('debug_forcedLanguage'));
                return;
        }

        await appReady;
fork icon61
star icon151
watch icon12

+ 3 other calls in file

31
32
33
34
35
36
37
38
39
40
  return key;
}

function changeLanguage(locale) {
  if (locale) {
    i18n.changeLanguage(locale);
  }
}

function addResourceBundle(lng, ns, resources) {
fork icon3
star icon22
watch icon3

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const i18next = require("i18next");

// Define some translations for the 'en' (English) language
const enTranslations = {
  greeting: "Hello, world!",
  farewell: "Goodbye, world!",
};

// Define some translations for the 'es' (Spanish) language
const esTranslations = {
  greeting: "¡Hola, mundo!",
  farewell: "¡Adiós, mundo!",
};

// Initialize an i18next instance with the 'en' translations
i18next.init({
  lng: "en",
  resources: {
    en: {
      translation: enTranslations,
    },
  },
});

// Change the current language of the i18next instance to 'es'
i18next.changeLanguage("es", (error, t) => {
  if (error) {
    console.error(error);
    return;
  }

  // Output some translations in the new language to the console
  console.log(t("greeting")); // Output: ¡Hola, mundo!
  console.log(t("farewell")); // Output: ¡Adiós, mundo!
});

In this example, we use i18next.changeLanguage() to change the current language of an i18next instance from English to Spanish. We first initialize the i18next instance with some translations for the English language using i18next.init(). We then call i18next.changeLanguage() with the string 'es' to change the current language to Spanish. The function takes a callback that can be used to access the translation functions for the new language. We output some translations in the new language to the console using the t() function from the callback. By using i18next.changeLanguage, we can change the language of an i18next instance dynamically and access the appropriate translations for the new language.

228
229
230
231
232
233
234
235
236
237
ipcMain.on('setAskBeforeClose', (e, val) => {
  askBeforeClose = val;
});

ipcMain.on('setLanguage', (e, language) => {
  i18n.changeLanguage(language).then(() => updateMenu()).catch((err) => logger.error('Failed to set language', err));
});

ipcMain.handle('tryTrashItem', async (e, path) => {
  try {
fork icon909
star icon0
watch icon195

346
347
348
349
350
351
352
353
354
355
356
    })
}


async function setLanguage(interaction, language = undefined) {
    if (language !== undefined) {
        await i18next.changeLanguage(language);
        return;
    }
    try {
        collection.findOne({guild_id: {$eq: interaction.guild.id}}, async (err, result) => {
fork icon1
star icon1
watch icon1

+ 137 other calls in file