1 | "use strict";
|
2 |
|
3 | const ZERO_CHAR_CODE = '0'.charCodeAt( 0 );
|
4 |
|
5 | exports.lang = function lang( _lang ) {
|
6 | let language = _lang;
|
7 | if ( typeof language === 'undefined' ) {
|
8 | if ( window.localStorage ) {
|
9 | language = window.localStorage.getItem( "Language" );
|
10 | }
|
11 | if ( !language ) {
|
12 | language = window.navigator.language;
|
13 | if ( !language ) {
|
14 | language = window.navigator.browserLanguage;
|
15 | if ( !language ) {
|
16 | language = "fr";
|
17 | }
|
18 | }
|
19 | }
|
20 | language = language.substr( 0, 2 ).toLowerCase();
|
21 | }
|
22 | if ( window.localStorage ) {
|
23 | window.localStorage.setItem( "Language", language );
|
24 | }
|
25 | return language;
|
26 | };
|
27 |
|
28 | exports.intl = function intl( words, params ) {
|
29 | let dic = words[ exports.lang() ];
|
30 |
|
31 | const
|
32 | k = params[ 0 ],
|
33 | defLang = Object.keys( words )[ 0 ];
|
34 | if ( !defLang ) return k;
|
35 |
|
36 | if ( !dic ) {
|
37 | dic = words[ defLang ];
|
38 | if ( !dic ) {
|
39 | return k;
|
40 | }
|
41 | }
|
42 | let txt = dic[ k ];
|
43 | if ( !txt ) {
|
44 | dic = words[ defLang ];
|
45 | txt = dic[ k ];
|
46 | }
|
47 | if ( !txt ) return k;
|
48 | return processArguments( txt, params );
|
49 | };
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | function processArguments( txt, params ) {
|
58 | let output = txt;
|
59 | if ( params.length > 1 ) {
|
60 | let
|
61 | newTxt = "",
|
62 | lastIdx = 0;
|
63 | for ( let i = 0; i < txt.length; i++ ) {
|
64 | const c = txt.charAt( i );
|
65 | if ( c === '$' ) {
|
66 | newTxt += txt.substring( lastIdx, i );
|
67 | i++;
|
68 | const pos = txt.charCodeAt( i ) - ZERO_CHAR_CODE;
|
69 | if ( pos < 0 || pos >= params.length ) {
|
70 | newTxt += `$${txt.charAt(i)}`;
|
71 | } else {
|
72 | newTxt += params[ pos ];
|
73 | }
|
74 | lastIdx = i + 1;
|
75 | } else if ( c === '\\' ) {
|
76 | newTxt += txt.substring( lastIdx, i );
|
77 | i++;
|
78 | newTxt += txt.charAt( i );
|
79 | lastIdx = i + 1;
|
80 | }
|
81 | }
|
82 | newTxt += txt.substr( lastIdx );
|
83 | output = newTxt;
|
84 | }
|
85 | return output;
|
86 | } |
\ | No newline at end of file |