Skip to content

Commit

Permalink
Improve fallback
Browse files Browse the repository at this point in the history
Closes rubenv#210
  • Loading branch information
crissdev committed Oct 27, 2015
1 parent 4e7b81f commit d3625fb
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
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
11 changes: 10 additions & 1 deletion dist/angular-gettext.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ angular.module('gettext').factory('gettextCatalog', ["gettextPlurals", "$http",
$rootScope.$broadcast('gettextLanguageChanged');
}

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

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

getStringForm: function (string, n, context) {
var stringTable = this.strings[this.currentLanguage] || {};
var fallback = fallbackLanguage();
var stringTable = this.strings[this.currentLanguage] || (fallback ? this.strings[this.currentLanguage] : {});
var contexts = stringTable[string] || {};
var plurals = contexts[context || noContext] || [];
return plurals[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.

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

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

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

getStringForm: function (string, n, context) {
var stringTable = this.strings[this.currentLanguage] || {};
var fallback = fallbackLanguage();
var stringTable = this.strings[this.currentLanguage] || (fallback ? this.strings[this.currentLanguage] : {});
var contexts = stringTable[string] || {};
var plurals = contexts[context || noContext] || [];
return plurals[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");
});
});

0 comments on commit d3625fb

Please sign in to comment.