Add new code new version
[sdc.git] / openecomp-ui / src / nfvo-utils / i18n / i18n.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 import IntlObj from 'intl';
22 import IntlMessageFormatObj from 'intl-messageformat';
23 import IntlRelativeFormatObj from 'intl-relativeformat';
24 import createFormatCacheObj from 'intl-format-cache';
25 import i18nJson from 'i18nJson';
26
27 /*
28         Intl libs are using out dated transpailer from ecmascript6.
29 *  TODO: As soon as they fix it, remove this assignments!!!
30 * */
31 var Intl               = window.Intl || IntlObj.default,
32         IntlMessageFormat  = IntlMessageFormatObj.default,
33         IntlRelativeFormat = IntlRelativeFormatObj.default,
34         createFormatCache  = createFormatCacheObj.default;
35
36 var i18nData;
37
38 if(i18nJson) {
39         i18nData = i18nJson.dataWrapperArr[i18nJson.i18nDataIdx];
40 }
41
42
43 /*extract locale*/
44 var _locale = window.localStorage && localStorage.getItem('user_locale');
45 if(!_locale) {
46         if(window.navigator) {
47                 _locale = navigator.language || navigator.userLanguage;
48
49                 //For now removing the dashes from the language.
50                 let indexOfDash = _locale.indexOf('-');
51                 if(-1 !== indexOfDash) {
52                         _locale = _locale.substr(0, indexOfDash);
53                 }
54         }
55         if(!_locale) {
56                 _locale = 'en';
57         }
58 }
59
60 var _localeUpper = _locale.toUpperCase();
61
62 var i18n = {
63
64         _locale: _locale,
65         _localeUpper: _localeUpper,
66         _i18nData: i18nData || {},
67
68         number(num) {
69                 return createFormatCache(Intl.NumberFormat)(this._locale).format(num);
70         },
71
72         date(date, options, relativeDates) {
73                 if (undefined === relativeDates || relativeDates) {
74                         return this.dateRelative(date, options);
75                 } else {
76                         return this.dateNormal(date, options);
77                 }
78         },
79
80         dateNormal(date, options) {
81                 return createFormatCache(Intl.DateTimeFormat)(this._locale, options).format(date);
82         },
83
84         dateRelative(date, options) {
85                 return createFormatCache(IntlRelativeFormat)(this._locale, options).format(date);
86         },
87
88         message(messageId, options) {
89                 return createFormatCache(IntlMessageFormat)(this._i18nData[messageId] || String(messageId), this._locale).format(options);
90         },
91
92         getLocale() {
93                 return this._locale;
94         },
95
96         getLocaleUpper() {
97                 return this._localeUpper;
98         },
99
100         setLocale(locale) {
101                 localStorage.setItem('user_locale', locale);
102                 window.location.reload();
103         }
104
105 };
106
107 function i18nWrapper() {
108         return i18nWrapper.message.apply(i18nWrapper, arguments);
109 }
110
111 /*replace with some kind of extend method*/
112 var prop, propKey;
113 for (propKey in i18n) {
114         prop = i18n[propKey];
115         if (typeof prop === 'function') {
116                 prop = prop.bind(i18nWrapper);
117         }
118         i18nWrapper[propKey] = prop;
119 }
120
121
122 export default i18nWrapper;