i18next / i18next-express-middleware

[deprecated] can be replaced with i18next-http-middleware
https://github.com/i18next/i18next-http-middleware
MIT License
207 stars 55 forks source link

Possible to change the url structure #183

Closed sowmiyaseshadriii closed 5 years ago

sowmiyaseshadriii commented 5 years ago

thanks for middleware. I have dropdown with languges , when i change , it translates but Is it possible to change the url structure ."/?lng=en to /en" , If i do so, not working,

//app.js
var express = require('express');
var app = express();
var homeRouter= require('./routes/home');
const i18next = require('i18next');
const i18nextMiddleware = require('i18next-express-middleware');
const Backend = require('i18next-node-fs-backend');
i18next
  .use(Backend)
  .use(i18nextMiddleware.LanguageDetector)
  .init({
    backend: {
      loadPath: __dirname + '/locales/{{lng}}.json',
      addPath: __dirname + '/locales/{{lng}}.missing.json'
    },
    detection: {
      order: ['querystring', 'cookie'],
      caches: ['cookie']
    },
    fallbackLng: 'en',
    preload: ['en', 'zh'],
    saveMissing: true
  });
  app.use(bodyParser.urlencoded({ extended: true }));
  app.use(bodyParser.json());
  app.use(i18nextMiddleware.handle(i18next));

//home.js
var express = require('express');
var router = express.Router();
router.get('/', function (req, res) {
  res.render("index.ejs") 
}

//index.ejs
<div>
 <h1><%=t("Welcome")%></h1>  //working fine in  template 
 <ul>
            <li class="nav-item dropdown" style="list-style-type: none;">
            <a class="dropdown-item" href="/?lng=en">EN</a></li>
            <li class="nav-item dropdown" style="list-style-type: none;">
            <a class="dropdown-item" href="/?lng=zh">ZH</a>
            </li>
          </li>
        </ul>
</div>

//en.json
{
  "Welcome": "Hi , Welcome to my site",
  "list": "Discover the List of Travelling Cities"
}
jamuhl commented 5 years ago

https://github.com/i18next/i18next-express-middleware#add-localized-routes