AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 1 | /*! |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 2 | * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 3 | * |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 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 |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 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. |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 15 | */ |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 16 | import IntlObj from 'intl'; |
avigaffa | 00e935f | 2017-09-10 08:58:51 +0300 | [diff] [blame] | 17 | import IntlMessageFormatObj from 'intl-messageformat'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 18 | import IntlRelativeFormatObj from 'intl-relativeformat'; |
| 19 | import createFormatCacheObj from 'intl-format-cache'; |
| 20 | import i18nJson from 'i18nJson'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 21 | /* |
avigaffa | 00e935f | 2017-09-10 08:58:51 +0300 | [diff] [blame] | 22 | Intl libs are using out dated transpailer from ecmascript6. |
| 23 | * TODO: As soon as they fix it, remove this assignments!!! |
| 24 | * */ |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 25 | var Intl = window.Intl || IntlObj.default, |
avigaffa | 00e935f | 2017-09-10 08:58:51 +0300 | [diff] [blame] | 26 | IntlMessageFormat = IntlMessageFormatObj.default, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 27 | IntlRelativeFormat = IntlRelativeFormatObj.default, |
| 28 | createFormatCache = createFormatCacheObj.default; |
| 29 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 30 | /*extract locale*/ |
| 31 | var _locale = window.localStorage && localStorage.getItem('user_locale'); |
| 32 | if(!_locale) { |
| 33 | if(window.navigator) { |
| 34 | _locale = navigator.language || navigator.userLanguage; |
| 35 | |
| 36 | //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 | } |
| 45 | } |
| 46 | |
| 47 | var _localeUpper = _locale.toUpperCase(); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 48 | var i18n = { |
| 49 | |
| 50 | _locale: _locale, |
| 51 | _localeUpper: _localeUpper, |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 52 | _i18nData: i18nJson || {}, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 53 | |
| 54 | number(num) { |
| 55 | return createFormatCache(Intl.NumberFormat)(this._locale).format(num); |
| 56 | }, |
| 57 | |
| 58 | date(date, options, relativeDates) { |
| 59 | if (undefined === relativeDates || relativeDates) { |
| 60 | return this.dateRelative(date, options); |
| 61 | } else { |
| 62 | return this.dateNormal(date, options); |
| 63 | } |
| 64 | }, |
| 65 | |
| 66 | dateNormal(date, options) { |
| 67 | return createFormatCache(Intl.DateTimeFormat)(this._locale, options).format(date); |
| 68 | }, |
| 69 | |
| 70 | dateRelative(date, options) { |
| 71 | return createFormatCache(IntlRelativeFormat)(this._locale, options).format(date); |
| 72 | }, |
avigaffa | 00e935f | 2017-09-10 08:58:51 +0300 | [diff] [blame] | 73 | message(messageId, options) { |
| 74 | let messageTxt = null; |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 75 | if (i18nJson && i18nJson[messageId]) { |
avigaffa | 00e935f | 2017-09-10 08:58:51 +0300 | [diff] [blame] | 76 | messageTxt = i18nJson[messageId]; |
| 77 | } else { |
| 78 | messageTxt = String(messageId); |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 79 | } |
avigaffa | 00e935f | 2017-09-10 08:58:51 +0300 | [diff] [blame] | 80 | return createFormatCache(IntlMessageFormat)(messageTxt, this._locale).format(options); |
| 81 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 82 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 83 | getLocale() { |
| 84 | return this._locale; |
| 85 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 86 | getLocaleUpper() { |
| 87 | return this._localeUpper; |
| 88 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 89 | setLocale(locale) { |
| 90 | localStorage.setItem('user_locale', locale); |
| 91 | window.location.reload(); |
| 92 | } |
| 93 | |
| 94 | }; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 95 | function i18nWrapper() { |
| 96 | return i18nWrapper.message.apply(i18nWrapper, arguments); |
| 97 | } |
| 98 | |
| 99 | /*replace with some kind of extend method*/ |
| 100 | var prop, propKey; |
| 101 | for (propKey in i18n) { |
| 102 | prop = i18n[propKey]; |
| 103 | if (typeof prop === 'function') { |
| 104 | prop = prop.bind(i18nWrapper); |
| 105 | } |
| 106 | i18nWrapper[propKey] = prop; |
| 107 | } |
| 108 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 109 | export default i18nWrapper; |