chore(localize): add throwing an exception for missing namespace
This commit is contained in:
parent
01d0cde646
commit
bc8a36d36f
3 changed files with 971 additions and 1054 deletions
|
|
@ -218,6 +218,11 @@ export class LocalizeManager extends LionSingleton {
|
|||
}
|
||||
|
||||
_getMessageForKey(key, locale) {
|
||||
if (key.indexOf(':') === -1) {
|
||||
throw new Error(
|
||||
`Namespace is missing in the key "${key}". The format for keys is "namespace:name".`,
|
||||
);
|
||||
}
|
||||
const [ns, namesString] = key.split(':');
|
||||
const namespaces = this.__storage[locale];
|
||||
const messages = namespaces ? namespaces[ns] : null;
|
||||
|
|
|
|||
|
|
@ -405,5 +405,14 @@ describe('LocalizeManager', () => {
|
|||
manager.addData('en-GB', 'overridden-ns', { greeting: 'Hello!' });
|
||||
expect(manager.msg(keys)).to.equal('Hello!');
|
||||
});
|
||||
|
||||
it('throws a custom error when namespace prefix is missing', () => {
|
||||
const manager = new LocalizeManager();
|
||||
const msgKey = 'greeting';
|
||||
manager.addData('en-GB', 'my-ns', { [msgKey]: 'Hello!' });
|
||||
expect(() => manager.msg(msgKey)).to.throw(
|
||||
`Namespace is missing in the key "${msgKey}". The format for keys is "namespace:name".`,
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue