From 502f35869c7cf97ffd27337fc41485a156c537b1 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Tue, 12 Feb 2013 10:40:41 -0800 Subject: [PATCH] Fall back to en strings (fixes #738) --- locale/locale.js | 11 ++++++++--- test/index.html | 1 + test/index_packaged.html | 1 + test/spec/lib/locale.js | 26 ++++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 test/spec/lib/locale.js diff --git a/locale/locale.js b/locale/locale.js index c3dd5bcc4..8582df03e 100644 --- a/locale/locale.js +++ b/locale/locale.js @@ -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; } } diff --git a/test/index.html b/test/index.html index fe81a4ec5..3ba4e3bd8 100644 --- a/test/index.html +++ b/test/index.html @@ -144,6 +144,7 @@ + diff --git a/test/index_packaged.html b/test/index_packaged.html index cd3451357..874639094 100644 --- a/test/index_packaged.html +++ b/test/index_packaged.html @@ -22,6 +22,7 @@ + diff --git a/test/spec/lib/locale.js b/test/spec/lib/locale.js new file mode 100644 index 000000000..3228aa99b --- /dev/null +++ b/test/spec/lib/locale.js @@ -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'); + }); + }); +});