scinote-web/app/javascript/packs/locales/utils.js

16 lines
542 B
JavaScript
Raw Normal View History

2017-08-02 23:27:05 +08:00
// with this utility function we can write nested locales like an json object
export function flattenMessages(nestedMessages, prefix = '') {
return Object.keys(nestedMessages).reduce((messages, key) => {
let value = nestedMessages[key];
let prefixedKey = prefix ? `${prefix}.${key}` : key;
if (typeof value === 'string') {
messages[prefixedKey] = value;
} else {
Object.assign(messages, flattenMessages(value, prefixedKey));
}
return messages;
}, {});
}