How to use the use function from i18next

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

The i18next.use function is used to add a plugin to the i18next translation framework.

27
28
29
30
31
32
33
34
35
36
        loadPath: path.resolve(__dirname, '../languages/{{lng}}/{{ns}}.json'),
};

const { namespaces, languages } = await walkDirectory(path.resolve(__dirname, '../languages/'));

i18next.use(Backend);
await i18next.init({
        backend: options,
        debug: false,
        fallbackLng: 'en-US',
fork icon325
star icon475
watch icon19

41
42
43
44
45
46
47
48
49
50

const dir = await FileUtils.readdir(dirPath)

return new Promise((resolve) => {
  try {
    i18next.use(translationBackend).init({
      ns: ['categories', 'commands', 'commons', 'errors', 'music', 'permissions', 'regions', 'moderation', 'lolservers', 'languages', 'countries', 'game'],
      preload: dir,
      fallbackLng: 'en-US',
      backend: {
fork icon238
star icon405
watch icon14

How does i18next.use work?

i18next.use() is a method provided by the i18next library in JavaScript that allows you to add a plugin or middleware to the i18next instance.

It takes a single argument, which is the plugin/middleware function that you want to add to the i18next instance.

This method enables you to add extra functionality to i18next, like loading translations from a remote API or caching translations to improve performance.

30
31
32
33
34
35
36
37
38
39
    jsonIndent: 2
};
let dirs = await this.getDirs('rem_translate/');
this.list = dirs;
return new Promise(function (resolve, reject) {
    i18next.use(Backend).init({
        backend: backendOptions,
        lng: 'en',
        fallbacklngs: false,
        preload: dirs,
fork icon47
star icon78
watch icon21

118
119
120
121
122
123
124
125
126
127
    client.webhooks = new webhookStructure_(client);
}
async loadLocates(){
    let path = `${process.cwd()}/locates`
    try {
        await i18next.use(Backend).init({
            ns: ["commands", "events", "permissions"],
            defaultNS: "commands",
            preload: fs.readdirSync(path),
            fallbackLng: "pt-BR",
fork icon0
star icon1
watch icon0

Ai Example

1
2
3
4
5
6
import i18next from "i18next";
import myPlugin from "./myPlugin";

i18next.use(myPlugin).init({
  // options
});

This code imports the i18next library and a custom plugin called myPlugin, and then uses i18next.use to add the plugin to the i18next instance. The init function is then called with options to initialize i18next. The myPlugin will then be used by i18next during translation.

766
767
768
769
770
771
772
773
774
775
776
// if it can't find a matching language.
const LocalizationInterceptor = {
  process(handlerInput) {
    const { requestEnvelope, attributesManager } = handlerInput;


    const localizationClient = i18n.use(sprintf).init({
      lng: requestEnvelope.request.locale,
      fallbackLng: 'en-US',
      resources: languageStrings,
    });
fork icon0
star icon0
watch icon1

136
137
138
139
140
141
142
143
144
145
function init(settings) {
    if (!initPromise) {
        // Keep this as a 'when' promise as top-level red.js uses 'otherwise'
        // and embedded users of NR may have copied that.
        initPromise = new Promise((resolve,reject) => {
            i18n.use(MessageFileLoader);
            var opt = {
                // debug: true,
                defaultNS: "runtime",
                ns: [],
fork icon0
star icon0
watch icon1

+ 35 other calls in file