-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
261 lines (210 loc) · 6.54 KB
/
test.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
'use strict'
var chai = require('chai'),
should = chai.should(),
Metalsmith = require('metalsmith'),
i18next = require('i18next'),
i18nextMS = require('.'),
helpers = require('./lib/helpers'),
copy = require('metalsmith-copy'),
templates = require('metalsmith-in-place'),
uglify = require('metalsmith-uglify')
describe('metalsmith-i18next', function(){
// ------------------------------------------------------------------------
// Generic Test Case
// ------------------------------------------------------------------------
function metalsmithTest(config, check) {
var once = false
return function(done) {
Metalsmith('./examples')
.use(i18nextMS(config))
.use(templates({
engine: 'haml-coffee',
pattern: '**/*.hamlc'
}))
.use(copy({
pattern: '**/*.hamlc',
extension: '.txt',
move: true
}))
.use(uglify())
.build(function(err, files){
if (once) return
once = true
if (err) return done(err)
try {
check(files)
done()
} catch(err) {
done(err)
}
})
}
}
function prop(value) {
return function(v) {
return arguments.length? (value = v) : value
}
}
// ------------------------------------------------------------------------
// Helper Test Cases
// ------------------------------------------------------------------------
it('should return the expected file parts', function(done){
i18next.init({
lng: 'en',
resGetPath: './examples/locales/__lng__/__ns__.json',
ns: {namespaces:['translations'], defaultNs:'translations'},
preload: ['en','fr'],
getAsync: false,
fallbackLng: false
})
var fileParts = helpers(i18next).fileParts,
parts
fileParts('index.html').should.eql({
file: 'index.html',
ext: '.html',
base: 'index.html',
dir: '',
name: 'index',
locale: 'en',
hash: '',
query: ''
})
fileParts('a/b/index.php?filter=cars').should.eql({
file: 'a/b/index.php?filter=cars',
ext: '.php',
base: 'index.php',
dir: 'a/b',
name: 'index',
locale: 'en',
hash: '',
query: '?filter=cars'
})
fileParts('computers/laptop.html#specs','fr').should.eql({
file: 'computers/laptop.html#specs',
ext: '.html',
base: 'laptop.html',
dir: 'computers',
name: 'laptop',
locale: 'fr',
hash: '#specs',
query: ''
})
fileParts('a/b/index.php?filter=cars#heading','fr').should.eql({
file: 'a/b/index.php?filter=cars#heading',
ext: '.php',
base: 'index.php',
dir: 'a/b',
name: 'index',
locale: 'fr',
hash: '#heading',
query: '?filter=cars'
})
done()
})
it('should localize the path as expected', function(done){
var path = prop(':locale/:file'),
locale = prop('en'),
tpath = helpers(i18next, {path, locale}).tpath
tpath('/').should.equal('/en')
tpath('/index.html').should.equal('/en/index.html')
tpath('/index.html','fr').should.equal('/fr/index.html')
path(':dir/:name-:locale:ext:query:hash')
tpath('/foo/bar.php?filter=cars#heading').should.equal('/foo/bar-en.php?filter=cars#heading')
locale('fr')
tpath('/foo/bar.php?filter=cars#heading').should.equal('/foo/bar-fr.php?filter=cars#heading')
done()
})
it('should translate as expected', function(done){
var path = prop(':locale/:file'),
locale = prop('fr'),
prefix = prop(['home','common']),
namespace = prop('translations'),
fn = helpers(i18next, {path, prefix, locale, namespace})
fn.t('translations:home.hello',{lng:'fr'}).should.equal('Bonjour')
fn.tt('hello').should.equal('Bonjour')
fn.tt('foo').should.equal('Fou!!!')
fn.tt('bar').should.equal('[home,common].bar')
done()
})
// ------------------------------------------------------------------------
// Normal Test Cases
// ------------------------------------------------------------------------
it('should create two localised directories each with index.txt', metalsmithTest(
{
pattern: '**/*.hamlc',
locales: ['en','fr'],
nsPath: './examples/locales/__lng__/__ns__.json',
namespaces: ['translations']
},
function(files) {
var enFile = files['en/index.txt'],
frFile = files['fr/index.txt']
should.exist(enFile)
should.exist(frFile)
enFile.contents.toString('utf8').should.equal('Hello')
frFile.contents.toString('utf8').should.equal('Bonjour')
should.exist(enFile.i18nBootstrap)
should.exist(frFile.i18nBootstrap)
should.exist(enFile.i18nOrigPath)
should.exist(frFile.i18nOrigPath)
should.exist(enFile.i18nResStore)
should.exist(frFile.i18nResStore)
enFile.i18nResStore.should.eql({en: {translations: {common:{foo:'Foo!!!'}, home: {hello: 'Hello'}},foo:{foo:{bar:'Foobar'}}}})
frFile.i18nResStore.should.eql({fr: {translations: {common:{foo:'Fou!!!'}, home: {hello: 'Bonjour'}},foo:{foo:{bar:'Foobar!!'}}}})
}
))
it('should create both index-en.txt and index-fr.txt in the same directory', metalsmithTest(
{
pattern: '**/*.hamlc',
locales: ['en','fr'],
nsPath: './examples/locales/__lng__/__ns__.json',
namespaces: ['translations'],
path: ':dir/:name-:locale:ext'
},
function(files) {
var enFile = files['index-en.txt'],
frFile = files['index-fr.txt']
should.exist(enFile)
should.exist(frFile)
enFile.contents.toString('utf8').should.equal('Hello')
frFile.contents.toString('utf8').should.equal('Bonjour')
}
))
it('file should have t, tt, tpath and locale', metalsmithTest(
{
pattern: '**/*.hamlc',
locales: ['en','fr'],
nsPath: './examples/locales/__lng__/__ns__.json',
namespaces: ['translations'],
path: ':dir/:name-:locale:ext'
},
function(files) {
var enFile = files['index-en.txt'],
frFile = files['index-fr.txt']
should.exist(enFile)
should.exist(frFile)
enFile.t.should.exist
enFile.tt.should.exist
enFile.tpath.should.exist
enFile.locale.should.equal('en')
frFile.t.should.exist
frFile.tt.should.exist
frFile.tpath.should.exist
frFile.locale.should.equal('fr')
}
))
it('should allow tpath to override the locale', metalsmithTest(
{
pattern: '**/*.hamlc',
locales: ['en','fr'],
nsPath: './examples/locales/__lng__/__ns__.json',
namespaces: ['translations']
},
function(files) {
var enFile = files['en/index.txt']
should.exist(enFile)
enFile.tpath('/toto.txt').should.equal('/en/toto.txt')
enFile.tpath('/toto.txt','fr').should.equal('/fr/toto.txt')
}
))
})