2016-05-25 04:27:03 +03:00
|
|
|
(ns status-im.i18n
|
|
|
|
(:require
|
2018-05-08 11:32:28 +03:00
|
|
|
[status-im.react-native.js-dependencies :as rn-dependencies]
|
|
|
|
[clojure.string :as string]
|
2019-06-01 09:47:37 +03:00
|
|
|
[status-im.i18n-resources :as i18n-resources]))
|
2016-05-25 04:27:03 +03:00
|
|
|
|
2019-06-01 09:47:37 +03:00
|
|
|
(set! (.-locale rn-dependencies/i18n) (name i18n-resources/default-device-language))
|
2017-07-16 12:04:35 +03:00
|
|
|
(set! (.-fallbacks rn-dependencies/i18n) true)
|
|
|
|
(set! (.-defaultSeparator rn-dependencies/i18n) "/")
|
2016-05-25 04:27:03 +03:00
|
|
|
|
2017-08-11 12:38:21 +03:00
|
|
|
(set! (.-translations rn-dependencies/i18n)
|
2019-06-01 09:47:37 +03:00
|
|
|
(clj->js i18n-resources/translations-by-locale))
|
|
|
|
|
|
|
|
(defn set-language [lang]
|
|
|
|
(i18n-resources/load-language lang)
|
|
|
|
(set! (.-locale rn-dependencies/i18n) lang))
|
2016-05-25 04:27:03 +03:00
|
|
|
|
2017-10-19 06:38:20 +02:00
|
|
|
;;:zh, :zh-hans-xx, :zh-hant-xx have been added until this bug will be fixed https://github.com/fnando/i18n-js/issues/460
|
2017-06-27 14:34:12 +03:00
|
|
|
|
2017-01-23 18:57:40 +03:00
|
|
|
(def delimeters
|
|
|
|
"This function is a hack: mobile Safari doesn't support toLocaleString(), so we need to pass
|
|
|
|
this map to WKWebView to make number formatting work."
|
2017-02-02 09:51:26 +03:00
|
|
|
(let [n (.toLocaleString (js/Number 1000.1))
|
|
|
|
delimiter? (= (count n) 7)]
|
|
|
|
(if delimiter?
|
|
|
|
{:delimiter (subs n 1 2)
|
|
|
|
:separator (subs n 5 6)}
|
|
|
|
{:delimiter ""
|
|
|
|
:separator (subs n 4 5)})))
|
2017-01-23 18:57:40 +03:00
|
|
|
|
|
|
|
(defn label-number [number]
|
|
|
|
(when number
|
|
|
|
(let [{:keys [delimiter separator]} delimeters]
|
2017-07-16 12:04:35 +03:00
|
|
|
(.toNumber rn-dependencies/i18n
|
2017-11-23 09:20:04 +01:00
|
|
|
(string/replace number #"," ".")
|
2017-01-23 18:57:40 +03:00
|
|
|
(clj->js {:precision 10
|
|
|
|
:strip_insignificant_zeros true
|
|
|
|
:delimiter delimiter
|
|
|
|
:separator separator})))))
|
|
|
|
|
2017-10-06 13:50:57 +02:00
|
|
|
(def default-option-value "<no value>")
|
|
|
|
|
|
|
|
(defn label-options [options]
|
|
|
|
;; i18n ignores nil value, leading to misleading messages
|
|
|
|
(into {} (for [[k v] options] [k (or v default-option-value)])))
|
|
|
|
|
2016-06-02 15:48:57 +03:00
|
|
|
(defn label
|
|
|
|
([path] (label path {}))
|
|
|
|
([path options]
|
2017-07-16 12:04:35 +03:00
|
|
|
(if (exists? rn-dependencies/i18n.t)
|
2017-01-23 18:57:40 +03:00
|
|
|
(let [options (update options :amount label-number)]
|
2017-10-06 13:50:57 +02:00
|
|
|
(.t rn-dependencies/i18n (name path) (clj->js (label-options options))))
|
2016-06-10 03:21:48 +03:00
|
|
|
(name path))))
|
2016-06-01 14:35:22 +03:00
|
|
|
|
2016-06-01 14:44:04 +03:00
|
|
|
(defn label-pluralize [count path & options]
|
2017-07-16 12:04:35 +03:00
|
|
|
(if (exists? rn-dependencies/i18n.t)
|
|
|
|
(.p rn-dependencies/i18n count (name path) (clj->js options))
|
2016-08-24 10:29:40 +03:00
|
|
|
(name path)))
|
|
|
|
|
|
|
|
(defn message-status-label [status]
|
|
|
|
(->> status
|
|
|
|
(name)
|
|
|
|
(str "t/status-")
|
|
|
|
(keyword)
|
2016-10-24 23:46:06 +03:00
|
|
|
(label)))
|
2016-12-28 13:00:21 +03:00
|
|
|
|
|
|
|
(def locale
|
2017-07-16 12:04:35 +03:00
|
|
|
(.-locale rn-dependencies/i18n))
|
2016-12-28 13:00:21 +03:00
|
|
|
|
2018-07-16 15:23:57 +03:00
|
|
|
(defn format-currency
|
|
|
|
([value currency-code]
|
|
|
|
(format-currency value currency-code true))
|
|
|
|
([value currency-code currency-symbol?]
|
2019-05-29 19:52:41 +03:00
|
|
|
(.addTier2Support goog/i18n.currency)
|
|
|
|
(let [currency-code-to-nfs-map {"ZAR" goog/i18n.NumberFormatSymbols_af
|
|
|
|
"ETB" goog/i18n.NumberFormatSymbols_am
|
|
|
|
"EGP" goog/i18n.NumberFormatSymbols_ar
|
|
|
|
"DZD" goog/i18n.NumberFormatSymbols_ar_DZ
|
|
|
|
"AZN" goog/i18n.NumberFormatSymbols_az
|
|
|
|
"BYN" goog/i18n.NumberFormatSymbols_be
|
|
|
|
"BGN" goog/i18n.NumberFormatSymbols_bg
|
|
|
|
"BDT" goog/i18n.NumberFormatSymbols_bn
|
|
|
|
"EUR" goog/i18n.NumberFormatSymbols_br
|
|
|
|
"BAM" goog/i18n.NumberFormatSymbols_bs
|
|
|
|
"USD" goog/i18n.NumberFormatSymbols_en
|
|
|
|
"CZK" goog/i18n.NumberFormatSymbols_cs
|
|
|
|
"GBP" goog/i18n.NumberFormatSymbols_cy
|
|
|
|
"DKK" goog/i18n.NumberFormatSymbols_da
|
|
|
|
"CHF" goog/i18n.NumberFormatSymbols_de_CH
|
|
|
|
"AUD" goog/i18n.NumberFormatSymbols_en_AU
|
|
|
|
"CAD" goog/i18n.NumberFormatSymbols_en_CA
|
|
|
|
"INR" goog/i18n.NumberFormatSymbols_en_IN
|
|
|
|
"SGD" goog/i18n.NumberFormatSymbols_en_SG
|
|
|
|
"MXN" goog/i18n.NumberFormatSymbols_es_419
|
|
|
|
"IRR" goog/i18n.NumberFormatSymbols_fa
|
|
|
|
"PHP" goog/i18n.NumberFormatSymbols_fil
|
|
|
|
"ILS" goog/i18n.NumberFormatSymbols_he
|
|
|
|
"HRK" goog/i18n.NumberFormatSymbols_hr
|
|
|
|
"HUF" goog/i18n.NumberFormatSymbols_hu
|
|
|
|
"AMD" goog/i18n.NumberFormatSymbols_hy
|
|
|
|
"IDR" goog/i18n.NumberFormatSymbols_id
|
|
|
|
"ISK" goog/i18n.NumberFormatSymbols_is
|
|
|
|
"JPY" goog/i18n.NumberFormatSymbols_ja
|
|
|
|
"GEL" goog/i18n.NumberFormatSymbols_ka
|
|
|
|
"KZT" goog/i18n.NumberFormatSymbols_kk
|
|
|
|
"KHR" goog/i18n.NumberFormatSymbols_km
|
|
|
|
"KRW" goog/i18n.NumberFormatSymbols_ko
|
|
|
|
"KGS" goog/i18n.NumberFormatSymbols_ky
|
|
|
|
"CDF" goog/i18n.NumberFormatSymbols_ln
|
|
|
|
"LAK" goog/i18n.NumberFormatSymbols_lo
|
|
|
|
"MKD" goog/i18n.NumberFormatSymbols_mk
|
|
|
|
"MNT" goog/i18n.NumberFormatSymbols_mn
|
|
|
|
"MDL" goog/i18n.NumberFormatSymbols_mo
|
|
|
|
"MYR" goog/i18n.NumberFormatSymbols_ms
|
|
|
|
"MMK" goog/i18n.NumberFormatSymbols_my
|
|
|
|
"NOK" goog/i18n.NumberFormatSymbols_nb
|
|
|
|
"NPR" goog/i18n.NumberFormatSymbols_ne
|
|
|
|
"PLN" goog/i18n.NumberFormatSymbols_pl
|
|
|
|
"BRL" goog/i18n.NumberFormatSymbols_pt
|
|
|
|
"RON" goog/i18n.NumberFormatSymbols_ro
|
|
|
|
"RUB" goog/i18n.NumberFormatSymbols_ru
|
|
|
|
"RSD" goog/i18n.NumberFormatSymbols_sh
|
|
|
|
"LKR" goog/i18n.NumberFormatSymbols_si
|
|
|
|
"ALL" goog/i18n.NumberFormatSymbols_sq
|
|
|
|
"SEK" goog/i18n.NumberFormatSymbols_sv
|
|
|
|
"TZS" goog/i18n.NumberFormatSymbols_sw
|
|
|
|
"THB" goog/i18n.NumberFormatSymbols_th
|
|
|
|
"TRY" goog/i18n.NumberFormatSymbols_tr
|
|
|
|
"UAH" goog/i18n.NumberFormatSymbols_uk
|
|
|
|
"PKR" goog/i18n.NumberFormatSymbols_ur
|
|
|
|
"UZS" goog/i18n.NumberFormatSymbols_uz
|
|
|
|
"VND" goog/i18n.NumberFormatSymbols_vi
|
|
|
|
"CNY" goog/i18n.NumberFormatSymbols_zh
|
|
|
|
"HKD" goog/i18n.NumberFormatSymbols_zh_HK
|
|
|
|
"TWD" goog/i18n.NumberFormatSymbols_zh_TW}
|
2018-07-16 15:23:57 +03:00
|
|
|
nfs (or (get currency-code-to-nfs-map currency-code)
|
2019-05-29 19:52:41 +03:00
|
|
|
goog/i18n.NumberFormatSymbols_en)]
|
|
|
|
(set! goog/i18n.NumberFormatSymbols
|
2018-07-16 15:23:57 +03:00
|
|
|
(if currency-symbol?
|
|
|
|
nfs
|
|
|
|
(-> nfs
|
|
|
|
(js->clj :keywordize-keys true)
|
|
|
|
;; Remove any currency symbol placeholders in the pattern
|
2019-05-29 19:52:41 +03:00
|
|
|
(update :CURRENCY_PATTERN (fn [pat]
|
|
|
|
(string/replace pat #"\s*¤\s*" "")))
|
2018-07-16 15:23:57 +03:00
|
|
|
clj->js)))
|
|
|
|
(.format
|
2019-05-29 19:52:41 +03:00
|
|
|
(new goog/i18n.NumberFormat goog/i18n.NumberFormat.Format.CURRENCY currency-code)
|
2018-07-16 15:23:57 +03:00
|
|
|
value))))
|
|
|
|
|