i18next on jade

Created on 12 Jan 2017  路  9Comments  路  Source: i18next/i18next

Hi! I'm using i18next in NodeJS with express, everything works well except the t object inside jade.
Here is my config:

        .use(Backend)
        .init({
              "whitelist": ["en", "it"],
                   "preload": ["en", "it"],
                   "ns": [
                       "api", "common"
                     ],
                   "defaultNS": "api",
                   "debug": true,
                   "interpolation": {
                      "escapeValue": false
                   },
                  "backend": {
                       "loadPath": "/locales/{{lng}}/{{ns}}.json"
                  },
                 "detection": {
                      "order": ["querystring", "path", "header"],
                     "lookupQuerystring": "lng",
                     "lookupPath": "lng"
                  }
        });
app.use(middleware.handle(i18next, {
    removeLngFromUrl: false
}));`

TEST.JADE

div=t('test')

t is not a function

Thank you!

All 9 comments

do you have a repo i could checkout....should work...at least i'm not aware of an issue yet.

i'm sorry i don't have.... maybe the problem is with jade.. and i have to use pug instead?

shouldn't make a difference i think...but honestly i do not test against latest express, jade/pug - as i personally do not use this module...

simplest would be you publish your progress on github and let me see the code and test that.

With pug everything works! Thank you :) 馃憤

good to know...thank you for sharing your finding.

I just stumbled into this and the issue for me wasn't jade or pug but rather how you pass the i18next.t function to the templating layer. So for anyone ending up here:

pug({
  locals: {
    // t: i18next.t // doesn't work
    t: i18next.t.bind(i18next) // works
  }
})

and then in templates

p= t('myKey')

@oskarrough if not using req.t from i18next-express-middleware i just can warn you that this won't work - you will end in race conditions as soon async stuff gets performed ending in mixed languages as you use i18next.t which is a singleton and not safe to use during requests....

https://github.com/i18next/i18next-express-middleware/blob/master/src/index.js#L13

@jamuhl thanks for the warning.

In my case, I'm not using Express rather building some static templates where the language can not be changed dynamically so I assume it should be ok here :)

Was this page helpful?
0 / 5 - 0 ratings