Skip to content

Commit

Permalink
Merge branch 'release/0.8.3' into npm
Browse files Browse the repository at this point in the history
  • Loading branch information
mashpie committed Jun 21, 2016
2 parents af360e1 + 575a7ea commit 523fa8e
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 13 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ No extra parsing needed.
[![NPM version][npm-image]][npm-url]
[![Dependency Status][dependency-image]][dependency-url]
[![Test Coverage][coveralls-image]][coveralls-url]

[![Known Vulnerabilities][snyk-image]][snyk-url]

## Install
```sh
Expand Down Expand Up @@ -946,6 +946,8 @@ i18n.configure({

## Changelog

* 0.8.3:
+ __fixed__: #235 objectNotation, #231 Plurals support regions ("en-US", "de-DE")
* 0.8.2:
* __fixed__: typos, objectNotation mutator #226, accept-language headers with fallback #228
* 0.8.1:
Expand Down Expand Up @@ -1014,3 +1016,6 @@ SOFTWARE.

[dependency-image]: https://img.shields.io/gemnasium/mashpie/i18n-node.svg
[dependency-url]: https://gemnasium.com/mashpie/i18n-node

[snyk-image]: https://snyk.io/test/npm/i18n/badge.svg
[snyk-url]: https://snyk.io/test/npm/i18n
20 changes: 12 additions & 8 deletions i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @link https://github.com/mashpie/i18n-node
* @license http://opensource.org/licenses/MIT
*
* @version 0.8.2
* @version 0.8.3
*/

'use strict';
Expand Down Expand Up @@ -66,7 +66,7 @@ module.exports = (function() {

i18n.locales = locales;

i18n.version = '0.8.2';
i18n.version = '0.8.3';

i18n.configure = function i18nConfigure(opt) {

Expand Down Expand Up @@ -363,7 +363,11 @@ module.exports = (function() {
if (PluralsForLocale[targetLocale]) {
p = PluralsForLocale[targetLocale];
} else {
p = new MakePlural(targetLocale);
// split locales with a region code
var lc = targetLocale.toLowerCase().split(/[_-\s]+/)
.filter(function(el){ return true && el; });
// take the first part of locale, fallback to full locale
p = new MakePlural(lc[0] || targetLocale);
PluralsForLocale[targetLocale] = p;
}

Expand Down Expand Up @@ -641,7 +645,7 @@ module.exports = (function() {

var guessLanguage = function(request) {
if (typeof request === 'object') {
var languageHeader = request.headers['accept-language'],
var languageHeader = request.headers? request.headers['accept-language'] : undefined,
languages = [],
regions = [];

Expand Down Expand Up @@ -921,8 +925,8 @@ module.exports = (function() {
if (!locales[locale]) return Function.prototype;

// Handle object lookup notation
var indexOfDot = objectNotation && singular.indexOf(objectNotation);
if (objectNotation && (0 < indexOfDot && indexOfDot < singular.length)) {
var indexOfDot = objectNotation && singular.lastIndexOf(objectNotation);
if (objectNotation && (0 < indexOfDot && indexOfDot < singular.length - 1)) {
// If delayed traversal wasn't specifically forbidden, it is allowed.
if (typeof allowDelayedTraversal === 'undefined') allowDelayedTraversal = true;
// The accessor we're trying to find and which we want to return.
Expand Down Expand Up @@ -987,8 +991,8 @@ module.exports = (function() {
if (!locales[locale]) return Function.prototype;

// Handle object lookup notation
var indexOfDot = objectNotation && singular.indexOf(objectNotation);
if (objectNotation && (0 < indexOfDot && indexOfDot < singular.length)) {
var indexOfDot = objectNotation && singular.lastIndexOf(objectNotation);
if (objectNotation && (0 < indexOfDot && indexOfDot < singular.length - 1)) {
// If branching wasn't specifically allowed, disable it.
if (typeof allowBranching === 'undefined') allowBranching = false;
// This will become the function we want to return.
Expand Down
6 changes: 5 additions & 1 deletion locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
"one": "plural",
"other": "plurals"
}
},
"path": {
"sub": "nested.path.sub"
}
},
"There is one monkey in the %%s": {
Expand Down Expand Up @@ -109,5 +112,6 @@
"ordered arguments with numbers": "%2$d then %1$s then %3$.2f",
"repeated argument": "%1$s, %1$s, %1$s",
". is first character": "Dot is first character",
"last character is .": "last character is Dot"
"last character is .": "last character is Dot",
"few sentences. with .": "few sentences with Dot"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "i18n",
"description": "lightweight translation module with dynamic json storage",
"version": "0.8.2",
"version": "0.8.3",
"homepage": "http://github.com/mashpie/i18n-node",
"repository": {
"type": "git",
Expand Down
10 changes: 10 additions & 0 deletions test/i18n.init.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ describe('i18n.init()', function() {
done();
});

it('should be possible to bind to non-request objects', function(done) {
var plain = new Object();
should.equal(i18n.init(plain), undefined);
should.equal(plain.locale, 'en');
should.equal(plain.__('Hello'), 'Hello');
should.equal(plain.setLocale('de'), 'de');
should.equal(plain.__('Hello'), 'Hallo');
done();
});

it('should be possible to bind public methods to foreign objects', function(done){
UnboundTestScope.translate = i18n.__;
should.equal(UnboundTestScope.translate('Hello'), 'Hallo');
Expand Down
34 changes: 33 additions & 1 deletion test/i18n.makePlurals.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,32 @@ function putJson(l, d) {
describe('i18n supports MakePlural', function() {

var TestScope = {};
var locales = ['en', 'de', 'fr', 'ru', 'ar'];
var locales = ['en', 'de', 'fr', 'ru', 'ar', 'de-DE', 'de-AT', 'de-CH'];
var fixture = {
de: {
"%s cat": {
one: '%d Katze',
other: '%d Katzen'
}
},
'de-DE': {
"%s cat": {
one: '%d Katze',
other: '%d Katzen'
}
},
'de-AT': {
"%s cat": {
one: '%d Katze',
other: '%d Katzen'
}
},
'de-CH': {
"%s cat": {
one: '%d Katze',
other: '%d Katzen'
}
},
en: {
"%s cat": {
one: '%d cat',
Expand Down Expand Up @@ -138,4 +156,18 @@ describe('i18n supports MakePlural', function() {
done();
});

it('__n() should return correctly in german for all regions', function(done) {
var regions = ['de-DE', 'de-AT', 'de-CH'];
for(var i = 0; i < regions.length; i++) {
TestScope.setLocale(regions[i]);
should.deepEqual(TestScope.__n('%s cat', 0), '0 Katzen');
should.deepEqual(TestScope.__n('%s cat', 1), '1 Katze');
should.deepEqual(TestScope.__n('%s cat', 2), '2 Katzen');
should.deepEqual(TestScope.__n('%s cat', 5), '5 Katzen');
should.deepEqual(TestScope.__n('%s cat', 6), '6 Katzen');
should.deepEqual(TestScope.__n('%s cat', 21), '21 Katzen');
}
done();
});

});
7 changes: 7 additions & 0 deletions test/i18n.objectnotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ describe('Object Notation', function() {
should.throws(__('greeting.placeholder.loud', 'Marcus'));
});

it('should return en translations as expected, when dot is first or last character', function () {
i18n.setLocale('en');
should.equal(__('. is first character'), 'Dot is first character');
should.equal(__('last character is .'), 'last character is Dot');
should.equal(__('few sentences. with .'), 'few sentences with Dot');
});

it('should provide proper pluralization support, using object traversal notation', function() {
i18n.setLocale('en');
var singular = __n({ singular: "cat", plural: "cat", locale: "de" }, 1);
Expand Down
2 changes: 1 addition & 1 deletion test/i18n.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ i18n.configure({

describe('Module Setup', function () {
it('should export a valid version', function () {
should.equal(i18n.version, '0.8.2');
should.equal(i18n.version, '0.8.3');
});

it('should export configure as i18nConfigure', function () {
Expand Down

0 comments on commit 523fa8e

Please sign in to comment.