@planet/localizer
Advanced tools
Comparing version 0.3.0 to 1.0.0
@@ -14,9 +14,9 @@ var Emitter = require('tiny-emitter'); | ||
if (!Array.isArray(config.supported)) { | ||
throw new Error('Missing "supported" array'); | ||
if (typeof(config.supported) !== 'object' || Array.isArray(config.supported)) { | ||
throw new Error('Missing "supported" object'); | ||
} | ||
/** | ||
* The list of supported locales. | ||
* @type {Array.<string>} | ||
* The lookup of supported locales (e.g. {en: 'English'}). | ||
* @type {Object>} | ||
*/ | ||
@@ -26,2 +26,12 @@ this.supported = config.supported; | ||
/** | ||
* The default locale. | ||
* @type {string>} | ||
*/ | ||
this.default = config.default; | ||
if (this.default && !(this.default in this.supported)) { | ||
throw new Error('The default locale must be one of the supported locales'); | ||
} | ||
/** | ||
* The current locale. | ||
@@ -60,3 +70,3 @@ * @type {string} | ||
var language = parts[0]; | ||
var supported = this.supported; | ||
var supported = Object.keys(this.supported); | ||
var fallback, locale; | ||
@@ -72,3 +82,3 @@ for (var i = 0, ii = supported.length; i < ii; ++i) { | ||
} | ||
return locale || fallback || supported[0]; | ||
return locale || fallback || this.default || supported[0]; | ||
}; | ||
@@ -102,3 +112,3 @@ | ||
this.current = locale; | ||
var formatter = new Formatter(data, locale, locale === this.supported[0]); | ||
var formatter = new Formatter(data, locale, locale === this.default); | ||
this.localize = function(key, values) { | ||
@@ -105,0 +115,0 @@ return formatter.format(key, values); |
{ | ||
"name": "@planet/localizer", | ||
"version": "0.3.0", | ||
"version": "1.0.0", | ||
"description": "A localization utility for fetching and formatting ICU messages", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -13,3 +13,3 @@ # `localizer` | ||
Configure your localizer with a URL template and a list of supported locales: | ||
Configure your localizer with a URL template and a lookup of supported locales: | ||
@@ -20,3 +20,4 @@ ```js | ||
url: 'http://example.com/{locale}.json', | ||
supported: ['en', 'es'] | ||
supported: {en: 'English', es: 'Español'}, | ||
default: 'en' | ||
}); | ||
@@ -77,4 +78,6 @@ ``` | ||
* `config.supported` - `Array.<string>` Required list of supported locales. The first listed locale is the default, and a resource file for this locale will be loaded after construction. | ||
* `config.supported` - `Object` Required lookup of supported locales. | ||
* `config.default` - `string` The default locale identifier. The resource file for this locale will be loaded at construction. If not provided, the first key in the `supported` lookup will be used as the default. | ||
**Methods** | ||
@@ -88,3 +91,3 @@ | ||
* `supported` - `Array.<string>` The list of supported locales provided to the constructor (read only). | ||
* `supported` - `Object` The lookup of supported locales provided to the constructor (read only). | ||
@@ -91,0 +94,0 @@ **Events** |
@@ -14,3 +14,3 @@ /* eslint-env mocha */ | ||
url: '/base/fixtures/{locale}.json', | ||
supported: ['en', 'zh'] | ||
supported: {en: 'English', zh: '中文'} | ||
}); | ||
@@ -22,2 +22,24 @@ | ||
it('allows configuration of default locale', function() { | ||
var localizer = new Localizer({ | ||
url: '/base/fixtures/{locale}.json', | ||
supported: {en: 'English', zh: '中文'}, | ||
default: 'en' | ||
}); | ||
assert.equal(localizer.default, 'en'); | ||
}); | ||
it('throws if "default" is not one of supported', function() { | ||
function call() { | ||
return new Localizer({ | ||
url: '/base/fixtures/{locale}.json', | ||
supported: {en: 'English', zh: '中文'}, | ||
default: 'foo' | ||
}); | ||
} | ||
assert.throws(call, Error, 'The default locale must be one of the supported locales'); | ||
}); | ||
it('throws if "supported" is not provided', function() { | ||
@@ -30,3 +52,3 @@ function call() { | ||
assert.throws(call, Error, 'Missing "supported" array'); | ||
assert.throws(call, Error, 'Missing "supported" object'); | ||
}); | ||
@@ -37,3 +59,3 @@ | ||
return new Localizer({ | ||
supported: ['en', 'zh'] | ||
supported: {en: 'English', zh: '中文'} | ||
}); | ||
@@ -52,3 +74,3 @@ } | ||
url: '/bad/path/{locale}.json', | ||
supported: ['en'] | ||
supported: {en: 'English'} | ||
}); | ||
@@ -66,3 +88,3 @@ | ||
url: '/base/fixtures/{locale}.not-json', | ||
supported: ['en'] | ||
supported: {en: 'English'} | ||
}); | ||
@@ -85,3 +107,3 @@ | ||
url: '/base/fixtures/{locale}.json', | ||
supported: ['en'] | ||
supported: {en: 'English'} | ||
}); | ||
@@ -105,3 +127,3 @@ | ||
url: '/base/fixtures/{locale}.json', | ||
supported: ['en', 'es'] | ||
supported: {en: 'English', es: 'Spanish'} | ||
}); | ||
@@ -130,3 +152,9 @@ | ||
url: '/base/fixtures/{locale}.json', | ||
supported: ['en', 'en-AU', 'es', 'es-AR'] | ||
supported: { | ||
en: 'English', | ||
'en-AU': 'English (Australia)', | ||
es: 'Spanish', | ||
'es-AR': 'Spanish (Argentina)' | ||
}, | ||
default: 'en' | ||
}); | ||
@@ -133,0 +161,0 @@ }); |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
22432
522
1
111