2 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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';
22 Intl libs are using out dated transpailer from ecmascript6.
23 * TODO: As soon as they fix it, remove this assignments!!!
25 var Intl = window.Intl || IntlObj.default,
26 IntlMessageFormat = IntlMessageFormatObj.default,
27 IntlRelativeFormat = IntlRelativeFormatObj.default,
28 createFormatCache = createFormatCacheObj;
31 var _locale = window.localStorage && localStorage.getItem('user_locale');
33 if (window.navigator) {
34 _locale = navigator.language || navigator.userLanguage;
36 //For now removing the dashes from the language.
37 let indexOfDash = _locale.indexOf('-');
38 if (-1 !== indexOfDash) {
39 _locale = _locale.substr(0, indexOfDash);
47 var _localeUpper = _locale.toUpperCase();
50 _localeUpper: _localeUpper,
51 _i18nData: i18nJson || {},
54 return createFormatCache(Intl.NumberFormat)(this._locale).format(num);
57 date(date, options, relativeDates) {
58 if (undefined === relativeDates || relativeDates) {
59 return this.dateRelative(date, options);
61 return this.dateNormal(date, options);
65 dateNormal(date, options) {
66 return createFormatCache(Intl.DateTimeFormat)(
72 dateRelative(date, options) {
73 return createFormatCache(IntlRelativeFormat)(
78 message(messageId, options) {
79 let messageTxt = null;
80 if (i18nJson && i18nJson[messageId]) {
81 messageTxt = i18nJson[messageId];
83 messageTxt = String(messageId);
85 return createFormatCache(IntlMessageFormat)(
94 return this._localeUpper;
97 localStorage.setItem('user_locale', locale);
98 window.location.reload();
101 function i18nWrapper() {
102 return i18nWrapper.message.apply(i18nWrapper, arguments);
105 /*replace with some kind of extend method*/
107 for (propKey in i18n) {
108 prop = i18n[propKey];
109 if (typeof prop === 'function') {
110 prop = prop.bind(i18nWrapper);
112 i18nWrapper[propKey] = prop;
115 export default i18nWrapper;