forked from helpers/handlebars-helpers
-
Notifications
You must be signed in to change notification settings - Fork 4
/
i18n.js
56 lines (47 loc) · 1.86 KB
/
i18n.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const assert = require('assert');
const hbs = require('handlebars').create();
const helpers = require('..');
helpers.i18n({ handlebars: hbs });
const context = {language: 'en', en: {key: 'value', a: {b: 'c'}}, fr: {key: 'valeur'}};
describe('i18n', function() {
it('should throw an error when key is not a string.', function() {
assert.throws(function() {
hbs.compile('{{#i18n}}{{/i18n}}')();
});
});
it('should throw an error when language parameter is not a string.', function() {
assert.throws(function() {
hbs.compile('{{#i18n "key"}}{{/i18n}}')();
});
});
it('should throw an error when the language is not found.', function() {
assert.throws(function() {
const ctx = {language: 'foo', en: {key: 'value'}, fr: {key: 'valeur'}};
hbs.compile('{{#i18n "key"}}{{/i18n}}')(ctx);
});
});
it('should throw an error when a key is not found.', function() {
assert.throws(function() {
const ctx = {language: 'en', en: {key: 'value'}, fr: {key: 'valeur'}};
hbs.compile('{{#i18n "foo"}}{{/i18n}}')(ctx);
});
});
it('should take a key and return for the default language', function() {
const fn = hbs.compile('{{#i18n "key"}}{{/i18n}}');
assert.equal(fn(context), 'value');
});
it('should use options passed on the context', function() {
const fn = hbs.compile('{{#i18n "key"}}{{/i18n}}');
const context = {en: {key: 'value'}, fr: {key: 'valeur'}};
context.options = {language: 'en'};
assert.equal(fn(context), 'value');
});
it('should take a key and return for the override language', function() {
const fn = hbs.compile('{{#i18n "key" language="fr"}}{{/i18n}}');
assert.equal(fn(context), 'valeur');
});
it('should support using dot notation for the key', function() {
const fn = hbs.compile('{{#i18n "a.b"}}{{/i18n}}');
assert.equal(fn(context), 'c');
});
});