javascriptsails.jsi18nexti18next-http-backend

how to use i18next in sailsjs


we are using handlebar to render the view and now must use i18next lib to render the translated pages in our sailsjs backend but i could not find any example on how to integrate i18next in sailsjs and use it in my template.

i am aware that there is i18next-http-middleware but i could not get my head around how to integrate it with sailsjs (very new to sailsjs) and official docs of i18next also does not have any example.


Solution

  • i was able to make it work with Built-in HTTP middleware configuration by adding a new middleware function in order array and initializing the i18next in the provided functions.

    // config/http.js
    
    const i18next = require("i18next"),
      middleware = require("i18next-http-middleware"),
      i18nConfig = require("./i18n").i18n;
    
    module.exports.http = {
      middleware: {
        order: ["i18n", "...other middlewares"],
    
        i18n: (function () {
          i18next.use(middleware.LanguageDetector).init(i18nConfig);
    
          return middleware.handle(i18next, {});
        })(),
      },
    };