Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

Page header: mount once translations are loaded (i18n.ready / i18nReady), so labels never show raw keys

Murtaza Nasir6835970b848c
diff --git a/static/js/global-header.js b/static/js/global-header.js
index 6005934c..482c48ef 100644
--- a/static/js/global-header.js
+++ b/static/js/global-header.js
@@ -33,6 +33,12 @@ function ensureVue() {
});
}
+function waitForTranslations(timeoutMs) {
+ const i18n = window.i18n;
+ if (!i18n || i18n.isReady || !i18n.ready) return Promise.resolve();
+ return Promise.race([i18n.ready, new Promise(resolve => setTimeout(resolve, timeoutMs))]);
+}
+
async function mountGlobalHeader() {
const el = document.getElementById('global-header');
if (!el || el.dataset.mounted) return;
@@ -40,9 +46,13 @@ async function mountGlobalHeader() {
await ensureVue();
const { createApp, ref, onMounted } = window.Vue;
- // Re-render the labels once the locale has loaded.
+ // Mount with the translations in place, so the labels appear once and
+ // already translated. A page that never initialises i18n still gets its
+ // header after a short wait; the labels then update when it does.
+ await waitForTranslations(1500);
const localeVersion = ref(0);
window.addEventListener('localeChanged', () => { localeVersion.value++; });
+ window.addEventListener('i18nReady', () => { localeVersion.value++; });
const t = (key, params) => {
localeVersion.value; // dependency, so labels follow the locale
const i18n = window.i18n;
diff --git a/static/js/i18n.js b/static/js/i18n.js
index 6cf90e07..5f6a521c 100644
--- a/static/js/i18n.js
+++ b/static/js/i18n.js
@@ -9,6 +9,10 @@ class I18n {
this.currentLocale = 'en';
this.fallbackLocale = 'en';
this.loadedLocales = new Set();
+ // Resolved once init() has loaded the translations, so code that
+ // renders before then (the shared page header) can wait for them.
+ this.isReady = false;
+ this.ready = new Promise(resolve => { this._resolveReady = resolve; });
}
/**
@@ -29,6 +33,12 @@ class I18n {
if (this.currentLocale !== this.fallbackLocale) {
await this.loadLocale(this.fallbackLocale);
}
+
+ if (!this.isReady) {
+ this.isReady = true;
+ this._resolveReady();
+ window.dispatchEvent(new CustomEvent('i18nReady', { detail: { locale: this.currentLocale } }));
+ }
}
/**