i18n-node icon indicating copy to clipboard operation
i18n-node copied to clipboard

Module falls back on key when no translation exists.

Open Sven65 opened this issue 8 years ago • 3 comments

Creating two files,

locales/en-us.json

{
	"hello": "Hello!",
	"user.name": "Username"
}

locales/sv-se.json

{
	"hello": "Hej!"
}

Running these in a file like this one;

const i18n = require("i18n");

i18n.configure({
	directory: __dirname+'/locales',
	defaultLocale: 'en-us',
	autoReload: true,
	updateFiles: false,
	fallbacks: {'sv-se': 'en-us'},
	retryInDefaultLocale: true
})

console.log(i18n.__('hello'))
console.log(i18n.__('user.name'))

i18n.setLocale('sv-se')
console.log(i18n.__h('user.name'))

console.log(i18n.__('hello'))
console.log(i18n.__('user.name'))

I get the output

Hello!
Username
[ { 'en-us': 'Username' }, { 'sv-se': 'user.name' } ]
Hej!
user.name

As can be seen, the last string is outputted as the translation key, despite having set the fallbacks key in the configuration.

Having a global fallback would also be helpful.

Sven65 avatar Nov 30 '17 18:11 Sven65

I'm having the same issue. It seems like the fallback settings are used when setting the locale to an unsupported one. However, I would expect the fallback to be used for searching an alternative translation when a key is missing from the active locale.

ahallez avatar Feb 13 '21 22:02 ahallez

@ahallez "I would expect the fallback to be used for searching an alternative translation"

I thought the same. There are a few issues where people have implemented this by rewriting the catalogs:

  • #387
  • #423
  • #439

axelpale avatar Dec 02 '21 10:12 axelpale

defaults as intended: https://github.com/mashpie/i18n-node/blob/master/test/i18n.missingPhrases.js#L27

hook for customization: https://github.com/mashpie/i18n-node/blob/master/test/i18n.missingPhrases.js#L42

mashpie avatar Dec 02 '21 11:12 mashpie