Preload default translations

This commit is contained in:
Andrey Antukh
2025-12-10 14:37:50 +01:00
parent 0604485d1f
commit eeabf02820
6 changed files with 29 additions and 28 deletions

View File

@@ -340,7 +340,7 @@
(dfn-diff t2 t1))) (dfn-diff t2 t1)))
#?(:cljs #?(:cljs
(defn set-default-locale! (defn set-default-locale
[locale] [locale]
(when-let [locale (unchecked-get locales locale)] (when-let [locale (unchecked-get locales locale)]
(dfn-set-default-options #js {:locale locale})))) (dfn-set-default-options #js {:locale locale}))))

View File

@@ -49,7 +49,9 @@
<script type="module" src="{{& libs}}"></script> <script type="module" src="{{& libs}}"></script>
<script type="module"> <script type="module">
import { init } from "{{& app_main}}"; import { init } from "{{& app_main}}";
init(); import defaultTranslations from "{{& default_translations}}";
init({defaultTranslations});
</script> </script>
{{/manifest}} {{/manifest}}
</body> </body>

View File

@@ -187,7 +187,7 @@ async function readManifestFile(resource) {
return JSON.parse(content); return JSON.parse(content);
} }
async function readShadowManifest() { async function generateManifest() {
const index = { const index = {
app_main: "./js/main.js", app_main: "./js/main.js",
render_main: "./js/render.js", render_main: "./js/render.js",
@@ -197,6 +197,7 @@ async function readShadowManifest() {
polyfills: "./js/polyfills.js?version=" + CURRENT_VERSION, polyfills: "./js/polyfills.js?version=" + CURRENT_VERSION,
libs: "./js/libs.js?version=" + CURRENT_VERSION, libs: "./js/libs.js?version=" + CURRENT_VERSION,
worker_main: "./js/worker/main.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({ importmap: JSON.stringify({
"imports": { "imports": {
@@ -391,7 +392,7 @@ async function generateTemplates() {
const isDebug = process.env.NODE_ENV !== "production"; const isDebug = process.env.NODE_ENV !== "production";
await fs.mkdir("./resources/public/", { recursive: true }); await fs.mkdir("./resources/public/", { recursive: true });
const manifest = await readShadowManifest(); const manifest = await generateManifest();
let content; let content;
const iconsSprite = await fs.readFile( const iconsSprite = await fs.readFile(

View File

@@ -90,7 +90,10 @@
(rx/map #(ws/initialize))))))) (rx/map #(ws/initialize)))))))
(defn ^:export init (defn ^:export init
[] [options]
(some-> (unchecked-get options "defaultTranslations")
(i18n/set-default-translations))
(mw/init!) (mw/init!)
(i18n/init) (i18n/init)
(cur/init-styles) (cur/init-styles)

View File

@@ -98,9 +98,7 @@
(def context (def context
(atom (d/without-nils (collect-context)))) (atom (d/without-nils (collect-context))))
(add-watch i18n/state "events" (add-watch i18n/locale "events" #(swap! context assoc :locale %4))
(fn [_ _ _ v]
(swap! context assoc :locale (get v :locale))))
;; --- EVENT TRANSLATION ;; --- EVENT TRANSLATION

View File

@@ -69,7 +69,6 @@
(-> (.-language globals/navigator) (-> (.-language globals/navigator)
(parse-locale)))) (parse-locale))))
;; Set initial translation loading state as globaly stored variable; ;; Set initial translation loading state as globaly stored variable;
;; this facilitates hot reloading ;; this facilitates hot reloading
(when-not (exists? (unchecked-get globals/global "penpotTranslations")) (when-not (exists? (unchecked-get globals/global "penpotTranslations"))
@@ -93,14 +92,8 @@
(def ^:dynamic *current-locale* (def ^:dynamic *current-locale*
(get-current)) (get-current))
(defonce state (defonce locale
(l/atom {:render 0 :locale *current-locale*})) (l/atom *current-locale*))
(defn- assign-current-locale
[state locale]
(-> state
(update :render inc)
(assoc :locale locale)))
(defn- get-translations (defn- get-translations
"Get globaly stored mutable object with all loaded translations" "Get globaly stored mutable object with all loaded translations"
@@ -114,6 +107,10 @@
(unchecked-set translations locale data) (unchecked-set translations locale data)
nil)) nil))
(defn set-default-translations
[data]
(set-translations cf/default-language data))
(defn- load (defn- load
[locale] [locale]
(let [path (str "./translation." locale ".js?version=" (:full cf/version))] (let [path (str "./translation." locale ".js?version=" (:full cf/version))]
@@ -122,15 +119,14 @@
(p/fnly (fn [data cause] (p/fnly (fn [data cause]
(if cause (if cause
(js/console.error "unexpected error on fetching locale" cause) (js/console.error "unexpected error on fetching locale" cause)
(do (set-translations locale data)))))))
(set! *current-locale* locale)
(set-translations locale data)
(swap! state assign-current-locale locale))))))))
(defn init (defn init
"Initialize the i18n module" "Initialize the i18n module"
[] []
(load *current-locale*)) (load *current-locale*)
(when-not (= *current-locale* cf/default-language)
(load cf/default-language)))
(defn set-locale (defn set-locale
[lname] [lname]
@@ -145,7 +141,10 @@
(recur (rest locales))) (recur (rest locales)))
cf/default-language))))] cf/default-language))))]
(load lname))) (->> (load lname)
(p/fnly (fn [_r _c]
(set! *current-locale* lname)
(reset! locale lname))))))
(deftype C [val] (deftype C [val]
IDeref IDeref
@@ -206,9 +205,7 @@
:className class :className class
:on-click on-click}])) :on-click on-click}]))
(add-watch state "common.time" (add-watch locale "common.time"
(fn [_ _ pv cv] (fn [_ _ pv cv]
(let [pv (get pv :locale) (when (not= pv cv)
cv (get cv :locale)] (ct/set-default-locale cv))))
(when (not= pv cv)
(ct/set-default-locale! cv)))))