Skip to content

Commit dfa89d0

Browse files
committed
Improve fallback
Closes #210
1 parent 2da6265 commit dfa89d0

File tree

5 files changed

+31
-3
lines changed

5 files changed

+31
-3
lines changed

Gruntfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ module.exports = function (grunt) {
8282
hostname: "0.0.0.0",
8383
middleware: function (connect) {
8484
return [
85+
// jscs:disable requireDotNotation
8586
connect["static"](__dirname)
8687
];
8788
}

dist/angular-gettext.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ angular.module('gettext').factory('gettextCatalog', ["gettextPlurals", "$http",
4141
$rootScope.$broadcast('gettextLanguageChanged');
4242
}
4343

44+
function fallbackLanguage(lang) {
45+
var parts = (lang || '').split('_');
46+
if (parts.length > 1) {
47+
return parts[0];
48+
}
49+
return null;
50+
}
51+
4452
catalog = {
4553
debug: false,
4654
debugPrefix: '[MISSING]: ',
@@ -93,7 +101,8 @@ angular.module('gettext').factory('gettextCatalog', ["gettextPlurals", "$http",
93101
},
94102

95103
getStringForm: function (string, n, context) {
96-
var stringTable = this.strings[this.currentLanguage] || {};
104+
var fallback = fallbackLanguage(this.currentLanguage);
105+
var stringTable = this.strings[this.currentLanguage] || this.strings[fallback] || {};
97106
var contexts = stringTable[string] || {};
98107
var plurals = contexts[context || noContext] || [];
99108
return plurals[n];

dist/angular-gettext.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/catalog.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, $h
2929
$rootScope.$broadcast('gettextLanguageChanged');
3030
}
3131

32+
function fallbackLanguage(lang) {
33+
var parts = (lang || '').split('_');
34+
if (parts.length > 1) {
35+
return parts[0];
36+
}
37+
return null;
38+
}
39+
3240
catalog = {
3341
debug: false,
3442
debugPrefix: '[MISSING]: ',
@@ -81,7 +89,8 @@ angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, $h
8189
},
8290

8391
getStringForm: function (string, n, context) {
84-
var stringTable = this.strings[this.currentLanguage] || {};
92+
var fallback = fallbackLanguage(this.currentLanguage);
93+
var stringTable = this.strings[this.currentLanguage] || this.strings[fallback] || {};
8594
var contexts = stringTable[string] || {};
8695
var plurals = contexts[context || noContext] || [];
8796
return plurals[n];

test/unit/catalog.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,13 @@ describe("Catalog", function () {
145145
assert.equal(catalog.getString("Archive", {}, "verb"), "Archiveren");
146146
assert.equal(catalog.getString("Archive", {}, "noun"), "Archief");
147147
});
148+
149+
it("Should return string from fallback language", function () {
150+
var strings = { Hello: "Hallo" };
151+
catalog.setStrings("nl", strings);
152+
catalog.setCurrentLanguage("nl_NL");
153+
// It should check strings for nl_NL, then strings for nl
154+
assert.equal(catalog.getString("Bye"), "Bye");
155+
assert.equal(catalog.getString("Hello"), "Hallo");
156+
});
148157
});

0 commit comments

Comments
 (0)