Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render in nodejs not working #231

Open
cojok opened this issue Dec 13, 2018 · 3 comments
Open

Render in nodejs not working #231

cojok opened this issue Dec 13, 2018 · 3 comments

Comments

@cojok
Copy link

cojok commented Dec 13, 2018

Hi there,

This looks like a great i18n lib. I have some issues regarding a nodejs script. I need to parse a ejs template and I am trying to integrate this lib into the project.
This is how my code looks like.

var path = require('path');
var ejs = require('ejs');

var i18next = require("i18next");
var Backend = require("i18next-node-fs-backend");

var i18nextOptions = {
    debug: true,
    load: ['en'],
    preload: ['en'],
    fallbackLng: 'en',
    backend: {
        loadPath: path.join(__dirname,'/locales/{{lng}}/{{ns}}.json'),
        addPath: path.join(__dirname,'/locales/{{lng}}/{{ns}}.missing.json')
    },
    getAsync:false,
    postProcess: true,
    saveMissing: true,
    saveMissingTo: path.join(__dirname,'/locales/en/missing.json')
}

i18next.use(Backend).init(i18nextOptions, function (err, result) {
    return result
})

var t = function(key, count) {
    if(count) {
        return i18next.t(key, {count: count})
    }
    return i18next.t(key);
};

var tpl = ejs.renderFile(path.join(__dirname, '/views/index.ejs'), {t:t, title: 'Title', message: 'This is a message'})

console.log(tpl)

Instead of getting the translations, I get only the keys. I have tried to do this:
console.log(i18next.t('key'))
but with no success. It still prints the key and not the value.

this is the output:

'<!DOCTYPE html>
<html>
   <head>
      <title>test ejs with i18next </title>
   </head>
  <body>
      <div id="greeting">
            Title   This is a message  hello   You_message
       </div>
    </body>
</html>'

where hello is a key name and You_message is also a key name.

Any suggestions? Thanks in advance.

Best,
cojok

PS: I forgot to put in the output of the debugger:

i18next::translator: missingKey undefined translation hello hello
i18next::translator: missingKey undefined translation You_message You_message
Promise {
  '<!DOCTYPE html>\n<html>\n<head>\n    <title>client side ejs compiled with express middleware</title>\n</head>\n<body>\n<div id="greeting">\n    Title\n    This is a message\n    hello\n    You_message\n</div>\n</body>\n</html>\n' }
i18next::backendConnector: loaded namespace translation for language en { hello: 'Hello',
  You_message: 'You have {{ count }} message',
  You_message_plural: 'You have {{ count }} messages' }
i18next: initialized { debug: true,
  initImmediate: true,
  ns: [ 'translation' ],
  defaultNS: [ 'translation' ],
  fallbackLng: [ 'en' ],
  fallbackNS: false,
  whitelist: false,
  nonExplicitWhitelist: false,
  load: [ 'en' ],
  preload: [ 'en' ],
  simplifyPluralSuffix: true,
  keySeparator: '.',
  nsSeparator: ':',
  pluralSeparator: '_',
  contextSeparator: '_',
  partialBundledLanguages: false,
  saveMissing: true,
  updateMissing: false,
  saveMissingTo:
   '/Users/cjk/Sites/test-tpl-engine-nodejs/locales/en/missing.json',
  saveMissingPlurals: true,
  missingKeyHandler: false,
  missingInterpolationHandler: false,
  postProcess: true,
  returnNull: true,
  returnEmptyString: true,
  returnObjects: false,
  joinArrays: false,
  returnedObjectHandler: [Function: returnedObjectHandler],
  parseMissingKeyHandler: false,
  appendNamespaceToMissingKey: false,
  appendNamespaceToCIMode: false,
  overloadTranslationOptionHandler: [Function: handle],
  interpolation:
   { escapeValue: true,
     format: [Function: format],
     prefix: '{{',
     suffix: '}}',
     formatSeparator: ',',
     unescapePrefix: '-',
     nestingPrefix: '$t(',
     nestingSuffix: ')',
     maxReplaces: 1000 },
  backend:
   { loadPath:
      '/Users/cjk/Sites/test-tpl-engine-nodejs/locales/{{lng}}/{{ns}}.json',
     addPath:
      '/Users/cjk/Sites/test-tpl-engine-nodejs/locales/{{lng}}/{{ns}}.missing.json' },
  getAsync: false }
@jamuhl
Copy link
Member

jamuhl commented Dec 13, 2018

i18next::translator: missingKey undefined translation hello hello
i18next::translator: missingKey undefined translation You_message You_message

--> undefined is the language

init with lng: 'en' and check if it works -> after that have a look in eg. i18next-express-middleware and the language detector plugin there

@cojok
Copy link
Author

cojok commented Dec 13, 2018

Checked with lng: 'en' and it gives back the same error.
I am not using expressjs at all. This nodejs script will be part of a lambda function. I will have a look on the i18next-express-middleware language detector.
Funny is that my code works inside an expressjs app.

Thanks.

@jamuhl
Copy link
Member

jamuhl commented Dec 13, 2018

In this case i guess - and should have be seen that before:

missing comes before backendConnector:loaded

You're calling t function before the async loading of those files are done -> move render into the i18next.init callback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants