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'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 17 | import IntlRelativeFormatObj from 'intl-relativeformat'; |
| 18 | import createFormatCacheObj from 'intl-format-cache'; |
| 19 | import i18nJson from 'i18nJson'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 20 | /* |
| 21 | Intl libs are using out dated transpailer from ecmascript6. |
| 22 | * TODO: As soon as they fix it, remove this assignments!!! |
| 23 | * */ |
| 24 | var Intl = window.Intl || IntlObj.default, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 25 | IntlRelativeFormat = IntlRelativeFormatObj.default, |
| 26 | createFormatCache = createFormatCacheObj.default; |
| 27 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 28 | /*extract locale*/ |
| 29 | var _locale = window.localStorage && localStorage.getItem('user_locale'); |
| 30 | if(!_locale) { |
| 31 | if(window.navigator) { |
| 32 | _locale = navigator.language || navigator.userLanguage; |
| 33 | |
| 34 | //For now removing the dashes from the language. |
| 35 | let indexOfDash = _locale.indexOf('-'); |
| 36 | if(-1 !== indexOfDash) { |
| 37 | _locale = _locale.substr(0, indexOfDash); |
| 38 | } |
| 39 | } |
| 40 | if(!_locale) { |
| 41 | _locale = 'en'; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | var _localeUpper = _locale.toUpperCase(); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 46 | var i18n = { |
| 47 | |
| 48 | _locale: _locale, |
| 49 | _localeUpper: _localeUpper, |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 50 | _i18nData: i18nJson || {}, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 51 | |
| 52 | number(num) { |
| 53 | return createFormatCache(Intl.NumberFormat)(this._locale).format(num); |
| 54 | }, |
| 55 | |
| 56 | date(date, options, relativeDates) { |
| 57 | if (undefined === relativeDates || relativeDates) { |
| 58 | return this.dateRelative(date, options); |
| 59 | } else { |
| 60 | return this.dateNormal(date, options); |
| 61 | } |
| 62 | }, |
| 63 | |
| 64 | dateNormal(date, options) { |
| 65 | return createFormatCache(Intl.DateTimeFormat)(this._locale, options).format(date); |
| 66 | }, |
| 67 | |
| 68 | dateRelative(date, options) { |
| 69 | return createFormatCache(IntlRelativeFormat)(this._locale, options).format(date); |
| 70 | }, |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 71 | message(messageId) { |
| 72 | if (i18nJson && i18nJson[messageId]) { |
| 73 | return i18nJson[messageId]; |
| 74 | } |
| 75 | return messageId; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 76 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 77 | getLocale() { |
| 78 | return this._locale; |
| 79 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 80 | getLocaleUpper() { |
| 81 | return this._localeUpper; |
| 82 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 83 | setLocale(locale) { |
| 84 | localStorage.setItem('user_locale', locale); |
| 85 | window.location.reload(); |
| 86 | } |
| 87 | |
| 88 | }; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 89 | function i18nWrapper() { |
| 90 | return i18nWrapper.message.apply(i18nWrapper, arguments); |
| 91 | } |
| 92 | |
| 93 | /*replace with some kind of extend method*/ |
| 94 | var prop, propKey; |
| 95 | for (propKey in i18n) { |
| 96 | prop = i18n[propKey]; |
| 97 | if (typeof prop === 'function') { |
| 98 | prop = prop.bind(i18nWrapper); |
| 99 | } |
| 100 | i18nWrapper[propKey] = prop; |
| 101 | } |
| 102 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 103 | export default i18nWrapper; |