Skip to content

Language fallbacks #250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Dec 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
/e2e-results.xml
/.idea
*.iml
.DS_Store
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ module.exports = function (grunt) {
hostname: "0.0.0.0",
middleware: function (connect) {
return [
// jscs:disable requireDotNotation
connect["static"](__dirname)
];
}
Expand Down
40 changes: 33 additions & 7 deletions dist/angular-gettext.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ angular.module('gettext').constant('gettext', function (str) {
return str;
});

angular.module('gettext').factory('gettextCatalog', ["gettextPlurals", "$http", "$cacheFactory", "$interpolate", "$rootScope", function (gettextPlurals, $http, $cacheFactory, $interpolate, $rootScope) {
angular.module('gettext').factory('gettextCatalog', ["gettextPlurals", "gettextFallbackLanguage", "$http", "$cacheFactory", "$interpolate", "$rootScope", function (gettextPlurals, gettextFallbackLanguage, $http, $cacheFactory, $interpolate, $rootScope) {
var catalog;
var noContext = '$$noContext';

Expand Down Expand Up @@ -92,22 +92,30 @@ angular.module('gettext').factory('gettextCatalog', ["gettextPlurals", "$http",
broadcastUpdated();
},

getStringForm: function (string, n, context) {
var stringTable = this.strings[this.currentLanguage] || {};
getStringFormFor: function (language, string, n, context) {
if (!language) {
return null;
}
var stringTable = this.strings[language] || {};
var contexts = stringTable[string] || {};
var plurals = contexts[context || noContext] || [];
return plurals[n];
return plurals[gettextPlurals(language, n)];
},

getString: function (string, scope, context) {
string = this.getStringForm(string, 0, context) || prefixDebug(string);
var fallbackLanguage = gettextFallbackLanguage(this.currentLanguage);
string = this.getStringFormFor(this.currentLanguage, string, 1, context) ||
this.getStringFormFor(fallbackLanguage, string, 1, context) ||
prefixDebug(string);
string = scope ? $interpolate(string)(scope) : string;
return addTranslatedMarkers(string);
},

getPlural: function (n, string, stringPlural, scope, context) {
var form = gettextPlurals(this.currentLanguage, n);
string = this.getStringForm(string, form, context) || prefixDebug(n === 1 ? string : stringPlural);
var fallbackLanguage = gettextFallbackLanguage(this.currentLanguage);
string = this.getStringFormFor(this.currentLanguage, string, n, context) ||
this.getStringFormFor(fallbackLanguage, string, n, context) ||
prefixDebug(n === 1 ? string : stringPlural);
if (scope) {
scope.$count = n;
string = $interpolate(string)(scope);
Expand Down Expand Up @@ -230,6 +238,24 @@ angular.module('gettext').directive('translate', ["gettextCatalog", "$parse", "$
};
}]);

angular.module("gettext").factory("gettextFallbackLanguage", function () {
var cache = {};
var pattern = /([^_]+)_[^_]+$/;

return function (langCode) {
if (cache[langCode]) {
return cache[langCode];
}

var matches = pattern.exec(langCode);
if (matches) {
cache[langCode] = matches[1];
return matches[1];
}

return null;
};
});
angular.module('gettext').filter('translate', ["gettextCatalog", function (gettextCatalog) {
function filter(input, context) {
return gettextCatalog.getString(input, null, context);
Expand Down
2 changes: 1 addition & 1 deletion dist/angular-gettext.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 15 additions & 7 deletions src/catalog.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, $http, $cacheFactory, $interpolate, $rootScope) {
angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, gettextFallbackLanguage, $http, $cacheFactory, $interpolate, $rootScope) {
var catalog;
var noContext = '$$noContext';

Expand Down Expand Up @@ -80,22 +80,30 @@ angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, $h
broadcastUpdated();
},

getStringForm: function (string, n, context) {
var stringTable = this.strings[this.currentLanguage] || {};
getStringFormFor: function (language, string, n, context) {
if (!language) {
return null;
}
var stringTable = this.strings[language] || {};
var contexts = stringTable[string] || {};
var plurals = contexts[context || noContext] || [];
return plurals[n];
return plurals[gettextPlurals(language, n)];
},

getString: function (string, scope, context) {
string = this.getStringForm(string, 0, context) || prefixDebug(string);
var fallbackLanguage = gettextFallbackLanguage(this.currentLanguage);
string = this.getStringFormFor(this.currentLanguage, string, 1, context) ||
this.getStringFormFor(fallbackLanguage, string, 1, context) ||
prefixDebug(string);
string = scope ? $interpolate(string)(scope) : string;
return addTranslatedMarkers(string);
},

getPlural: function (n, string, stringPlural, scope, context) {
var form = gettextPlurals(this.currentLanguage, n);
string = this.getStringForm(string, form, context) || prefixDebug(n === 1 ? string : stringPlural);
var fallbackLanguage = gettextFallbackLanguage(this.currentLanguage);
string = this.getStringFormFor(this.currentLanguage, string, n, context) ||
this.getStringFormFor(fallbackLanguage, string, n, context) ||
prefixDebug(n === 1 ? string : stringPlural);
if (scope) {
scope.$count = n;
string = $interpolate(string)(scope);
Expand Down
18 changes: 18 additions & 0 deletions src/fallback_language.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
angular.module("gettext").factory("gettextFallbackLanguage", function () {
var cache = {};
var pattern = /([^_]+)_[^_]+$/;

return function (langCode) {
if (cache[langCode]) {
return cache[langCode];
}

var matches = pattern.exec(langCode);
if (matches) {
cache[langCode] = matches[1];
return matches[1];
}

return null;
};
});
18 changes: 18 additions & 0 deletions test/unit/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,22 @@ describe("Catalog", function () {
assert.equal(catalog.getString("Archive", {}, "verb"), "Archiveren");
assert.equal(catalog.getString("Archive", {}, "noun"), "Archief");
});

it("Should return string from fallback language if current language has no translation", function () {
var strings = { Hello: "Hallo" };
catalog.setStrings("nl", strings);
catalog.setCurrentLanguage("nl_NL");
assert.equal(catalog.getString("Bye"), "Bye");
assert.equal(catalog.getString("Hello"), "Hallo");
});

it("Should not return string from fallback language if current language has translation", function () {
var stringsEn = { Baggage: "Baggage" };
var stringsEnGB = { Baggage: "Luggage" };
catalog.setStrings("en", stringsEn);
catalog.setStrings("en_GB", stringsEnGB);
catalog.setCurrentLanguage("en_GB");
assert.equal(catalog.getString("Bye"), "Bye");
assert.equal(catalog.getString("Baggage"), "Luggage");
});
});
22 changes: 22 additions & 0 deletions test/unit/fallback_language.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
describe("Fallback languages", function () {
var fallback = null;

beforeEach(module("gettext"));

beforeEach(inject(function (gettextFallbackLanguage) {
fallback = gettextFallbackLanguage;
}));

it("returns base language as fallback", function () {
assert.equal(fallback("en_GB"), "en");
});
it("returns null for simple language codes", function () {
assert.isNull(fallback("nl"), null);
});
it("returns null for null", function () {
assert.isNull(fallback(null));
});
it("returns consistent values", function () {
assert.equal(fallback("de_CH"), fallback("de_CH"));
});
});
2 changes: 1 addition & 1 deletion test/unit/plurals.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe("Plurals", function () {
it("Plural form of plural french is 1", function () {
assert.equal(plurals("fr", 2), 1);
});
it("Plural form of zero in french is 1", function () {
it("Plural form of zero in french is 0", function () {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch :-)

assert.equal(plurals("fr", 0), 0);
});
it("Plural form of 27 in arabic is 4", function () {
Expand Down