Merge pull request #1557 from pndewit/feat/translation-fallback
feat(localize): add option to show the key when a locale is missing a translation
This commit is contained in:
commit
b902f9df50
3 changed files with 27 additions and 2 deletions
5
.changeset/fast-trees-prove.md
Normal file
5
.changeset/fast-trees-prove.md
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@lion/localize': minor
|
||||||
|
---
|
||||||
|
|
||||||
|
add option to show the key as a fallback when a locale is missing a translation
|
||||||
|
|
@ -14,13 +14,19 @@ import isLocalizeESModule from './isLocalizeESModule.js';
|
||||||
*/
|
*/
|
||||||
export class LocalizeManager {
|
export class LocalizeManager {
|
||||||
// eslint-disable-line no-unused-vars
|
// eslint-disable-line no-unused-vars
|
||||||
constructor({ autoLoadOnLocaleChange = false, fallbackLocale = '' } = {}) {
|
constructor({
|
||||||
|
autoLoadOnLocaleChange = false,
|
||||||
|
fallbackLocale = '',
|
||||||
|
showKeyAsFallback = false,
|
||||||
|
} = {}) {
|
||||||
/** @private */
|
/** @private */
|
||||||
this.__delegationTarget = document.createDocumentFragment();
|
this.__delegationTarget = document.createDocumentFragment();
|
||||||
/** @protected */
|
/** @protected */
|
||||||
this._autoLoadOnLocaleChange = !!autoLoadOnLocaleChange;
|
this._autoLoadOnLocaleChange = !!autoLoadOnLocaleChange;
|
||||||
/** @protected */
|
/** @protected */
|
||||||
this._fallbackLocale = fallbackLocale;
|
this._fallbackLocale = fallbackLocale;
|
||||||
|
/** @protected */
|
||||||
|
this._showKeyAsFallback = showKeyAsFallback;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type {Object.<string, Object.<string, Object>>}
|
* @type {Object.<string, Object.<string, Object>>}
|
||||||
|
|
@ -574,7 +580,7 @@ export class LocalizeManager {
|
||||||
messages,
|
messages,
|
||||||
);
|
);
|
||||||
|
|
||||||
return String(result || '');
|
return String(result || (this._showKeyAsFallback ? key : ''));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -677,6 +677,20 @@ describe('LocalizeManager', () => {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('show key as fallback', () => {
|
||||||
|
it('shows the key as a fallback when a translation cannot be found', () => {
|
||||||
|
manager = new LocalizeManager({ showKeyAsFallback: true });
|
||||||
|
manager.addData('en-GB', 'my-ns', { greeting: 'Hello!' });
|
||||||
|
expect(manager.msg('my-ns:unknownKey')).to.equal('my-ns:unknownKey');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('shows nothing when a translation cannot be found by default', () => {
|
||||||
|
manager = new LocalizeManager();
|
||||||
|
manager.addData('en-GB', 'my-ns', { greeting: 'Hello!' });
|
||||||
|
expect(manager.msg('my-ns:unknownKey')).to.equal('');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('When supporting external translation tools like Google Translate', () => {
|
describe('When supporting external translation tools like Google Translate', () => {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue