Fall back to en strings (fixes #738)

This commit is contained in:
John Firebaugh
2013-02-12 10:40:41 -08:00
parent 2062fe9a67
commit 502f35869c
4 changed files with 36 additions and 3 deletions

View File

@@ -6,9 +6,11 @@ locale.current = function(_) {
return locale;
};
function t(s, o) {
function t(s, o, loc) {
loc = loc || locale._current;
var path = s.split(".").reverse(),
rep = locale[locale._current];
rep = locale[loc];
while (rep !== undefined && path.length) rep = rep[path.pop()];
@@ -16,6 +18,9 @@ function t(s, o) {
if (o) for (var k in o) rep = rep.replace('{' + k + '}', o[k]);
return rep;
} else {
if (console) console.error('key ' + s + ' not found');
var missing = 'Missing translation: ' + s;
if (console) console.error(missing);
if (loc !== 'en') return t(s, o, 'en');
return missing;
}
}

View File

@@ -144,6 +144,7 @@
<!-- include spec files here... -->
<script src="spec/lib/d3.keybinding.js"></script>
<script src="spec/lib/locale.js"></script>
<script src="spec/actions/add_midpoint.js"></script>
<script src="spec/actions/add_entity.js"></script>

View File

@@ -22,6 +22,7 @@
<!-- include spec files here... -->
<script src="spec/lib/d3.keybinding.js"></script>
<script src="spec/lib/locale.js"></script>
<script src="spec/actions/add_midpoint.js"></script>
<script src="spec/actions/add_entity.js"></script>

26
test/spec/lib/locale.js Normal file
View File

@@ -0,0 +1,26 @@
describe("locale", function() {
var saved, error;
beforeEach(function() {
saved = locale;
error = console.error;
console.error = function () {};
locale = { _current: 'en', en: {test: 'test', foo: 'bar'}, __: {}}
});
afterEach(function() {
locale = saved;
console.error = error;
});
describe("t", function() {
it("defaults to locale._current", function() {
expect(t('test')).to.equal('test');
});
it("falls back to en", function() {
locale._current = '__';
expect(t('test')).to.equal('test');
});
});
});