Comparing version 3.0.0 to 3.1.0
25
index.js
@@ -11,2 +11,3 @@ var fs = require('fs') | ||
this.locale = opts.locale || 'en' | ||
this.fallbackToLanguage = typeof opts.fallbackToLanguage === 'boolean' ? opts.fallbackToLanguage : true | ||
@@ -57,3 +58,3 @@ // internal stuff. | ||
var languageFile = path.resolve(directory, './', locale + '.json') | ||
var languageFile = this._resolveLocaleFile(directory, locale) | ||
var serializedLocale = JSON.stringify(this.cache[locale], null, 2) | ||
@@ -70,3 +71,3 @@ | ||
var localeLookup = {} | ||
var languageFile = path.resolve(this.directory, './', this.locale + '.json') | ||
var languageFile = this._resolveLocaleFile(this.directory, this.locale) | ||
@@ -87,2 +88,22 @@ try { | ||
Y18N.prototype._resolveLocaleFile = function (directory, locale) { | ||
var file = path.resolve(directory, './', locale + '.json') | ||
if (this.fallbackToLanguage && !this._fileExistsSync(file) && ~locale.lastIndexOf('_')) { | ||
// attempt fallback to language only | ||
var languageFile = path.resolve(directory, './', locale.split('_')[0] + '.json') | ||
if (this._fileExistsSync(languageFile)) file = languageFile | ||
} | ||
return file | ||
} | ||
// this only exists because fs.existsSync() "will be deprecated" | ||
// see https://nodejs.org/api/fs.html#fs_fs_existssync_path | ||
Y18N.prototype._fileExistsSync = function (file) { | ||
try { | ||
return fs.statSync(file).isFile() | ||
} catch (err) { | ||
return false | ||
} | ||
} | ||
Y18N.prototype.__n = function () { | ||
@@ -89,0 +110,0 @@ var args = Array.prototype.slice.call(arguments) |
{ | ||
"name": "y18n", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "the bare-bones internationalization library used by yargs", | ||
@@ -28,8 +28,8 @@ "main": "index.js", | ||
"chai": "^3.2.0", | ||
"coveralls": "^2.11.3", | ||
"coveralls": "^2.11.4", | ||
"mocha": "^2.2.5", | ||
"nyc": "^3.0.1", | ||
"nyc": "^3.1.0", | ||
"rimraf": "^2.4.2", | ||
"standard": "^4.5.4" | ||
"standard": "^5.1.0" | ||
} | ||
} |
@@ -31,3 +31,3 @@ # y18n | ||
console.log(__('one fish %s', '%d fishes %s', 2, 'foo')) | ||
console.log(__n('one fish %s', '%d fishes %s', 2, 'foo')) | ||
``` | ||
@@ -56,2 +56,5 @@ | ||
* `locale`: what locale should be used. | ||
* `fallbackToLanguage`: should fallback to a language-only file (e.g. `en.json`) | ||
be allowed if a file matching the locale does not exist (e.g. `en_US.json`), | ||
default `true`. | ||
@@ -71,3 +74,3 @@ ### y18n.\_\_(str, arg, arg, arg) | ||
### y18n.getLocale(locale) | ||
### y18n.getLocale() | ||
@@ -74,0 +77,0 @@ What locale is currently being used? |
@@ -49,2 +49,32 @@ /* global describe, it, after, beforeEach */ | ||
it('uses language file if language_territory file does not exist', function () { | ||
var __ = y18n({ | ||
locale: 'pirate_JM', | ||
directory: __dirname + '/locales' | ||
}).__ | ||
__('Hello').should.equal('Avast ye mateys!') | ||
}) | ||
it('does not fallback to language file if fallbackToLanguage is false', function () { | ||
var __ = y18n({ | ||
locale: 'pirate_JM', | ||
fallbackToLanguage: false, | ||
updateFiles: false, | ||
directory: __dirname + '/locales' | ||
}).__ | ||
__('Hello').should.equal('Hello') | ||
}) | ||
it('uses strings as given if no matching locale files found', function () { | ||
var __ = y18n({ | ||
locale: 'zz_ZZ', | ||
updateFiles: false, | ||
directory: __dirname + '/locales' | ||
}).__ | ||
__('Hello').should.equal('Hello') | ||
}) | ||
it('expands arguments into %s placeholders', function () { | ||
@@ -60,3 +90,3 @@ var __ = y18n({ | ||
beforeEach(function (done) { | ||
rimraf('./test/locales/fr.json', function () { | ||
rimraf('./test/locales/fr*.json', function () { | ||
return done() | ||
@@ -77,3 +107,3 @@ }) | ||
var __ = y18n({ | ||
locale: 'fr', | ||
locale: 'fr_FR', | ||
directory: __dirname + '/locales' | ||
@@ -83,2 +113,18 @@ }).__ | ||
__('banana', function (err) { | ||
var locale = JSON.parse(fs.readFileSync('./test/locales/fr_FR.json', 'utf-8')) | ||
locale.banana.should.equal('banana') | ||
return done(err) | ||
}) | ||
}) | ||
it('writes new word to language file if language_territory file does not exist', function (done) { | ||
fs.writeFileSync('./test/locales/fr.json', '{"meow": "le meow"}', 'utf-8') | ||
var __ = y18n({ | ||
locale: 'fr_FR', | ||
directory: __dirname + '/locales' | ||
}).__ | ||
__('meow').should.equal('le meow') | ||
__('banana', function (err) { | ||
var locale = JSON.parse(fs.readFileSync('./test/locales/fr.json', 'utf-8')) | ||
@@ -90,2 +136,26 @@ locale.banana.should.equal('banana') | ||
it('writes word to missing locale file, if no fallback takes place', function (done) { | ||
fs.writeFileSync('./test/locales/fr.json', '{"meow": "le meow"}', 'utf-8') | ||
var __ = y18n({ | ||
locale: 'fr_FR', | ||
fallbackToLanguage: false, | ||
directory: __dirname + '/locales' | ||
}).__ | ||
__('banana', function (err) { | ||
// 'banana' should be written to fr_FR.json | ||
var locale = JSON.parse(fs.readFileSync('./test/locales/fr_FR.json', 'utf-8')) | ||
locale.should.deep.equal({ | ||
banana: 'banana' | ||
}) | ||
// fr.json should remain untouched | ||
var frJson = JSON.parse(fs.readFileSync('./test/locales/fr.json', 'utf-8')) | ||
frJson.should.deep.equal({ | ||
meow: 'le meow' | ||
}) | ||
return done(err) | ||
}) | ||
}) | ||
it('handles enqueuing multiple writes at the same time', function (done) { | ||
@@ -92,0 +162,0 @@ var __ = y18n({ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18425
441
92
1