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