mirror of
https://github.com/penpot/penpot.git
synced 2025-12-11 22:14:05 +01:00
✨ Preload default translations
This commit is contained in:
@@ -340,7 +340,7 @@
|
||||
(dfn-diff t2 t1)))
|
||||
|
||||
#?(:cljs
|
||||
(defn set-default-locale!
|
||||
(defn set-default-locale
|
||||
[locale]
|
||||
(when-let [locale (unchecked-get locales locale)]
|
||||
(dfn-set-default-options #js {:locale locale}))))
|
||||
|
||||
@@ -49,7 +49,9 @@
|
||||
<script type="module" src="{{& libs}}"></script>
|
||||
<script type="module">
|
||||
import { init } from "{{& app_main}}";
|
||||
init();
|
||||
import defaultTranslations from "{{& default_translations}}";
|
||||
|
||||
init({defaultTranslations});
|
||||
</script>
|
||||
{{/manifest}}
|
||||
</body>
|
||||
|
||||
@@ -187,7 +187,7 @@ async function readManifestFile(resource) {
|
||||
return JSON.parse(content);
|
||||
}
|
||||
|
||||
async function readShadowManifest() {
|
||||
async function generateManifest() {
|
||||
const index = {
|
||||
app_main: "./js/main.js",
|
||||
render_main: "./js/render.js",
|
||||
@@ -197,6 +197,7 @@ async function readShadowManifest() {
|
||||
polyfills: "./js/polyfills.js?version=" + CURRENT_VERSION,
|
||||
libs: "./js/libs.js?version=" + CURRENT_VERSION,
|
||||
worker_main: "./js/worker/main.js?version=" + CURRENT_VERSION,
|
||||
default_translations: "./js/translation.en.js?version=" + CURRENT_VERSION,
|
||||
|
||||
importmap: JSON.stringify({
|
||||
"imports": {
|
||||
@@ -391,7 +392,7 @@ async function generateTemplates() {
|
||||
const isDebug = process.env.NODE_ENV !== "production";
|
||||
await fs.mkdir("./resources/public/", { recursive: true });
|
||||
|
||||
const manifest = await readShadowManifest();
|
||||
const manifest = await generateManifest();
|
||||
let content;
|
||||
|
||||
const iconsSprite = await fs.readFile(
|
||||
|
||||
@@ -90,7 +90,10 @@
|
||||
(rx/map #(ws/initialize)))))))
|
||||
|
||||
(defn ^:export init
|
||||
[]
|
||||
[options]
|
||||
(some-> (unchecked-get options "defaultTranslations")
|
||||
(i18n/set-default-translations))
|
||||
|
||||
(mw/init!)
|
||||
(i18n/init)
|
||||
(cur/init-styles)
|
||||
|
||||
@@ -98,9 +98,7 @@
|
||||
(def context
|
||||
(atom (d/without-nils (collect-context))))
|
||||
|
||||
(add-watch i18n/state "events"
|
||||
(fn [_ _ _ v]
|
||||
(swap! context assoc :locale (get v :locale))))
|
||||
(add-watch i18n/locale "events" #(swap! context assoc :locale %4))
|
||||
|
||||
;; --- EVENT TRANSLATION
|
||||
|
||||
|
||||
@@ -69,7 +69,6 @@
|
||||
(-> (.-language globals/navigator)
|
||||
(parse-locale))))
|
||||
|
||||
|
||||
;; Set initial translation loading state as globaly stored variable;
|
||||
;; this facilitates hot reloading
|
||||
(when-not (exists? (unchecked-get globals/global "penpotTranslations"))
|
||||
@@ -93,14 +92,8 @@
|
||||
(def ^:dynamic *current-locale*
|
||||
(get-current))
|
||||
|
||||
(defonce state
|
||||
(l/atom {:render 0 :locale *current-locale*}))
|
||||
|
||||
(defn- assign-current-locale
|
||||
[state locale]
|
||||
(-> state
|
||||
(update :render inc)
|
||||
(assoc :locale locale)))
|
||||
(defonce locale
|
||||
(l/atom *current-locale*))
|
||||
|
||||
(defn- get-translations
|
||||
"Get globaly stored mutable object with all loaded translations"
|
||||
@@ -114,6 +107,10 @@
|
||||
(unchecked-set translations locale data)
|
||||
nil))
|
||||
|
||||
(defn set-default-translations
|
||||
[data]
|
||||
(set-translations cf/default-language data))
|
||||
|
||||
(defn- load
|
||||
[locale]
|
||||
(let [path (str "./translation." locale ".js?version=" (:full cf/version))]
|
||||
@@ -122,15 +119,14 @@
|
||||
(p/fnly (fn [data cause]
|
||||
(if cause
|
||||
(js/console.error "unexpected error on fetching locale" cause)
|
||||
(do
|
||||
(set! *current-locale* locale)
|
||||
(set-translations locale data)
|
||||
(swap! state assign-current-locale locale))))))))
|
||||
(set-translations locale data)))))))
|
||||
|
||||
(defn init
|
||||
"Initialize the i18n module"
|
||||
[]
|
||||
(load *current-locale*))
|
||||
(load *current-locale*)
|
||||
(when-not (= *current-locale* cf/default-language)
|
||||
(load cf/default-language)))
|
||||
|
||||
(defn set-locale
|
||||
[lname]
|
||||
@@ -145,7 +141,10 @@
|
||||
(recur (rest locales)))
|
||||
cf/default-language))))]
|
||||
|
||||
(load lname)))
|
||||
(->> (load lname)
|
||||
(p/fnly (fn [_r _c]
|
||||
(set! *current-locale* lname)
|
||||
(reset! locale lname))))))
|
||||
|
||||
(deftype C [val]
|
||||
IDeref
|
||||
@@ -206,9 +205,7 @@
|
||||
:className class
|
||||
:on-click on-click}]))
|
||||
|
||||
(add-watch state "common.time"
|
||||
(add-watch locale "common.time"
|
||||
(fn [_ _ pv cv]
|
||||
(let [pv (get pv :locale)
|
||||
cv (get cv :locale)]
|
||||
(when (not= pv cv)
|
||||
(ct/set-default-locale! cv)))))
|
||||
(when (not= pv cv)
|
||||
(ct/set-default-locale cv))))
|
||||
|
||||
Reference in New Issue
Block a user