conditional require

Signed-off-by: andrey <motor4ik@gmail.com>
This commit is contained in:
andrey 2020-06-29 12:21:29 +02:00
parent 2e7f4212c1
commit 2110446d29
No known key found for this signature in database
GPG Key ID: 89B67245FD2F0272
2 changed files with 25 additions and 29 deletions

View File

@ -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"))

View File

@ -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))]