Skip to content
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

Improve fallback #236

Closed
wants to merge 2 commits into from
Closed
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 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
16 changes: 14 additions & 2 deletions dist/angular-gettext.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ angular.module('gettext').factory('gettextCatalog', ["gettextPlurals", "$http",
$rootScope.$broadcast('gettextLanguageChanged');
}

function baseLanguage() {
if (catalog.currentLanguage) {
var parts = catalog.currentLanguage.split('_');
if (parts.length > 0) {
return parts[0];
}
return catalog.currentLanguage;
}
return null;
}

catalog = {
debug: false,
debugPrefix: '[MISSING]: ',
Expand Down Expand Up @@ -93,7 +104,7 @@ angular.module('gettext').factory('gettextCatalog', ["gettextPlurals", "$http",
},

getStringForm: function (string, n, context) {
var stringTable = this.strings[this.currentLanguage] || {};
var stringTable = this.strings[this.currentLanguage] || this.strings[baseLanguage()] || {};
var contexts = stringTable[string] || {};
var plurals = contexts[context || noContext] || [];
return plurals[n];
Expand All @@ -106,7 +117,8 @@ angular.module('gettext').factory('gettextCatalog', ["gettextPlurals", "$http",
},

getPlural: function (n, string, stringPlural, scope, context) {
var form = gettextPlurals(this.currentLanguage, n);
var baseLang = baseLanguage();
var form = gettextPlurals(this.currentLanguage !== 'pt_BR' ? baseLang : this.currentLanguage, n);
string = this.getStringForm(string, form, context) || prefixDebug(n === 1 ? string : stringPlural);
if (scope) {
scope.$count = n;
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.

16 changes: 14 additions & 2 deletions src/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, $h
$rootScope.$broadcast('gettextLanguageChanged');
}

function baseLanguage() {
if (catalog.currentLanguage) {
var parts = catalog.currentLanguage.split('_');
if (parts.length > 0) {
return parts[0];
}
return catalog.currentLanguage;
}
return null;
}

catalog = {
debug: false,
debugPrefix: '[MISSING]: ',
Expand Down Expand Up @@ -81,7 +92,7 @@ angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, $h
},

getStringForm: function (string, n, context) {
var stringTable = this.strings[this.currentLanguage] || {};
var stringTable = this.strings[this.currentLanguage] || this.strings[baseLanguage()] || {};
var contexts = stringTable[string] || {};
var plurals = contexts[context || noContext] || [];
return plurals[n];
Expand All @@ -94,7 +105,8 @@ angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, $h
},

getPlural: function (n, string, stringPlural, scope, context) {
var form = gettextPlurals(this.currentLanguage, n);
var baseLang = baseLanguage();
var form = gettextPlurals(this.currentLanguage !== 'pt_BR' ? baseLang : this.currentLanguage, n);
string = this.getStringForm(string, form, context) || prefixDebug(n === 1 ? string : stringPlural);
if (scope) {
scope.$count = n;
Expand Down
9 changes: 9 additions & 0 deletions test/unit/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,13 @@ describe("Catalog", function () {
assert.equal(catalog.getString("Archive", {}, "verb"), "Archiveren");
assert.equal(catalog.getString("Archive", {}, "noun"), "Archief");
});

it("Should return string from fallback language", function () {
var strings = { Hello: "Hallo" };
catalog.setStrings("nl", strings);
catalog.setCurrentLanguage("nl_NL");
// It should check strings for nl_NL, then strings for nl
assert.equal(catalog.getString("Bye"), "Bye");
assert.equal(catalog.getString("Hello"), "Hallo");
});
});