Repository navigation
Expand file tree
/
Copy pathpolyglot.html
More file actions
560 lines (426 loc) · 30.2 KB
/
Copy pathpolyglot.html
File metadata and controls
560 lines (426 loc) · 30.2 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
<!DOCTYPE html>
<html>
<head>
<title>polyglot.js</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
<link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
<div id="container">
<div id="background"></div>
<ul class="sections">
<li id="title">
<div class="annotation">
<h1>polyglot.js</h1>
</div>
</li>
<li id="section-1">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-1">¶</a>
</div>
<pre><code>(c) <span class="hljs-number">2012</span><span class="hljs-number">-2016</span> Airbnb, Inc.
polyglot.js may be freely distributed under the terms <span class="hljs-keyword">of</span> the BSD
license. For all licensing information, details, and documention:
http:<span class="hljs-comment">//airbnb.github.com/polyglot.js</span>
</code></pre><p>Polyglot.js is an I18n helper library written in JavaScript, made to
work both in the browser and in Node. It provides a simple solution for
interpolation and pluralization, based off of Airbnb’s
experience adding I18n functionality to its Backbone.js and Node apps.</p>
<p>Polylglot is agnostic to your translation backend. It doesn’t perform any
translation; it simply gives you a way to manage translated phrases from
your client- or server-side JavaScript application.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-meta">
'use strict'</span>;
<span class="hljs-keyword">var</span> forEach = <span class="hljs-built_in">require</span>(<span class="hljs-string">'for-each'</span>);
<span class="hljs-keyword">var</span> warning = <span class="hljs-built_in">require</span>(<span class="hljs-string">'warning'</span>);
<span class="hljs-keyword">var</span> has = <span class="hljs-built_in">require</span>(<span class="hljs-string">'has'</span>);
<span class="hljs-keyword">var</span> trim = <span class="hljs-built_in">require</span>(<span class="hljs-string">'string.prototype.trim'</span>);
<span class="hljs-keyword">var</span> warn = <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">warn</span>(<span class="hljs-params">message</span>) </span>{
warning(<span class="hljs-literal">false</span>, message);
};
<span class="hljs-keyword">var</span> replace = <span class="hljs-built_in">String</span>.prototype.replace;
<span class="hljs-keyword">var</span> split = <span class="hljs-built_in">String</span>.prototype.split;</pre></div></div>
</li>
<li id="section-2">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-2">¶</a>
</div>
<h4 id="pluralization-methods">Pluralization methods</h4>
<p>The string that separates the different phrase possibilities.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">var</span> delimeter = <span class="hljs-string">'||||'</span>;</pre></div></div>
</li>
<li id="section-3">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-3">¶</a>
</div>
<p>Mapping from pluralization group plural logic.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">var</span> pluralTypes = {
arabic: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">n</span>) </span>{</pre></div></div>
</li>
<li id="section-4">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-4">¶</a>
</div>
<p><a href="http://www.arabeyes.org/Plural_Forms">http://www.arabeyes.org/Plural_Forms</a></p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (n < <span class="hljs-number">3</span>) { <span class="hljs-keyword">return</span> n; }
<span class="hljs-keyword">if</span> (n % <span class="hljs-number">100</span> >= <span class="hljs-number">3</span> && n % <span class="hljs-number">100</span> <= <span class="hljs-number">10</span>) <span class="hljs-keyword">return</span> <span class="hljs-number">3</span>;
<span class="hljs-keyword">return</span> n % <span class="hljs-number">100</span> >= <span class="hljs-number">11</span> ? <span class="hljs-number">4</span> : <span class="hljs-number">5</span>;
},
chinese: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{ <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>; },
german: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">n</span>) </span>{ <span class="hljs-keyword">return</span> n !== <span class="hljs-number">1</span> ? <span class="hljs-number">1</span> : <span class="hljs-number">0</span>; },
french: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">n</span>) </span>{ <span class="hljs-keyword">return</span> n > <span class="hljs-number">1</span> ? <span class="hljs-number">1</span> : <span class="hljs-number">0</span>; },
russian: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">n</span>) </span>{
<span class="hljs-keyword">if</span> (n % <span class="hljs-number">10</span> === <span class="hljs-number">1</span> && n % <span class="hljs-number">100</span> !== <span class="hljs-number">11</span>) { <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>; }
<span class="hljs-keyword">return</span> n % <span class="hljs-number">10</span> >= <span class="hljs-number">2</span> && n % <span class="hljs-number">10</span> <= <span class="hljs-number">4</span> && (n % <span class="hljs-number">100</span> < <span class="hljs-number">10</span> || n % <span class="hljs-number">100</span> >= <span class="hljs-number">20</span>) ? <span class="hljs-number">1</span> : <span class="hljs-number">2</span>;
},
czech: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">n</span>) </span>{
<span class="hljs-keyword">if</span> (n === <span class="hljs-number">1</span>) { <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>; }
<span class="hljs-keyword">return</span> (n >= <span class="hljs-number">2</span> && n <= <span class="hljs-number">4</span>) ? <span class="hljs-number">1</span> : <span class="hljs-number">2</span>;
},
polish: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">n</span>) </span>{
<span class="hljs-keyword">if</span> (n === <span class="hljs-number">1</span>) { <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>; }
<span class="hljs-keyword">return</span> n % <span class="hljs-number">10</span> >= <span class="hljs-number">2</span> && n % <span class="hljs-number">10</span> <= <span class="hljs-number">4</span> && (n % <span class="hljs-number">100</span> < <span class="hljs-number">10</span> || n % <span class="hljs-number">100</span> >= <span class="hljs-number">20</span>) ? <span class="hljs-number">1</span> : <span class="hljs-number">2</span>;
},
icelandic: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">n</span>) </span>{ <span class="hljs-keyword">return</span> (n % <span class="hljs-number">10</span> !== <span class="hljs-number">1</span> || n % <span class="hljs-number">100</span> === <span class="hljs-number">11</span>) ? <span class="hljs-number">1</span> : <span class="hljs-number">0</span>; }
};</pre></div></div>
</li>
<li id="section-5">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-5">¶</a>
</div>
<p>Mapping from pluralization group to individual locales.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">var</span> pluralTypeToLanguages = {
arabic: [<span class="hljs-string">'ar'</span>],
chinese: [<span class="hljs-string">'fa'</span>, <span class="hljs-string">'id'</span>, <span class="hljs-string">'ja'</span>, <span class="hljs-string">'ko'</span>, <span class="hljs-string">'lo'</span>, <span class="hljs-string">'ms'</span>, <span class="hljs-string">'th'</span>, <span class="hljs-string">'tr'</span>, <span class="hljs-string">'zh'</span>],
german: [<span class="hljs-string">'da'</span>, <span class="hljs-string">'de'</span>, <span class="hljs-string">'en'</span>, <span class="hljs-string">'es'</span>, <span class="hljs-string">'fi'</span>, <span class="hljs-string">'el'</span>, <span class="hljs-string">'he'</span>, <span class="hljs-string">'hu'</span>, <span class="hljs-string">'it'</span>, <span class="hljs-string">'nl'</span>, <span class="hljs-string">'no'</span>, <span class="hljs-string">'pt'</span>, <span class="hljs-string">'sv'</span>],
french: [<span class="hljs-string">'fr'</span>, <span class="hljs-string">'tl'</span>, <span class="hljs-string">'pt-br'</span>],
russian: [<span class="hljs-string">'hr'</span>, <span class="hljs-string">'ru'</span>, <span class="hljs-string">'lt'</span>],
czech: [<span class="hljs-string">'cs'</span>, <span class="hljs-string">'sk'</span>],
polish: [<span class="hljs-string">'pl'</span>],
icelandic: [<span class="hljs-string">'is'</span>]
};
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">langToTypeMap</span>(<span class="hljs-params">mapping</span>) </span>{
<span class="hljs-keyword">var</span> ret = {};
forEach(mapping, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">langs, type</span>) </span>{
forEach(langs, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">lang</span>) </span>{
ret[lang] = type;
});
});
<span class="hljs-keyword">return</span> ret;
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">pluralTypeName</span>(<span class="hljs-params">locale</span>) </span>{
<span class="hljs-keyword">var</span> langToPluralType = langToTypeMap(pluralTypeToLanguages);
<span class="hljs-keyword">return</span> langToPluralType[locale]
|| langToPluralType[split.call(locale, <span class="hljs-regexp">/-/</span>, <span class="hljs-number">1</span>)[<span class="hljs-number">0</span>]]
|| langToPluralType.en;
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">pluralTypeIndex</span>(<span class="hljs-params">locale, count</span>) </span>{
<span class="hljs-keyword">return</span> pluralTypes[pluralTypeName(locale)](count);
}
<span class="hljs-keyword">var</span> dollarRegex = <span class="hljs-regexp">/\$/g</span>;
<span class="hljs-keyword">var</span> dollarBillsYall = <span class="hljs-string">'$$'</span>;
<span class="hljs-keyword">var</span> tokenRegex = <span class="hljs-regexp">/%\{(.*?)\}/g</span>;</pre></div></div>
</li>
<li id="section-6">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-6">¶</a>
</div>
<h3 id="transformphrase-phrase-substitutions-locale-">transformPhrase(phrase, substitutions, locale)</h3>
<p>Takes a phrase string and transforms it by choosing the correct
plural form and interpolating it.</p>
<pre><code>transformPhrase(<span class="hljs-string">'Hello, %{name}!'</span>, {name: <span class="hljs-string">'Spike'</span>});
<span class="hljs-comment">// "Hello, Spike!"</span>
</code></pre><p>The correct plural form is selected if substitutions.smart_count
is set. You can pass in a number instead of an Object as <code>substitutions</code>
as a shortcut for <code>smart_count</code>.</p>
<pre><code>transformPhrase(<span class="hljs-string">'%{smart_count} new messages |||| 1 new message'</span>, {smart_count: <span class="hljs-number">1</span>}, <span class="hljs-string">'en'</span>);
<span class="hljs-comment">// "1 new message"</span>
transformPhrase(<span class="hljs-string">'%{smart_count} new messages |||| 1 new message'</span>, {smart_count: <span class="hljs-number">2</span>}, <span class="hljs-string">'en'</span>);
<span class="hljs-comment">// "2 new messages"</span>
transformPhrase(<span class="hljs-string">'%{smart_count} new messages |||| 1 new message'</span>, <span class="hljs-number">5</span>, <span class="hljs-string">'en'</span>);
<span class="hljs-comment">// "5 new messages"</span>
</code></pre><p>You should pass in a third argument, the locale, to specify the correct plural type.
It defaults to <code>'en'</code> with 2 plural forms.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">transformPhrase</span>(<span class="hljs-params">phrase, substitutions, locale</span>) </span>{
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> phrase !== <span class="hljs-string">'string'</span>) {
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">TypeError</span>(<span class="hljs-string">'Polyglot.transformPhrase expects argument #1 to be string'</span>);
}
<span class="hljs-keyword">if</span> (substitutions == <span class="hljs-literal">null</span>) {
<span class="hljs-keyword">return</span> phrase;
}
<span class="hljs-keyword">var</span> result = phrase;</pre></div></div>
</li>
<li id="section-7">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-7">¶</a>
</div>
<p>allow number as a pluralization shortcut</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> options = <span class="hljs-keyword">typeof</span> substitutions === <span class="hljs-string">'number'</span> ? { smart_count: substitutions } : substitutions;</pre></div></div>
</li>
<li id="section-8">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-8">¶</a>
</div>
<p>Select plural form: based on a phrase text that contains <code>n</code>
plural forms separated by <code>delimeter</code>, a <code>locale</code>, and a <code>substitutions.smart_count</code>,
choose the correct plural form. This is only done if <code>count</code> is set.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (options.smart_count != <span class="hljs-literal">null</span> && result) {
<span class="hljs-keyword">var</span> texts = split.call(result, delimeter);
result = trim(texts[pluralTypeIndex(locale || <span class="hljs-string">'en'</span>, options.smart_count)] || texts[<span class="hljs-number">0</span>]);
}</pre></div></div>
</li>
<li id="section-9">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-9">¶</a>
</div>
<p>Interpolate: Creates a <code>RegExp</code> object for each interpolation placeholder.</p>
</div>
<div class="content"><div class='highlight'><pre> result = replace.call(result, tokenRegex, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">expression, argument</span>) </span>{
<span class="hljs-keyword">if</span> (!has(options, argument)) { <span class="hljs-keyword">return</span> <span class="hljs-string">''</span>; }
<span class="hljs-keyword">if</span> (options[argument] == <span class="hljs-literal">null</span>) { <span class="hljs-keyword">return</span> expression; }</pre></div></div>
</li>
<li id="section-10">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-10">¶</a>
</div>
<p>Ensure replacement value is escaped to prevent special $-prefixed regex replace tokens.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">return</span> replace.call(options[argument], dollarRegex, dollarBillsYall);
});
<span class="hljs-keyword">return</span> result;
}</pre></div></div>
</li>
<li id="section-11">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-11">¶</a>
</div>
<h3 id="polyglot-class-constructor">Polyglot class constructor</h3>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Polyglot</span>(<span class="hljs-params">options</span>) </span>{
<span class="hljs-keyword">var</span> opts = options || {};
<span class="hljs-keyword">this</span>.phrases = {};
<span class="hljs-keyword">this</span>.extend(opts.phrases || {});
<span class="hljs-keyword">this</span>.currentLocale = opts.locale || <span class="hljs-string">'en'</span>;
<span class="hljs-keyword">var</span> allowMissing = opts.allowMissing ? transformPhrase : <span class="hljs-literal">null</span>;
<span class="hljs-keyword">this</span>.onMissingKey = <span class="hljs-keyword">typeof</span> opts.onMissingKey === <span class="hljs-string">'function'</span> ? opts.onMissingKey : allowMissing;
<span class="hljs-keyword">this</span>.warn = opts.warn || warn;
}</pre></div></div>
</li>
<li id="section-12">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-12">¶</a>
</div>
<h3 id="polyglot-locale-locale-">polyglot.locale([locale])</h3>
<p>Get or set locale. Internally, Polyglot only uses locale for pluralization.</p>
</div>
<div class="content"><div class='highlight'><pre>Polyglot.prototype.locale = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">newLocale</span>) </span>{
<span class="hljs-keyword">if</span> (newLocale) <span class="hljs-keyword">this</span>.currentLocale = newLocale;
<span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.currentLocale;
};</pre></div></div>
</li>
<li id="section-13">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-13">¶</a>
</div>
<h3 id="polyglot-extend-phrases-">polyglot.extend(phrases)</h3>
<p>Use <code>extend</code> to tell Polyglot how to translate a given key.</p>
<pre><code>polyglot.extend({
<span class="hljs-string">"hello"</span>: <span class="hljs-string">"Hello"</span>,
<span class="hljs-string">"hello_name"</span>: <span class="hljs-string">"Hello, %{name}"</span>
});
</code></pre><p>The key can be any string. Feel free to call <code>extend</code> multiple times;
it will override any phrases with the same key, but leave existing phrases
untouched.</p>
<p>It is also possible to pass nested phrase objects, which get flattened
into an object with the nested keys concatenated using dot notation.</p>
<pre><code>polyglot.extend({
<span class="hljs-string">"nav"</span>: {
<span class="hljs-string">"hello"</span>: <span class="hljs-string">"Hello"</span>,
<span class="hljs-string">"hello_name"</span>: <span class="hljs-string">"Hello, %{name}"</span>,
<span class="hljs-string">"sidebar"</span>: {
<span class="hljs-string">"welcome"</span>: <span class="hljs-string">"Welcome"</span>
}
}
});
<span class="hljs-built_in">console</span>.log(polyglot.phrases);
<span class="hljs-comment">// {</span>
<span class="hljs-comment">// 'nav.hello': 'Hello',</span>
<span class="hljs-comment">// 'nav.hello_name': 'Hello, %{name}',</span>
<span class="hljs-comment">// 'nav.sidebar.welcome': 'Welcome'</span>
<span class="hljs-comment">// }</span>
</code></pre><p><code>extend</code> accepts an optional second argument, <code>prefix</code>, which can be used
to prefix every key in the phrases object with some string, using dot
notation.</p>
<pre><code>polyglot.extend({
<span class="hljs-string">"hello"</span>: <span class="hljs-string">"Hello"</span>,
<span class="hljs-string">"hello_name"</span>: <span class="hljs-string">"Hello, %{name}"</span>
}, <span class="hljs-string">"nav"</span>);
<span class="hljs-built_in">console</span>.log(polyglot.phrases);
<span class="hljs-comment">// {</span>
<span class="hljs-comment">// 'nav.hello': 'Hello',</span>
<span class="hljs-comment">// 'nav.hello_name': 'Hello, %{name}'</span>
<span class="hljs-comment">// }</span>
</code></pre><p>This feature is used internally to support nested phrase objects.</p>
</div>
<div class="content"><div class='highlight'><pre>Polyglot.prototype.extend = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">morePhrases, prefix</span>) </span>{
forEach(morePhrases, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">phrase, key</span>) </span>{
<span class="hljs-keyword">var</span> prefixedKey = prefix ? prefix + <span class="hljs-string">'.'</span> + key : key;
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> phrase === <span class="hljs-string">'object'</span>) {
<span class="hljs-keyword">this</span>.extend(phrase, prefixedKey);
} <span class="hljs-keyword">else</span> {
<span class="hljs-keyword">this</span>.phrases[prefixedKey] = phrase;
}
}, <span class="hljs-keyword">this</span>);
};</pre></div></div>
</li>
<li id="section-14">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-14">¶</a>
</div>
<h3 id="polyglot-unset-phrases-">polyglot.unset(phrases)</h3>
<p>Use <code>unset</code> to selectively remove keys from a polyglot instance.</p>
<pre><code>polyglot.unset(<span class="hljs-string">"some_key"</span>);
polyglot.unset({
<span class="hljs-string">"hello"</span>: <span class="hljs-string">"Hello"</span>,
<span class="hljs-string">"hello_name"</span>: <span class="hljs-string">"Hello, %{name}"</span>
});
</code></pre><p>The unset method can take either a string (for the key), or an object hash with
the keys that you would like to unset.</p>
</div>
<div class="content"><div class='highlight'><pre>Polyglot.prototype.unset = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">morePhrases, prefix</span>) </span>{
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> morePhrases === <span class="hljs-string">'string'</span>) {
<span class="hljs-keyword">delete</span> <span class="hljs-keyword">this</span>.phrases[morePhrases];
} <span class="hljs-keyword">else</span> {
forEach(morePhrases, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">phrase, key</span>) </span>{
<span class="hljs-keyword">var</span> prefixedKey = prefix ? prefix + <span class="hljs-string">'.'</span> + key : key;
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> phrase === <span class="hljs-string">'object'</span>) {
<span class="hljs-keyword">this</span>.unset(phrase, prefixedKey);
} <span class="hljs-keyword">else</span> {
<span class="hljs-keyword">delete</span> <span class="hljs-keyword">this</span>.phrases[prefixedKey];
}
}, <span class="hljs-keyword">this</span>);
}
};</pre></div></div>
</li>
<li id="section-15">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-15">¶</a>
</div>
<h3 id="polyglot-clear-">polyglot.clear()</h3>
<p>Clears all phrases. Useful for special cases, such as freeing
up memory if you have lots of phrases but no longer need to
perform any translation. Also used internally by <code>replace</code>.</p>
</div>
<div class="content"><div class='highlight'><pre>Polyglot.prototype.clear = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">this</span>.phrases = {};
};</pre></div></div>
</li>
<li id="section-16">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-16">¶</a>
</div>
<h3 id="polyglot-replace-phrases-">polyglot.replace(phrases)</h3>
<p>Completely replace the existing phrases with a new set of phrases.
Normally, just use <code>extend</code> to add more phrases, but under certain
circumstances, you may want to make sure no old phrases are lying around.</p>
</div>
<div class="content"><div class='highlight'><pre>Polyglot.prototype.replace = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">newPhrases</span>) </span>{
<span class="hljs-keyword">this</span>.clear();
<span class="hljs-keyword">this</span>.extend(newPhrases);
};</pre></div></div>
</li>
<li id="section-17">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-17">¶</a>
</div>
<h3 id="polyglot-t-key-options-">polyglot.t(key, options)</h3>
<p>The most-used method. Provide a key, and <code>t</code> will return the
phrase.</p>
<pre><code>polyglot.t(<span class="hljs-string">"hello"</span>);
=> <span class="hljs-string">"Hello"</span>
</code></pre><p>The phrase value is provided first by a call to <code>polyglot.extend()</code> or
<code>polyglot.replace()</code>.</p>
<p>Pass in an object as the second argument to perform interpolation.</p>
<pre><code>polyglot.t(<span class="hljs-string">"hello_name"</span>, {name: <span class="hljs-string">"Spike"</span>});
=> <span class="hljs-string">"Hello, Spike"</span>
</code></pre><p>If you like, you can provide a default value in case the phrase is missing.
Use the special option key “_” to specify a default.</p>
<pre><code>polyglot.t(<span class="hljs-string">"i_like_to_write_in_language"</span>, {
_: <span class="hljs-string">"I like to write in %{language}."</span>,
language: <span class="hljs-string">"JavaScript"</span>
});
=> <span class="hljs-string">"I like to write in JavaScript."</span>
</code></pre>
</div>
<div class="content"><div class='highlight'><pre>Polyglot.prototype.t = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">key, options</span>) </span>{
<span class="hljs-keyword">var</span> phrase, result;
<span class="hljs-keyword">var</span> opts = options == <span class="hljs-literal">null</span> ? {} : options;
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> <span class="hljs-keyword">this</span>.phrases[key] === <span class="hljs-string">'string'</span>) {
phrase = <span class="hljs-keyword">this</span>.phrases[key];
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> opts._ === <span class="hljs-string">'string'</span>) {
phrase = opts._;
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.onMissingKey) {
<span class="hljs-keyword">var</span> onMissingKey = <span class="hljs-keyword">this</span>.onMissingKey;
result = onMissingKey(key, opts, <span class="hljs-keyword">this</span>.currentLocale);
} <span class="hljs-keyword">else</span> {
<span class="hljs-keyword">this</span>.warn(<span class="hljs-string">'Missing translation for key: "'</span> + key + <span class="hljs-string">'"'</span>);
result = key;
}
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> phrase === <span class="hljs-string">'string'</span>) {
result = transformPhrase(phrase, opts, <span class="hljs-keyword">this</span>.currentLocale);
}
<span class="hljs-keyword">return</span> result;
};</pre></div></div>
</li>
<li id="section-18">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-18">¶</a>
</div>
<h3 id="polyglot-has-key-">polyglot.has(key)</h3>
<p>Check if polyglot has a translation for given key</p>
</div>
<div class="content"><div class='highlight'><pre>Polyglot.prototype.has = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">key</span>) </span>{
<span class="hljs-keyword">return</span> has(<span class="hljs-keyword">this</span>.phrases, key);
};</pre></div></div>
</li>
<li id="section-19">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-19">¶</a>
</div>
<p>export transformPhrase</p>
</div>
<div class="content"><div class='highlight'><pre>Polyglot.transformPhrase = transformPhrase;
<span class="hljs-built_in">module</span>.exports = Polyglot;</pre></div></div>
</li>
</ul>
</div>
</body>
</html>