Skip to content

Commit

Permalink
Added a method to register a single locale
Browse files Browse the repository at this point in the history
  • Loading branch information
adrenalin committed Mar 19, 2021
1 parent 1504f42 commit 858afd3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
38 changes: 23 additions & 15 deletions lib/Localization.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,30 +134,38 @@ module.exports = class Localization {
/**
* Register locales
*
* @param { object } data Locales to be registered
* @param { string } locale Locale key
* @param { object } translations Translations
* @return { Localization } This instance
*/
static registerLocales (data) {
if (!isObject(data)) {
throw new Error('registerLocales requires an object as its argument')
static registerLocale (locale, translations) {
if (!isObject(translations)) {
throw new Error('Invalid locale, each node has to be an object')
}

// Validate locale
for (const key in data) {
const value = data[key]
if (!isObject(value)) {
throw new Error('Invalid locale, each node has to be an object')
for (const lang in translations) {
if (typeof translations[lang] !== 'string') {
throw new Error('Invalid locale, each language node has to be a string')
}
}

for (const lang in value) {
if (typeof value[lang] !== 'string') {
throw new Error('Invalid locale, each language node has to be a string')
}
}
locales[locale] = translations
}

/**
* Register locales
*
* @param { object } data Locales to register
* @return { Localization } This instance
*/
static registerLocales (data) {
if (!isObject(data)) {
throw new Error('registerLocales requires an object as its argument')
}

// Validate locale
for (const key in data) {
locales[key] = data[key]
Localization.registerLocale(key, data[key])
}

return this
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "helpers.js",
"version": "0.18.2",
"version": "0.18.3",
"description": "Miscellaneous helpers",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 858afd3

Please sign in to comment.