Load languages on demand and publish complete viewer bundles
This commit is contained in:
parent
2f0c4a0503
commit
ca275d6a37
8 changed files with 103 additions and 43 deletions
|
|
@ -25,3 +25,5 @@ npm run serve
|
|||
```
|
||||
|
||||
It will use the engine in its sibling folder
|
||||
|
||||
Viewer releases: see [uploading the complete viewer build](docs/viewer-publishing.md).
|
||||
|
|
|
|||
11
docs/viewer-publishing.md
Normal file
11
docs/viewer-publishing.md
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
# Publishing the viewer
|
||||
|
||||
Upload `container-viewer.umd.min.js`, `container-viewer.css` and all `container-viewer.umd.min.locale-*.js` files from `viewer/dist`. The other UMD/CommonJS variants are not needed.
|
||||
|
||||
All files in a release must share one BGS `bundle` directory, with their relative paths preserved. Upload everything before saving the new entry URL. A main-JS-only upload will break language or asset loading. Keep engines and test data out of the viewer upload.
|
||||
|
||||
BGS compresses JS, CSS, JSON and WASM automatically during upload. Send the original bytes; do not create `.gz` files. Keep sourcemaps local unless you deliberately want to publish them. Set `viewer.scriptBytes` to the original entry size so BGS can show download progress.
|
||||
|
||||
The BGS admin's **Upload folder** control handles multi-file builds. For scripted releases, use BGS's `scripts/publish-viewer.mjs` with `--dir`, `--entry` and `--style`; it verifies every uploaded file, records the previous viewer, and activates only with `--apply --activate`. The full command and API contract are in the [BGS viewer documentation](https://docs.boardgamers.space/guide/viewer-api#publishing-a-viewer-with-multiple-files).
|
||||
|
||||
English needs no language download; other languages load when selected. Test a cold load and a language change after publishing. Also check the browser Network panel for missing relative assets.
|
||||
|
|
@ -62,6 +62,7 @@
|
|||
"dist/img",
|
||||
"dist/media",
|
||||
"tsconfig.json",
|
||||
"public/audio"
|
||||
"public/audio",
|
||||
"dist/container-viewer.umd.min.locale-*.js"
|
||||
]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,6 +26,9 @@ const server = createServer(async (req, res) => {
|
|||
if (req.url === '/') {
|
||||
res.setHeader('Content-Type', 'text/html');
|
||||
res.end(html);
|
||||
} else if (/^\/container-viewer\.umd\.min\.locale-[a-zA-Z-]+\.js$/.test(req.url)) {
|
||||
res.setHeader('Content-Type', 'text/javascript; charset=utf-8');
|
||||
res.end(await readFile(fileURLToPath(new URL('viewer/dist' + req.url, repo))));
|
||||
} else if (files[req.url]) {
|
||||
res.setHeader('Content-Type', req.url.endsWith('.css') ? 'text/css' : 'text/javascript; charset=utf-8');
|
||||
res.end(await readFile(files[req.url]));
|
||||
|
|
|
|||
|
|
@ -54,6 +54,7 @@ function launch(selector: string) {
|
|||
return thumbnail.render(app.$el.querySelector('#scene'), size, '#c6deda', '[data-thumbnail-omit]');
|
||||
},
|
||||
async onState(data) {
|
||||
await localization.ready;
|
||||
localization.setState(data);
|
||||
params.state = data;
|
||||
app.$forceUpdate();
|
||||
|
|
@ -63,8 +64,10 @@ function launch(selector: string) {
|
|||
params.player = data.index;
|
||||
app.$forceUpdate();
|
||||
},
|
||||
onPreferences(data) {
|
||||
localization.setLocale(data.locale);
|
||||
async onPreferences(data) {
|
||||
if (!(await localization.setLocale(data.locale))) {
|
||||
return;
|
||||
}
|
||||
Object.assign(params.preferences, data);
|
||||
app.$forceUpdate();
|
||||
},
|
||||
|
|
|
|||
5
viewer/src/localization/index.d.ts
vendored
5
viewer/src/localization/index.d.ts
vendored
|
|
@ -2,7 +2,8 @@ import type { TutorialMount } from '@boardgamers/protocol/tutorial';
|
|||
export interface Localization {
|
||||
readonly locale: string;
|
||||
translate(text: string): string;
|
||||
setLocale(locale: unknown): void;
|
||||
readonly ready: Promise<boolean>;
|
||||
setLocale(locale: unknown): Promise<boolean>;
|
||||
setState(state: unknown): void;
|
||||
setNames(names: unknown[]): void;
|
||||
refresh(): void;
|
||||
|
|
@ -14,3 +15,5 @@ export function resolveLocale(value: unknown): string;
|
|||
export function translateText(text: string, locale?: string): string;
|
||||
export function mountLocalization(target: Element, locale?: unknown): Localization;
|
||||
export function localizeTutorial(mount: TutorialMount): TutorialMount;
|
||||
|
||||
export function loadLocale(locale: unknown): Promise<string>;
|
||||
|
|
|
|||
|
|
@ -1,40 +1,46 @@
|
|||
import catalog8 from './da.json';
|
||||
import catalog1 from './de.json';
|
||||
import catalog5 from './el.json';
|
||||
import catalog0 from './en.json';
|
||||
import persian from './fa.json';
|
||||
import catalog2 from './fr.json';
|
||||
import catalog6 from './hi.json';
|
||||
import catalog13 from './it.json';
|
||||
import catalog10 from './ko.json';
|
||||
import catalog14 from './nl.json';
|
||||
import catalog3 from './pl.json';
|
||||
import catalog9 from './pt-BR.json';
|
||||
import catalog4 from './ro.json';
|
||||
import './rtl.css';
|
||||
import catalog7 from './ru.json';
|
||||
import { createTranslator, mountLocalization as mount } from './runtime.js';
|
||||
import catalog12 from './vi.json';
|
||||
import catalog11 from './zh-TW.json';
|
||||
import { createTranslator, mountLocalization as mount, resolveLocale } from './runtime.js';
|
||||
export { languages, resolveLocale } from './runtime.js';
|
||||
export const catalogs = {
|
||||
en: catalog0,
|
||||
fa: persian,
|
||||
de: catalog1,
|
||||
fr: catalog2,
|
||||
pl: catalog3,
|
||||
ro: catalog4,
|
||||
el: catalog5,
|
||||
hi: catalog6,
|
||||
ru: catalog7,
|
||||
da: catalog8,
|
||||
'pt-BR': catalog9,
|
||||
ko: catalog10,
|
||||
'zh-TW': catalog11,
|
||||
vi: catalog12,
|
||||
it: catalog13,
|
||||
nl: catalog14,
|
||||
|
||||
const loaders = {
|
||||
de: () => import(/* webpackChunkName: "locale-de" */ './de.json'),
|
||||
fr: () => import(/* webpackChunkName: "locale-fr" */ './fr.json'),
|
||||
pl: () => import(/* webpackChunkName: "locale-pl" */ './pl.json'),
|
||||
ro: () => import(/* webpackChunkName: "locale-ro" */ './ro.json'),
|
||||
el: () => import(/* webpackChunkName: "locale-el" */ './el.json'),
|
||||
hi: () => import(/* webpackChunkName: "locale-hi" */ './hi.json'),
|
||||
ru: () => import(/* webpackChunkName: "locale-ru" */ './ru.json'),
|
||||
da: () => import(/* webpackChunkName: "locale-da" */ './da.json'),
|
||||
'pt-BR': () => import(/* webpackChunkName: "locale-pt-BR" */ './pt-BR.json'),
|
||||
ko: () => import(/* webpackChunkName: "locale-ko" */ './ko.json'),
|
||||
'zh-TW': () => import(/* webpackChunkName: "locale-zh-TW" */ './zh-TW.json'),
|
||||
vi: () => import(/* webpackChunkName: "locale-vi" */ './vi.json'),
|
||||
it: () => import(/* webpackChunkName: "locale-it" */ './it.json'),
|
||||
nl: () => import(/* webpackChunkName: "locale-nl" */ './nl.json'),
|
||||
fa: () => import(/* webpackChunkName: "locale-fa" */ './fa.json'),
|
||||
};
|
||||
export const catalogs = { en: {} };
|
||||
const pending = new Map();
|
||||
export function loadLocale(value) {
|
||||
const locale = resolveLocale(value);
|
||||
if (catalogs[locale]) return Promise.resolve(locale);
|
||||
if (!pending.has(locale)) {
|
||||
pending.set(
|
||||
locale,
|
||||
loaders[locale]()
|
||||
.then((module) => {
|
||||
catalogs[locale] = module.default;
|
||||
translators.delete(locale);
|
||||
return locale;
|
||||
})
|
||||
.catch((error) => {
|
||||
pending.delete(locale);
|
||||
throw error;
|
||||
})
|
||||
);
|
||||
}
|
||||
return pending.get(locale);
|
||||
}
|
||||
const translators = new Map();
|
||||
export function translateText(text, locale = 'en') {
|
||||
if (!translators.has(locale)) {
|
||||
|
|
@ -43,12 +49,46 @@ export function translateText(text, locale = 'en') {
|
|||
return translators.get(locale).translate(text);
|
||||
}
|
||||
export function mountLocalization(target, locale) {
|
||||
return mount(target, catalogs, locale ?? target.ownerDocument.documentElement.lang ?? 'en');
|
||||
const localization = mount(target, catalogs, 'en');
|
||||
let revision = 0;
|
||||
let ready = Promise.resolve(true);
|
||||
function setLocale(value) {
|
||||
const attempt = ++revision;
|
||||
ready = loadLocale(value)
|
||||
.then((loaded) => {
|
||||
if (attempt !== revision) return false;
|
||||
localization.setLocale(loaded);
|
||||
return true;
|
||||
})
|
||||
.catch((error) => {
|
||||
console.warn('Could not load game language', error);
|
||||
if (attempt !== revision) return false;
|
||||
localization.setLocale('en');
|
||||
return true;
|
||||
});
|
||||
return ready;
|
||||
}
|
||||
setLocale(locale ?? target.ownerDocument.documentElement.lang ?? 'en');
|
||||
return {
|
||||
...localization,
|
||||
setLocale,
|
||||
get locale() {
|
||||
return localization.locale;
|
||||
},
|
||||
get ready() {
|
||||
return ready;
|
||||
},
|
||||
destroy() {
|
||||
revision++;
|
||||
localization.destroy();
|
||||
},
|
||||
};
|
||||
}
|
||||
export function localizeTutorial(mountTutorial) {
|
||||
return async (target, options) => {
|
||||
const localization = mountLocalization(target, options.locale);
|
||||
try {
|
||||
await localization.ready;
|
||||
const dispose = await mountTutorial(target, options);
|
||||
localization.refresh();
|
||||
return () => {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,4 @@
|
|||
// The production deploy uploads ONLY dist/container-viewer.umd.min.js and
|
||||
// dist/container-viewer.css — nothing else from dist/ is served. Any asset emitted
|
||||
// as a separate file (dist/img/*.svg icons, dist/media/*.mp3 audio) would 404, so
|
||||
// every static asset must be inlined into the bundles as a data URI.
|
||||
// Small icons and sounds stay inline. Language chunks are uploaded with the entry and CSS.
|
||||
const INLINE_ASSETS_LIMIT = 10 * 1024 * 1024;
|
||||
|
||||
const { dirname, join } = require('path');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue