I18next: Empty string causes fallback

Created on 15 Feb 2013  路  7Comments  路  Source: i18next/i18next

It appears having the value for a key as an empty string causes the fallback value to be returned instead.

Value returned is from the fallbackLng locale instead of the specified.
Data is loaded as JSON from resGetPath, containing key/values such as:
"info":"",

Expected outcome is only for fallback to occur when the key is actually missing.
If the key has any value, it is expected to be returned (including for empty string).

Most helpful comment

did you try setting: returnEmptyString: false on init? that should do the trick as it does not accept empty string as valid translation.

All 7 comments

Version used: 1.5.10

I see issue 49 should have solved allowing empty string as a valid translation value (comparing value against null instead of falsy check).

Except that my test case is more like:

var resStore = {
dev: { translation: { empty: 'Default text' } },
en: { translation: { } },
'en-US': { translation: { empty: '' } }
};

// For 'en-US'
expect(i18n.t('empty')).to.be('');
// For 'en'
expect(i18n.t('empty')).to.be('Default text'');

changes are already committed https://github.com/jamuhl/i18next/commit/b6bbeef1e3d70e6e80986e4f307a903f95528e37 and will be added to next release (comes end of March/April i think)

Awesome, I tested it successfully, closing

What is your opinion about adding an option to init() for enabling/disabling fallback to key if it exists in the translation, but corresponds to an empty string?

In my case this "feature" is more like a bug - I'm using i18next-parse to extract all strings to JSON and i18next-gettext-convert to convert between JSON and .po files and in my case empty string means a missing translation in which case I need a fallback to key.

If nobody has the time to deal with that but you like the idea, I'll try to do it and open a PR. Any suggestions are more than welcome.

did you try setting: returnEmptyString: false on init? that should do the trick as it does not accept empty string as valid translation.

Crap. I've slept over this one in the docs. Sorry for the disturbance, it works like a charm.

@isld no problem...there are tons of options and ways to use i18next - easy to miss one.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

slim-hmidi picture slim-hmidi  路  9Comments

alakdam07 picture alakdam07  路  6Comments

karol-majewski picture karol-majewski  路  6Comments

LodewijkSioen picture LodewijkSioen  路  3Comments

salvador-dali picture salvador-dali  路  7Comments