2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
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';
28 Intl libs are using out dated transpailer from ecmascript6.
29 * TODO: As soon as they fix it, remove this assignments!!!
31 var Intl = window.Intl || IntlObj.default,
32 IntlMessageFormat = IntlMessageFormatObj.default,
33 IntlRelativeFormat = IntlRelativeFormatObj.default,
34 createFormatCache = createFormatCacheObj.default;
39 i18nData = i18nJson.dataWrapperArr[i18nJson.i18nDataIdx];
44 var _locale = window.localStorage && localStorage.getItem('user_locale');
46 if(window.navigator) {
47 _locale = navigator.language || navigator.userLanguage;
49 //For now removing the dashes from the language.
50 let indexOfDash = _locale.indexOf('-');
51 if(-1 !== indexOfDash) {
52 _locale = _locale.substr(0, indexOfDash);
60 var _localeUpper = _locale.toUpperCase();
65 _localeUpper: _localeUpper,
66 _i18nData: i18nData || {},
69 return createFormatCache(Intl.NumberFormat)(this._locale).format(num);
72 date(date, options, relativeDates) {
73 if (undefined === relativeDates || relativeDates) {
74 return this.dateRelative(date, options);
76 return this.dateNormal(date, options);
80 dateNormal(date, options) {
81 return createFormatCache(Intl.DateTimeFormat)(this._locale, options).format(date);
84 dateRelative(date, options) {
85 return createFormatCache(IntlRelativeFormat)(this._locale, options).format(date);
88 message(messageId, options) {
89 return createFormatCache(IntlMessageFormat)(this._i18nData[messageId] || String(messageId), this._locale).format(options);
97 return this._localeUpper;
101 localStorage.setItem('user_locale', locale);
102 window.location.reload();
107 function i18nWrapper() {
108 return i18nWrapper.message.apply(i18nWrapper, arguments);
111 /*replace with some kind of extend method*/
113 for (propKey in i18n) {
114 prop = i18n[propKey];
115 if (typeof prop === 'function') {
116 prop = prop.bind(i18nWrapper);
118 i18nWrapper[propKey] = prop;
122 export default i18nWrapper;