chore(localize): add throwing an exception for missing namespace

This commit is contained in:
Pavlo Kozlov 2019-05-31 10:19:53 +02:00 committed by Joren Broekema
parent 01d0cde646
commit bc8a36d36f
3 changed files with 971 additions and 1054 deletions

View file

@ -218,6 +218,11 @@ export class LocalizeManager extends LionSingleton {
} }
_getMessageForKey(key, locale) { _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 [ns, namesString] = key.split(':');
const namespaces = this.__storage[locale]; const namespaces = this.__storage[locale];
const messages = namespaces ? namespaces[ns] : null; const messages = namespaces ? namespaces[ns] : null;

View file

@ -405,5 +405,14 @@ describe('LocalizeManager', () => {
manager.addData('en-GB', 'overridden-ns', { greeting: 'Hello!' }); manager.addData('en-GB', 'overridden-ns', { greeting: 'Hello!' });
expect(manager.msg(keys)).to.equal('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".`,
);
});
}); });
}); });

2011
yarn.lock

File diff suppressed because it is too large Load diff