forked from helpers/handlebars-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n.js
59 lines (49 loc) · 1.87 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
57
58
59
'use strict';
require('mocha');
var assert = require('assert');
var hbs = require('handlebars').create();
var helpers = require('..');
helpers.i18n({handlebars: hbs});
var 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() {
var 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() {
var 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() {
var fn = hbs.compile('{{#i18n "key"}}{{/i18n}}');
assert.equal(fn(context), 'value');
});
it('should use options passed on the context', function() {
var fn = hbs.compile('{{#i18n "key"}}{{/i18n}}');
var 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() {
var fn = hbs.compile('{{#i18n "key" language="fr"}}{{/i18n}}');
assert.equal(fn(context), 'valeur');
});
it('should support using dot notation for the key', function() {
var fn = hbs.compile('{{#i18n "a.b"}}{{/i18n}}');
assert.equal(fn(context), 'c');
});
});