Skip to content

Commit

Permalink
conditional require
Browse files Browse the repository at this point in the history
Signed-off-by: andrey <motor4ik@gmail.com>
  • Loading branch information
flexsurfer committed Jul 2, 2020
1 parent 2e7f421 commit 2110446
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/mocks/js_dependencies.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@
"../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/pt_br.json" (js/JSON.parse (slurp "./translations/pt_br.json"))
"../translations/pt_BR.json" (js/JSON.parse (slurp "./translations/pt_BR.json"))
"../translations/ru.json" (js/JSON.parse (slurp "./translations/ru.json"))
"../translations/tr.json" (js/JSON.parse (slurp "./translations/tr.json"))
"../translations/zh.json" (js/JSON.parse (slurp "./translations/zh.json"))
Expand Down
52 changes: 24 additions & 28 deletions src/status_im/i18n_resources.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,52 @@
(def default-device-language
(keyword (.-language react-native-languages)))

;; Update js_dependencies.cljs
(def languages [:ar :de :en :es :es_419 :fil :fr :id :in :it :ko :pt_BR :ru :tr :zh :zh_Hant :zh_TW])
(def languages #{:ar :de :en :es :es_419 :fil :fr :id :in :it :ko :pt_BR :ru :tr :zh :zh_Hant :zh_TW})

(defonce loaded-languages
(atom
(conj #{:en} default-device-language)))

(def translations
{:ar (js/require "../translations/ar.json")
:de (js/require "../translations/de.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")
:pt_BR (js/require "../translations/pt_br.json")
:ru (js/require "../translations/ru.json")
:tr (js/require "../translations/tr.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)
(if (contains? languages 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))
(if (and (> (count parts) 2) (contains? languages short-lang))
short-lang
(when (contains? translations shortest-lang)
(when (contains? languages shortest-lang)
shortest-lang)))))

(defn require-translation [lang-key]
(when-let [lang (valid-language (keyword lang-key))]
(get translations lang)))
(case lang
:ar (js/require "../translations/ar.json")
:de (js/require "../translations/de.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")
:pt_BR (js/require "../translations/pt_BR.json")
:ru (js/require "../translations/ru.json")
:tr (js/require "../translations/tr.json")
:zh (js/require "../translations/zh.json")
:zh_Hant (js/require "../translations/zh_hant.json")
:zh_TW (js/require "../translations/zh_TW.json"))))

;; translations
(def translations-by-locale
(cond->
{:en (require-translation :en)
(cond-> {:en (require-translation :en)}
(not= :en default-device-language)
(assoc default-device-language
(require-translation (-> (name default-device-language)
(string/replace "-" "_")
keyword)))}))
keyword)))))

(defn load-language [lang]
(when-let [lang-key (valid-language (keyword lang))]
Expand Down

0 comments on commit 2110446

Please sign in to comment.