jeresig / i18n-node-2

Lightweight simple translation module for node.js / express.js with dynamic json storage. Uses common __('...') syntax in app and templates.
MIT License
507 stars 79 forks source link

Issue with getting value from i18n.__("Hello") #119

Open nijat13 opened 6 years ago

nijat13 commented 6 years ago

Hello! I've tried to use this lib in my own project. But basic configuration doesn't work for me. Here's the language files: locales/en.json

{
    "Hello": "Hello EN",
    ....
}

locales/de.json

{
    "Hello": "Hello DE",
    ....
}

Here's the express.js:


var i18n = new (require('i18n-2'))({
    // setup some locales - other locales default to the first locale
    locales: ['en', 'de'],
    extension: '.json',
    directory: path.join(__dirname, '/locales')
});

// Use it however you wish
console.log( 'i18n-2 Hello value: ', i18n.__("Hello") );

This console gives thee Hello instead of Hello EN.

Additionally, my i18n object looks like this:

{ devMode: true,
locales: { en: {}, de: {} },
extension: '.json',
directory: '/backend-app/dist/config/locales',
defaultLocale: 'en',
locale: 'en' }

Thanks in advance!

iKonrad commented 5 years ago

@nijat13 I've got exactly same issue. Have you managed to find out what the issue was?

iKonrad commented 5 years ago

Okay, in my case it was the missing defaultLocale setting.

But in your case (I can see that you've submitted this a few months ago but maybe someone will have a similar issue), I think you need to add a parse function that will transform your json into a JS object.

Mine looks like this:

      parse(data) {
        const parsed = JSON.parse(data.toString());
        return parsed;
      },
rmartinez-alfredsmart commented 5 years ago

Hi, I do have the same problem, any solution?

Thanks.