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

__n() throws error if setLocale not called, despite defaultLocale #429

Open
ucacoin opened this issue Apr 3, 2020 · 4 comments
Open

__n() throws error if setLocale not called, despite defaultLocale #429

ucacoin opened this issue Apr 3, 2020 · 4 comments

Comments

@ucacoin
Copy link

ucacoin commented Apr 3, 2020

HI,

if you call __n() without setLocale beforehand it will throw:

var lc = targetLocale.toLowerCase().split(/[_-\s]+/)
                              ^

TypeError: Cannot read property 'toLowerCase' of undefined
    at Object.i18nTranslatePlural [as __n] (path/node_modules/i18n/i18n.js:371:31)
    at Module._compile (internal/modules/cjs/loader.js:1147:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:10)
    at Module.load (internal/modules/cjs/loader.js:996:32)
    at Function.Module._load (internal/modules/cjs/loader.js:896:14)
    at Module.require (internal/modules/cjs/loader.js:1036:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Module._compile (internal/modules/cjs/loader.js:1147:30)

Even if you define a default locale with:

i18n.configure({
    locales:['en', 'de'],
    directory: __dirname + '/../locales',
    defaultLocale: 'de',
});

There should be a fallback that if no targetLocale is found the defaultLocale should be used.

@mashpie
Copy link
Owner

mashpie commented Aug 19, 2020

Maybe you can add some code to reproduce your setup?

@latagore
Copy link

latagore commented May 15, 2021

I don't have a project setup, but I was able to reproduce the issue with

const path = require('path');
const {I18n} = require('i18n');
const i18n = new I18n({
    locales: ['en', 'es'],
    directory: path.join(__dirname, '..', '..', 'locales'),
});

let {__n} = i18n;
__n('%s cats', 1);

This can be fixed by doing

let {__n} = i18n;
__n = __n.bind(i18n);
__n('%s cats', 1);

edit: I missed this fix I had locally. seems that you must set the locale before using __n as well.

let {__n} = i18n;
i18n.setLocale('en');
__n = __n.bind(i18n);
__n('%s cats', 1);

@beytarovski
Copy link

Adding setLocale was enough for me just after initializing I18n.

const i18n = new I18n({ ... })
i18n.setLocale('en')

@mathmul
Copy link

mathmul commented Apr 8, 2024

Works for me without setLocale on minimal setup.

package.json

{
  "name": "tmp",
  "version": "1.0.0",
  "description": "",
  "type": "module",
  "main": "index.js",
  "scripts": {
    "test": "test"
  },
  "author": "me",
  "license": "ISC",
  "dependencies": {
    "i18n": "^0.15.1"
  }
}

index.js

import i18n from 'i18n';
import path from 'path';
import { fileURLToPath } from 'url'

const __dirname = path.dirname( fileURLToPath( import.meta.url ) )

i18n.configure( {
  locales: ['en', 'de'],
  fallbacks: { 'en-*': 'en', 'de-*': 'de' },
  defaultLocale: 'en',
  register: global,
  queryParameter: 'lang',
  directory: path.join( __dirname, 'locales' ),
} );

console.log( i18n.getLocale() )
console.log( i18n.__( '%s cat', 1 ) )

Outputs

*personal information redacted

user@machine tmp % node index.js
current locale: en
__   : 1 cat
user@machine tmp % npm list
tmp@1.0.0 /.../tmp
└── i18n@0.15.1

user@machine tmp % npm -v
9.8.1
user@machine tmp % nvm -v
0.39.1
user@machine tmp % node -v
v21.6.2

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

5 participants