1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
11 sinon.stub(chrome.i18n, 'getMessage');
13 teardown: function() {
14 chrome.i18n.getMessage.restore();
18 test('getTranslationOrError(tag) should return tag on error', function() {
19 var translation = l10n.getTranslationOrError('non_existent_tag');
20 equal(translation, 'non_existent_tag');
23 test('localizeElementFromTag() should replace innerText by default',
25 var element = document.createElement('div');
26 chrome.i18n.getMessage.withArgs('tag').returns('<b>Hello World</b>');
28 l10n.localizeElementFromTag(element, 'tag');
30 equal(element.innerHTML, '<b>Hello World</b>');
33 test('localizeElementFromTag() should replace innerHTML if flag is set',
35 var element = document.createElement('div');
36 chrome.i18n.getMessage.withArgs('tag').returns('<b>Hello World</b>');
38 l10n.localizeElementFromTag(element, 'tag', null, true);
40 equal(element.innerHTML, '<b>Hello World</b>');
44 'localizeElement() should replace innerText using the "i18n-content" ' +
45 'attribute as the tag',
47 var element = document.createElement('div');
48 element.setAttribute('i18n-content', 'tag');
49 chrome.i18n.getMessage.withArgs('tag').returns('<b>Hello World</b>');
51 l10n.localizeElement(element);
53 equal(element.innerHTML, '<b>Hello World</b>');
57 'localize() should replace element title using the "i18n-title" ' +
58 'attribute as the tag',
60 var fixture = document.getElementById('qunit-fixture');
61 fixture.innerHTML = '<div class="target" i18n-title="tag"></div>';
62 chrome.i18n.getMessage.withArgs('tag').returns('localized title');
66 var target = document.querySelector('.target');
67 equal(target.title, 'localized title');
70 test('localize() should support string substitutions', function() {
71 var fixture = document.getElementById('qunit-fixture');
73 '<div class="target" ' +
74 'i18n-content="tag" ' +
75 'i18n-value-1="param1" ' +
76 'i18n-value-2="param2">' +
79 chrome.i18n.getMessage.withArgs('tag', ['param1', 'param2'])
80 .returns('localized');
84 var target = document.querySelector('.target');
85 equal(target.innerText, 'localized');
88 test('localize() should support tag substitutions', function() {
89 var fixture = document.getElementById('qunit-fixture');
91 '<div class="target" i18n-content="tag"' +
92 ' i18n-value-name-1="tag1" i18n-value-name-2="tag2"></div>';
94 var getMessage = chrome.i18n.getMessage;
95 getMessage.withArgs('tag1').returns('param1');
96 getMessage.withArgs('tag2').returns('param2');
97 getMessage.withArgs('tag', ['param1', 'param2']).returns('localized');
101 var target = document.querySelector('.target');
102 equal(target.innerText, 'localized');