status-mobile/src/status_im/i18n.cljs

73 lines
2.4 KiB
Clojure

(ns status-im.i18n
(:require
[status-im.react-native.js-dependencies :as rn-dependencies]
[clojure.string :as string]
[status-im.i18n-resources :as i18n-resources]
[status-im.goog.i18n-module :as goog.i18n]))
(set! (.-locale rn-dependencies/i18n) (name i18n-resources/default-device-language))
(set! (.-fallbacks rn-dependencies/i18n) true)
(set! (.-defaultSeparator rn-dependencies/i18n) "/")
(set! (.-translations rn-dependencies/i18n)
(clj->js i18n-resources/translations-by-locale))
(defn set-language [lang]
(i18n-resources/load-language lang)
(set! (.-locale rn-dependencies/i18n) lang))
;;:zh, :zh-hans-xx, :zh-hant-xx have been added until this bug will be fixed https://github.com/fnando/i18n-js/issues/460
(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."
(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)})))
(defn label-number [number]
(when number
(let [{:keys [delimiter separator]} delimeters]
(.toNumber rn-dependencies/i18n
(string/replace number #"," ".")
(clj->js {:precision 10
:strip_insignificant_zeros true
:delimiter delimiter
:separator separator})))))
(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)])))
(defn label
([path] (label path {}))
([path options]
(if (exists? rn-dependencies/i18n.t)
(let [options (update options :amount label-number)]
(.t rn-dependencies/i18n (name path) (clj->js (label-options options))))
(name path))))
(defn label-pluralize [count path & options]
(if (exists? rn-dependencies/i18n.t)
(.p rn-dependencies/i18n count (name path) (clj->js options))
(name path)))
(defn message-status-label [status]
(->> status
(name)
(str "t/status-")
(keyword)
(label)))
(def locale
(.-locale rn-dependencies/i18n))
(def format-currency goog.i18n/format-currency)