mashpie / i18n-node

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

Problems with local files #510

Closed akzi11 closed 1 year ago

akzi11 commented 1 year ago

I have a local folder, with two .json files (en, fr). In another file (.ts) I have: const { I18n } = require('i18n'); var i18n = new I18n( { locales: ['en', 'fr'], defaultLocale: 'fr', directory: 'locales',)} When I try i18n.getCatalog(), it return : { Hello: 'Hello', Hi: 'Hi' } But my fr.json contain : {"Hello": "Salut"} When i try i18n.__("Hello"), it return: "Hello"

How to acces to the json files?

mashpie commented 1 year ago

either set locale explicitly:

https://github.com/mashpie/i18n-node#i18nsetlocale

or by included middleware:

https://github.com/mashpie/i18n-node#middleware-in-expressjs

mashpie commented 1 year ago

...what I get from your setup, you should check the simple example https://github.com/mashpie/i18n-node/blob/master/examples/singleton/index.js

akzi11 commented 1 year ago

Thanks for your answer. Even with setty locale explicity, __ return the key. Here the complete code:

   const { I18n } = require('i18n');
    const path = require('path')
    this.i18n = new I18n( {
        locales: ['en', 'fr'],
        defaultLocale: 'fr',
        directory: path.join(__dirname, 'locales'),
        logWarnFn: function (msg) {
            console.log('warn', msg)
        },
        logErrorFn: function (msg) {
            console.log('error', msg)
         },
    })
    this.i18n.setLocale('en');
    const allo = this.i18n.__("Hello")
    console.debug(allo)