Skip to content

Commit

Permalink
Merge pull request #14 from cedk/subtags
Browse files Browse the repository at this point in the history
Add support for language with subtags
  • Loading branch information
guillaumepotier authored Nov 5, 2018
2 parents 1d19293 + 04c26e9 commit 0f4d560
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: node_js

node_js:
- 0.11
- "node"

script:
- npm run-script test
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# gettext.js changelog

- add support for language with subtags

**[0.5.2]**

- fixed bugs for plurals. (#2)
Expand Down
41 changes: 31 additions & 10 deletions lib/gettext.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@
});
};

var expand_locale = function(locale) {
var locales = [locale],
i = locale.lastIndexOf('-');
while (i > 0) {
locale = locale.slice(0, i);
locales.push(locale);
i = locale.lastIndexOf('-');
}
return locales;
};

var getPluralFunc = function (plural_form) {
// Plural form string regexp
// taken from https://github.com/Orange-OpenSource/gettext.js/blob/master/lib.gettext.js
Expand Down Expand Up @@ -99,6 +110,7 @@

return {
strfmt: strfmt, // expose strfmt util
expand_locale: expand_locale, // expose expand_locale util

// Declare shortcuts
__: function () { return this.gettext.apply(this, arguments); },
Expand Down Expand Up @@ -167,22 +179,31 @@
translation,
options = {},
key = msgctxt ? msgctxt + _ctxt_delimiter + msgid : msgid,
exist = _dictionary[domain] && _dictionary[domain][_locale] && _dictionary[domain][_locale][key];

// because it's not possible to define both a singular and a plural form of the same msgid,
// we need to check that the stored form is the same as the expected one.
// if not, we'll just ignore the translation and consider it as not translated.
if (msgid_plural) {
exist = exist && "string" !== typeof _dictionary[domain][_locale][key];
} else {
exist = exist && "string" === typeof _dictionary[domain][_locale][key];
exist,
locale;
var locales = expand_locale(_locale);
for (var i in locales) {
locale = locales[i];
exist = _dictionary[domain] && _dictionary[domain][locale] && _dictionary[domain][locale][key];

// because it's not possible to define both a singular and a plural form of the same msgid,
// we need to check that the stored form is the same as the expected one.
// if not, we'll just ignore the translation and consider it as not translated.
if (msgid_plural) {
exist = exist && "string" !== typeof _dictionary[domain][locale][key];
} else {
exist = exist && "string" === typeof _dictionary[domain][locale][key];
}
if (exist) {
break;
}
}

if (!exist) {
translation = msgid;
options.plural_func = defaults.plural_func;
} else {
translation = _dictionary[domain][_locale][key];
translation = _dictionary[domain][locale][key];
}

// Singular form
Expand Down
30 changes: 30 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@
});
});

describe('expand_locale', function() {
it('should be a i18n method', function() {
expect(i18n.expand_locale).to.be.a('function');
});
it('should handle simple locale', function() {
expect(i18n.expand_locale('fr')).to.eql(['fr']);
});
it('should handle complex locale', function() {
expect(i18n.expand_locale('de-CH-1996')).to.eql(['de-CH-1996', 'de-CH', 'de']);
});
});

describe('gettext', function () {
it('should handle peacefully singular untranslated keys', function () {
expect(i18n.gettext('not translated')).to.be('not translated');
Expand All @@ -60,6 +72,24 @@
expect(i18n.gettext('not %1 translated', 'correctly')).to.be('not correctly translated');
expect(i18n.gettext('not %1 %2 translated', 'fully', 'correctly')).to.be('not fully correctly translated');
});
it('should fallback to father language', function() {
i18n = new window.i18n();
i18n.setMessages('messages', 'fr', {
"Mop": "Serpillière",
});
i18n.setMessages('messages', 'fr-BE', {
"Mop": "Torchon",
});

i18n.setLocale('fr-BE');
expect(i18n.gettext("Mop")).to.be("Torchon");

i18n.setLocale('fr');
expect(i18n.gettext("Mop")).to.be("Serpillière");

i18n.setLocale('fr-FR');
expect(i18n.gettext("Mop")).to.be("Serpillière");
});
});

describe('ngettext', function () {
Expand Down

0 comments on commit 0f4d560

Please sign in to comment.