From abfe0869f3784775f7ebc2674b7f5915fedb5984 Mon Sep 17 00:00:00 2001 From: yenda Date: Thu, 7 May 2020 14:28:24 +0200 Subject: [PATCH] requiring translations inline Signed-off-by: yenda --- .../android/targets/release-android.nix | 2 +- src/mocks/js_dependencies.cljs | 14 ++++ src/status_im/bootnodes/core_test.cljs | 2 +- src/status_im/contact/db_test.cljs | 2 +- src/status_im/i18n.clj | 21 ------ src/status_im/i18n_resources.cljs | 66 +++++++++++++++---- src/status_im/i18n_test.cljs | 7 +- .../multiaccounts/login/data_test.cljs | 2 +- .../multiaccounts/login/flow_test.cljs | 4 +- 9 files changed, 78 insertions(+), 42 deletions(-) delete mode 100644 src/status_im/i18n.clj diff --git a/nix/mobile/android/targets/release-android.nix b/nix/mobile/android/targets/release-android.nix index a3c945b2c2..d1cc0de968 100644 --- a/nix/mobile/android/targets/release-android.nix +++ b/nix/mobile/android/targets/release-android.nix @@ -59,7 +59,7 @@ in stdenv.mkDerivation rec { filter = lib.mkFilter { root = path; include = [ - "mobile/js_files.*" "resources/.*" + "mobile/js_files.*" "resources/.*" "translations/.*" "modules/react-native-status/android.*" envFileName "VERSION" ".watchmanconfig" "status-go-version.json" "react-native.config.js" diff --git a/src/mocks/js_dependencies.cljs b/src/mocks/js_dependencies.cljs index 0d02f9a043..6b64939c91 100644 --- a/src/mocks/js_dependencies.cljs +++ b/src/mocks/js_dependencies.cljs @@ -1,4 +1,5 @@ (ns mocks.js-dependencies + (:require-macros [status-im.utils.slurp :refer [slurp]]) (:require [status-im.default-fleet :refer (default-fleets)])) (def action-button #js {:default #js {:Item #js {}}}) @@ -190,4 +191,17 @@ "react-native-mail" react-native-mail "react-native-image-resizer" image-resizer "./fleets.js" default-fleets + "../translations/ar.json" (js/JSON.parse (slurp "./translations/ar.json")) + "../translations/en.json" (js/JSON.parse (slurp "./translations/en.json")) + "../translations/es.json" (js/JSON.parse (slurp "./translations/es.json")) + "../translations/es_419.json" (js/JSON.parse (slurp "./translations/es_419.json")) + "../translations/fil.json" (js/JSON.parse (slurp "./translations/fil.json")) + "../translations/fr.json" (js/JSON.parse (slurp "./translations/fr.json")) + "../translations/id.json" (js/JSON.parse (slurp "./translations/id.json")) + "../translations/it.json" (js/JSON.parse (slurp "./translations/it.json")) + "../translations/ko.json" (js/JSON.parse (slurp "./translations/ko.json")) + "../translations/ru.json" (js/JSON.parse (slurp "./translations/ru.json")) + "../translations/zh.json" (js/JSON.parse (slurp "./translations/zh.json")) + "../translations/zh_hant.json" (js/JSON.parse (slurp "./translations/zh_hant.json")) + "../translations/zh_TW.json" (js/JSON.parse (slurp "./translations/zh_TW.json")) nil)) diff --git a/src/status_im/bootnodes/core_test.cljs b/src/status_im/bootnodes/core_test.cljs index 8218272dc4..a2ed78fdab 100644 --- a/src/status_im/bootnodes/core_test.cljs +++ b/src/status_im/bootnodes/core_test.cljs @@ -1,4 +1,4 @@ -(ns status-im.bootnode.core-test +(ns status-im.bootnodes.core-test (:require [cljs.test :refer-macros [deftest is testing]] [status-im.bootnodes.core :as model])) diff --git a/src/status_im/contact/db_test.cljs b/src/status_im/contact/db_test.cljs index a8a7f62a72..f38ec70dca 100644 --- a/src/status_im/contact/db_test.cljs +++ b/src/status_im/contact/db_test.cljs @@ -1,4 +1,4 @@ -(ns status-im.contacts.db-test +(ns status-im.contact.db-test (:require [cljs.test :refer-macros [deftest is testing]] [status-im.utils.gfycat.core :as gfycat] [status-im.utils.identicon :as identicon] diff --git a/src/status_im/i18n.clj b/src/status_im/i18n.clj deleted file mode 100644 index e5d57be71d..0000000000 --- a/src/status_im/i18n.clj +++ /dev/null @@ -1,21 +0,0 @@ -(ns status-im.i18n - (:require [clojure.string :as string])) - -(defn read-file [f] - (when (.isFile f) - (let [locale (-> f - .getName - (string/split #"\.") - first) - content (slurp f)] - [(keyword locale) content]))) - -(defn read-translations [] - (->> - (java.io.File. "translations") - file-seq - (keep read-file) - vec)) - -(defmacro translations [languages] - (select-keys (into {} (read-translations)) languages)) diff --git a/src/status_im/i18n_resources.cljs b/src/status_im/i18n_resources.cljs index 4c0806c2ff..8bd6a9a9fd 100644 --- a/src/status_im/i18n_resources.cljs +++ b/src/status_im/i18n_resources.cljs @@ -1,21 +1,63 @@ (ns status-im.i18n-resources - (:require-macros [status-im.i18n :as i18n]) - (:require [status-im.utils.types :as types] - [clojure.string :as string] + (:require [clojure.string :as string] + ["i18n-js" :as i18n-js] ["react-native-languages" :default react-native-languages])) (def default-device-language (keyword (.-language react-native-languages))) +(def languages [:ar :en :es :es_419 :fil :fr :id :in :it :ko :ru :zh :zh_Hant :zh_TW]) + +(defonce loaded-languages + (atom + (conj #{:en} default-device-language))) + +(def translations + { + :ar (js/require "../translations/ar.json") + :en (js/require "../translations/en.json") + :es (js/require "../translations/es.json") + :es_419 (js/require "../translations/es_419.json") + :fil (js/require "../translations/fil.json") + :fr (js/require "../translations/fr.json") + :id (js/require "../translations/id.json") + :in (js/require "../translations/id.json") + :it (js/require "../translations/it.json") + :ko (js/require "../translations/ko.json") + :ru (js/require "../translations/ru.json") + :zh (js/require "../translations/zh.json") + :zh_Hant (js/require "../translations/zh_hant.json") + :zh_TW (js/require "../translations/zh_TW.json")}) + +(defn valid-language [lang] + (if (contains? translations lang) + (keyword lang) + (let [parts (string/split (name lang) #"[\-\_]") + short-lang (keyword (str (first parts) "_" (second parts))) + shortest-lang (keyword (first parts))] + (if (and (> (count parts) 2) (contains? translations short-lang)) + short-lang + (when (contains? translations shortest-lang) + shortest-lang))))) + +(defn require-translation [lang-key] + (when-let [lang (valid-language (keyword lang-key))] + (get translations lang))) + ;; translations (def translations-by-locale - (->> (i18n/translations [:ar :en :es :es_419 :fil :fr :it :ko :ru :in :id :tr :zh :zh_Hant :zh_TW]) - (map (fn [[k t]] - (let [k' (-> (name k) - (string/replace "_" "-") - keyword)] - [k' (types/json->clj t)]))) - (into {}))) + (cond-> + {:en (require-translation :en)} + (not= :en default-device-language) + (assoc default-device-language + (require-translation (-> (name default-device-language) + (string/replace "-" "_") + keyword))))) -;; API compatibility -(defn load-language [_]) +(defn load-language [lang] + (when-let [lang-key (valid-language (keyword lang))] + (when-not (contains? @loaded-languages lang-key) + (aset (.-translations i18n-js) + lang + (require-translation lang-key)) + (swap! loaded-languages conj lang-key)))) diff --git a/src/status_im/i18n_test.cljs b/src/status_im/i18n_test.cljs index b4b1192484..5cbd36b5e2 100644 --- a/src/status_im/i18n_test.cljs +++ b/src/status_im/i18n_test.cljs @@ -7,14 +7,15 @@ [clojure.string :as string])) ;; english as source of truth -(def labels (set (keys (:en i18n-resources/translations-by-locale)))) +(def labels (set (keys (js->clj (:en i18n-resources/translations-by-locale) + :keywordize-keys true)))) (spec/def ::label labels) (spec/def ::labels (spec/coll-of ::label :kind set? :into #{})) (defn labels-for-all-locales [] (->> i18n-resources/translations-by-locale - (mapcat #(-> % val keys)) + (mapcat #(-> % val (js->clj :keywordize-keys true) keys)) set)) ;; checkpoints @@ -1030,7 +1031,7 @@ (spec/def ::locales (spec/coll-of ::locale :kind set? :into #{})) (defn locale->labels [locale] - (-> i18n-resources/translations-by-locale (get locale) keys set)) + (-> i18n-resources/translations-by-locale (get locale) (js->clj :keywordize-keys true) keys set)) (defn locale->checkpoint [locale] (let [locale-labels (locale->labels locale) diff --git a/src/status_im/multiaccounts/login/data_test.cljs b/src/status_im/multiaccounts/login/data_test.cljs index c8c153f659..3b68d0abd9 100644 --- a/src/status_im/multiaccounts/login/data_test.cljs +++ b/src/status_im/multiaccounts/login/data_test.cljs @@ -1,4 +1,4 @@ -(ns status-im.multiaccount.login.data-test) +(ns status-im.multiaccounts.login.data-test) (def all-contacts [{:last-updated 1547185503000 diff --git a/src/status_im/multiaccounts/login/flow_test.cljs b/src/status_im/multiaccounts/login/flow_test.cljs index bf1bbf30ca..6405c75c94 100644 --- a/src/status_im/multiaccounts/login/flow_test.cljs +++ b/src/status_im/multiaccounts/login/flow_test.cljs @@ -1,11 +1,11 @@ -(ns status-im.multiaccount.login.flow-test +(ns status-im.multiaccounts.login.flow-test "The main purpose of these tests is to signal that some steps of the sign in flow has been changed. Such changes should be reflected in both these tests and documents which describe the whole \"sign in\" flow." (:require [cljs.test :refer-macros [deftest is testing]] [status-im.ethereum.core :as ethereum] [status-im.ethereum.json-rpc :as json-rpc] - [status-im.multiaccount.login.data-test :as data] + [status-im.multiaccounts.login.data-test :as data] [status-im.multiaccounts.login.core :as login.core])) (deftest on-password-input-submitted