forked from helpers/handlebars-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi18n.js
58 lines (48 loc) · 2.05 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
'use strict';
require('should');
var hbs = require('handlebars');
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() {
(function() {
hbs.compile('{{#i18n}}{{/i18n}}')();
}).should.throw('{{i18n}} helper expected "key" to be a string');
});
it('should throw an error when language parameter is not a string.', function() {
(function() {
hbs.compile('{{#i18n "key"}}{{/i18n}}')();
}).should.throw('{{i18n}} helper expected "language" parameter to be a string');
});
it('should throw an error when the language is not found.', function() {
(function() {
var ctx = {language: 'foo', en: {key: 'value'}, fr: {key: 'valeur'}};
hbs.compile('{{#i18n "key"}}{{/i18n}}')(ctx);
}).should.throw('{{i18n}} helper cannot find language "foo"');
});
it('should throw an error when a key is not found.', function() {
(function() {
var ctx = {language: 'en', en: {key: 'value'}, fr: {key: 'valeur'}};
hbs.compile('{{#i18n "foo"}}{{/i18n}}')(ctx);
}).should.throw('{{i18n}} helper cannot find property "foo" for language "en"');
});
it('should take a key and return for the default language', function() {
var fn = hbs.compile('{{#i18n "key"}}{{/i18n}}');
fn(context).should.equal('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'};
fn(context).should.equal('value');
});
it('should take a key and return for the override language', function() {
var fn = hbs.compile('{{#i18n "key" language="fr"}}{{/i18n}}');
fn(context).should.equal('valeur');
});
it('should support using dot notation for the key', function() {
var fn = hbs.compile('{{#i18n "a.b"}}{{/i18n}}');
fn(context).should.equal('c');
});
});