blob: 6d60daec24036c03902328895f1e9fa361ecaefd [file] [log] [blame]
AviZi280f8012017-06-09 02:39:56 +03001/*!
Michael Landoefa037d2017-02-19 12:57:33 +02002 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
AviZi280f8012017-06-09 02:39:56 +03003 *
Michael Landoefa037d2017-02-19 12:57:33 +02004 * 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
AviZi280f8012017-06-09 02:39:56 +03007 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
Michael Landoefa037d2017-02-19 12:57:33 +020010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
AviZi280f8012017-06-09 02:39:56 +030012 * 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.
Michael Landoefa037d2017-02-19 12:57:33 +020015 */
Michael Landoefa037d2017-02-19 12:57:33 +020016import IntlObj from 'intl';
avigaffa00e935f2017-09-10 08:58:51 +030017import IntlMessageFormatObj from 'intl-messageformat';
Michael Landoefa037d2017-02-19 12:57:33 +020018import IntlRelativeFormatObj from 'intl-relativeformat';
19import createFormatCacheObj from 'intl-format-cache';
20import i18nJson from 'i18nJson';
Michael Landoefa037d2017-02-19 12:57:33 +020021/*
avigaffa00e935f2017-09-10 08:58:51 +030022 Intl libs are using out dated transpailer from ecmascript6.
23 * TODO: As soon as they fix it, remove this assignments!!!
24 * */
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020025var Intl = window.Intl || IntlObj.default,
26 IntlMessageFormat = IntlMessageFormatObj.default,
27 IntlRelativeFormat = IntlRelativeFormatObj.default,
andre.schmidead5c382021-08-06 16:04:50 +010028 createFormatCache = createFormatCacheObj;
Michael Landoefa037d2017-02-19 12:57:33 +020029
Michael Landoefa037d2017-02-19 12:57:33 +020030/*extract locale*/
31var _locale = window.localStorage && localStorage.getItem('user_locale');
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020032if (!_locale) {
33 if (window.navigator) {
34 _locale = navigator.language || navigator.userLanguage;
Michael Landoefa037d2017-02-19 12:57:33 +020035
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020036 //For now removing the dashes from the language.
37 let indexOfDash = _locale.indexOf('-');
38 if (-1 !== indexOfDash) {
39 _locale = _locale.substr(0, indexOfDash);
40 }
41 }
42 if (!_locale) {
43 _locale = 'en';
44 }
Michael Landoefa037d2017-02-19 12:57:33 +020045}
46
47var _localeUpper = _locale.toUpperCase();
Michael Landoefa037d2017-02-19 12:57:33 +020048var i18n = {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020049 _locale: _locale,
50 _localeUpper: _localeUpper,
51 _i18nData: i18nJson || {},
Michael Landoefa037d2017-02-19 12:57:33 +020052
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020053 number(num) {
54 return createFormatCache(Intl.NumberFormat)(this._locale).format(num);
55 },
Michael Landoefa037d2017-02-19 12:57:33 +020056
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020057 date(date, options, relativeDates) {
58 if (undefined === relativeDates || relativeDates) {
59 return this.dateRelative(date, options);
60 } else {
61 return this.dateNormal(date, options);
62 }
63 },
Michael Landoefa037d2017-02-19 12:57:33 +020064
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020065 dateNormal(date, options) {
66 return createFormatCache(Intl.DateTimeFormat)(
67 this._locale,
68 options
69 ).format(date);
70 },
Michael Landoefa037d2017-02-19 12:57:33 +020071
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020072 dateRelative(date, options) {
73 return createFormatCache(IntlRelativeFormat)(
74 this._locale,
75 options
76 ).format(date);
77 },
78 message(messageId, options) {
79 let messageTxt = null;
80 if (i18nJson && i18nJson[messageId]) {
81 messageTxt = i18nJson[messageId];
82 } else {
83 messageTxt = String(messageId);
84 }
85 return createFormatCache(IntlMessageFormat)(
86 messageTxt,
87 this._locale
88 ).format(options);
89 },
90 getLocale() {
91 return this._locale;
92 },
93 getLocaleUpper() {
94 return this._localeUpper;
95 },
96 setLocale(locale) {
97 localStorage.setItem('user_locale', locale);
98 window.location.reload();
99 }
Michael Landoefa037d2017-02-19 12:57:33 +0200100};
Michael Landoefa037d2017-02-19 12:57:33 +0200101function i18nWrapper() {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200102 return i18nWrapper.message.apply(i18nWrapper, arguments);
Michael Landoefa037d2017-02-19 12:57:33 +0200103}
104
105/*replace with some kind of extend method*/
106var prop, propKey;
107for (propKey in i18n) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200108 prop = i18n[propKey];
109 if (typeof prop === 'function') {
110 prop = prop.bind(i18nWrapper);
111 }
112 i18nWrapper[propKey] = prop;
Michael Landoefa037d2017-02-19 12:57:33 +0200113}
114
Michael Landoefa037d2017-02-19 12:57:33 +0200115export default i18nWrapper;