Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Bug 914414 - Refactor l10n.js. r=vingtetun
Browse files Browse the repository at this point in the history
  • Loading branch information
stasm committed Apr 8, 2014
1 parent 15a7077 commit fd2d573
Show file tree
Hide file tree
Showing 19 changed files with 1,971 additions and 775 deletions.
Expand Up @@ -106,7 +106,7 @@ btExport-title = 匯出至藍牙
sdUMSEnabled = 因為已啟用 USB 儲存空間,無法使用 SD 卡
exportErrorTitle = 匯出錯誤

contactsExported2 = {{ plural(exported) }}
contactsExported2 = {[ plural(exported) ]}
contactsExported2[zero] = 未匯出聯絡人
contactsExported2[one] = 已匯出 {{exported}}/{{total}} 聯絡人
contactsExported2[two] = 已匯出 {{exported}}/{{total}} 聯絡人
Expand Down
Expand Up @@ -99,7 +99,7 @@ Contacts.prototype = {
return data;
}, [file, key]);

return string[key]._;
return string[key];
},

waitSlideLeft: function(elementKey) {
Expand Down
Expand Up @@ -3,7 +3,6 @@
* TODO: Shared code unit tests should not be in gallery
* Bug #841422 has been filed to move these tests
*/
require('/shared/js/l10n.js');

suite('L10n', function() {
var _;
Expand Down Expand Up @@ -38,7 +37,7 @@ suite('L10n', function() {
].join('\n');

var inlineL10Props = {
'inline-translation-test': {'_': 'static content provided by inlined JSON'}
'inline-translation-test': 'static content provided by inlined JSON'
};

var key_cropImage = 'cropimage';
Expand All @@ -48,9 +47,11 @@ suite('L10n', function() {
var key_multiLine = 'multiLine';
var key_backslash = 'trailingBackslash';

var xhr;

// Do not begin tests until the test locale has been loaded.
suiteSetup(function(done) {
var xhr = sinon.useFakeXMLHttpRequest();
xhr = sinon.useFakeXMLHttpRequest();

xhr.onCreate = function(request) {
setTimeout(function() {
Expand All @@ -62,7 +63,9 @@ suite('L10n', function() {
_translate = navigator.mozL10n.translate;
_localize = navigator.mozL10n.localize;

var lang = 'en-US';
// en-US has already been loaded in setup.js and l10n.js is smart enough
// not to re-fetch resources; hence, set the lang to something new
var lang = 'fr';

var inline = document.createElement('script');
inline.setAttribute('type', 'application/l10n');
Expand All @@ -73,13 +76,16 @@ suite('L10n', function() {
navigator.mozL10n.language.code = lang;
navigator.mozL10n.ready(function suiteSetup_ready() {
// Make sure to remove this event listener in case we re-translate
// below. The xhr mock won't exist any more.
// below.
window.removeEventListener('localized', suiteSetup_ready);
xhr.restore();
done();
});
});

suiteTeardown(function() {
xhr.restore();
});

suite('get', function() {
test('existing key', function() {
assert.strictEqual(_(key_cropImage), 'Crop');
Expand Down Expand Up @@ -240,7 +246,7 @@ suite('L10n', function() {
test('inline translation', function(done) {
elem.dataset.l10nId = 'inline-translation-test';
assert.equal(elem.textContent, '');
setLang('en-US', done, function() {
setLang('zh-TW', done, function() {
assert.equal(elem.textContent,
'static content provided by inlined JSON');
done();
Expand All @@ -250,7 +256,7 @@ suite('L10n', function() {
test('downloaded translation', function(done) {
elem.dataset.l10nId = 'cropimage';
assert.equal(elem.textContent, '');
setLang('en-US', done, function() {
setLang('ar', done, function() {
assert.equal(elem.textContent, 'Crop');
done();
});
Expand Down
73 changes: 73 additions & 0 deletions apps/gallery/test/unit/l10n/lib/context/basic_test.js
@@ -0,0 +1,73 @@
/* global it, assert:true, describe, beforeEach */
/* global window, navigator, process, __dirname */
'use strict';

var assert = require('assert') || window.assert;

if (typeof navigator !== 'undefined') {
var L10n = navigator.mozL10n._getInternalAPI();
var L20n = {
getContext: function() {
return new L10n.Context();
}
};
} else {
var L20n = process.env.L20N_COV ?
require('../../../build/cov/lib/l20n')
: require('../../../lib/l20n');
}

if (typeof navigator !== 'undefined') {
var path = 'http://gallery.gaiamobile.org:8080/test/unit/l10n/lib/context';
} else {
var path = __dirname;
}

describe('A simple context with one resource', function() {
var ctx;

beforeEach(function(done) {
ctx = L20n.getContext();
ctx.resLinks.push(path + '/fixtures/basic.properties');
ctx.ready(done);
ctx.requestLocales('en-US');
});

it('should return the string value of brandName', function() {
var value = ctx.get('brandName');
assert.strictEqual(value, 'Firefox');
});

it('should return the value of about with the value' +
' of brandName in it', function() {
var value = ctx.get('about');
assert.strictEqual(value, 'About Firefox');
});

it('should return the value of cert with the value of ' +
'organization passed directly', function() {
var value = ctx.get('cert', {organization: 'Mozilla Foundation'});
assert.strictEqual(value, 'Certificate signed by Mozilla Foundation');
});

it('should return the correct plural form for 0', function() {
var value = ctx.get('unreadMessages', {unread: 0});
assert.strictEqual(value, '0 unread');
});

it('should return the correct plural form for 1', function() {
var value = ctx.get('unreadMessages', {unread: 1});
assert.strictEqual(value, 'One unread');
});

it('should return the correct plural form for 2', function() {
var value = ctx.get('unreadMessages', {unread: 2});
assert.strictEqual(value, '2 unread');
});

it('should return the correct plural form for 3', function() {
var value = ctx.get('unreadMessages', {unread: 3});
assert.strictEqual(value, '3 unread');
});

});
124 changes: 124 additions & 0 deletions apps/gallery/test/unit/l10n/lib/context/fallback_test.js
@@ -0,0 +1,124 @@
/* global it, assert:true, describe, beforeEach */
/* global window, navigator, process, __dirname */
'use strict';

var assert = require('assert') || window.assert;

if (typeof navigator !== 'undefined') {
var L10n = navigator.mozL10n._getInternalAPI();
var Context = L10n.Context;
var path = 'http://gallery.gaiamobile.org:8080/test/unit/l10n/lib/context';
} else {
var Context = process.env.L20N_COV ?
require('../../../build/cov/lib/l20n/context').Context
: require('../../../lib/l20n/context').Context;
var path = __dirname;
}

describe('One fallback locale', function() {
var ctx;

beforeEach(function(done) {
ctx = new Context();
ctx.resLinks.push(path + '/fixtures/{{locale}}.properties');
ctx.once(done);
ctx.requestLocales('pl');
});

describe('Translation in the first locale exists and is OK', function() {
it('[e]', function() {
var entity = ctx.getEntity('e');
assert.strictEqual(entity, 'E pl');
});
});

describe('ValueError in first locale', function() {
describe('Entity exists in second locale:', function() {
it('[ve]', function() {
var entity = ctx.getEntity('ve');
assert.strictEqual(entity, 'VE {{ boo }} pl');
});
});

describe('ValueError in second locale:', function() {
it('[vv]', function() {
var entity = ctx.getEntity('vv');
assert.strictEqual(entity, 'VV {{ boo }} pl');
});
});

describe('IndexError in second locale:', function() {
it('[vi]', function() {
var entity = ctx.getEntity('vi');
assert.strictEqual(entity, 'VI {{ boo }} pl');
});
});

describe('Entity missing in second locale:', function() {
it('[vm]', function() {
var entity = ctx.getEntity('vm');
assert.strictEqual(entity, 'VM {{ boo }} pl');
});
});
});

describe('IndexError in first locale', function() {
describe('Entity exists in second locale', function() {
it('[ie]', function() {
var entity = ctx.getEntity('ie');
assert.strictEqual(entity, undefined);
});
});

describe('ValueError in second locale', function() {
it('[iv]', function() {
var entity = ctx.getEntity('iv');
assert.strictEqual(entity, undefined);
});
});

describe('IndexError in second locale', function() {
it('[ii]', function() {
var entity = ctx.getEntity('ii');
assert.strictEqual(entity, undefined);
});
});

describe('Entity missing in second locale:', function() {
it('[im]', function() {
var entity = ctx.getEntity('im');
assert.strictEqual(entity, undefined);
});
});
});

describe('Entity not found in first locale', function() {
describe('Entity exists in second locale:', function() {
it('[me]', function() {
var entity = ctx.getEntity('me');
assert.strictEqual(entity, 'ME en-US');
});
});

describe('ValueError in second locale:', function() {
it('[mv]', function() {
var entity = ctx.getEntity('mv');
assert.strictEqual(entity, 'MV {{ boo }} en-US');
});
});

describe('IndexError in second locale:', function() {
it('[mi]', function() {
var entity = ctx.getEntity('mi');
assert.strictEqual(entity, undefined);
});
});

describe('Entity missing in second locale:', function() {
it('[mm]', function() {
var entity = ctx.getEntity('mm');
assert.strictEqual(entity, null);
});
});
});
});
@@ -0,0 +1,6 @@
brandName=Firefox
about=About {{ brandName }}
cert=Certificate signed by {{ organization }}
unreadMessages={[plural(unread)]}
unreadMessages[one]=One unread
unreadMessages[other]={{unread}} unread
57 changes: 57 additions & 0 deletions apps/gallery/test/unit/l10n/lib/context/fixtures/en-US.properties
@@ -0,0 +1,57 @@
foo=Foo en-US

# e - existing
# v - ValueError
# i - IndexError
# m - missing

me=ME en-US
mv=MV {{ boo }} en-US
mi={[plural(boo)]}
mi[key]=MI en-US

vve=VVE en-US
vvv=VVV {{ boo }} en-US
<vvi[boo] { key: "VVI en-US" }>

vie=VIE en-US
viv=VIV {{ boo }} en-US
vii={[plural(boo)]}
vii[key]=VII en-US

vme=VME en-US
vmv=VMV {{ boo }} en-US
vmi={[plural(boo)]}
vmi[key]=VMI en-US


ive=IVE en-US
ivv=IVV {{ boo }} en-US
ivi={[plural(boo)]}
ivi[key]=IVI en-US

iie=IIE en-US
iiv=IIV {{ boo }} en-US
iii={[plural(boo)]}
iii[key]=III en-US

ime=IME en-US
imv=IMV {{ boo }} en-US
imi={[plural(boo)]}
imi[key]=IMI en-US


mve=MVE en-US
mvv=MVV {{ boo }} en-US
mvi={[plural(boo)]}
mvi[key]=MVI en-US

mie=MIE en-US
miv=MIV {{ boo }} en-US
mii={[plural(boo)]}
mii[key]=MII en-US

mme=MME en-US
mmv=MMV {{ boo }} en-US
mmi={[plural(boo)]}
mmi[key]=MMI en-US

0 comments on commit fd2d573

Please sign in to comment.