From bddc00bf475229cafeeb7b2f9666953a51ca5fdd Mon Sep 17 00:00:00 2001 From: tai-cha Date: Mon, 23 Jun 2025 19:53:41 +0900 Subject: [PATCH 1/4] =?UTF-8?q?chore(frontend):=20=E9=96=8B=E7=99=BA?= =?UTF-8?q?=E3=83=A2=E3=83=BC=E3=83=89=E6=99=82=E3=81=AB=E8=A8=80=E8=AA=9E?= =?UTF-8?q?=E3=83=95=E3=82=A1=E3=82=A4=E3=83=AB=E3=81=AE=E5=A4=89=E6=9B=B4?= =?UTF-8?q?=E3=82=92=E8=87=AA=E5=8B=95=E3=81=A7=E5=8F=8D=E6=98=A0=E3=81=99?= =?UTF-8?q?=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../frontend/lib/vite-plugin-watch-locales.ts | 31 +++++++++++++++++++ packages/frontend/src/boot/common.ts | 23 ++++++++++++-- packages/frontend/vite.config.ts | 2 ++ 3 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 packages/frontend/lib/vite-plugin-watch-locales.ts diff --git a/packages/frontend/lib/vite-plugin-watch-locales.ts b/packages/frontend/lib/vite-plugin-watch-locales.ts new file mode 100644 index 0000000000..ddb47a218d --- /dev/null +++ b/packages/frontend/lib/vite-plugin-watch-locales.ts @@ -0,0 +1,31 @@ +import path from 'node:path' +import locales from '../../../locales/index.js'; + +const localesDir = path.resolve(__dirname, '../../../locales') + +/** + * 外部ファイルを監視し、必要に応じてサーバを再起動するViteプラグイン + * @returns {import('vite').Plugin} + */ +export default function pluginWatchLocales() { + return { + name: 'watch-locales', + + configureServer(server) { + const localeYmlPaths = Object.keys(locales).map(locale => path.join(localesDir, `${locale}.yml`)); + + // watcherにパスを追加 + server.watcher.add(localeYmlPaths); + + server.watcher.on('change', (filePath) => { + if (localeYmlPaths.includes(filePath)) { + server.ws.send({ + type: 'custom', + event: 'language-update', + data: filePath.match(/([^\/]+)\.yml$/)?.[1] || null, + }) + } + }); + }, + }; +} diff --git a/packages/frontend/src/boot/common.ts b/packages/frontend/src/boot/common.ts index c8098b6cf8..d62db98acf 100644 --- a/packages/frontend/src/boot/common.ts +++ b/packages/frontend/src/boot/common.ts @@ -81,8 +81,10 @@ export async function common(createVue: () => Promise>) { //#region Detect language & fetch translations const localeVersion = miLocalStorage.getItem('localeVersion'); const localeOutdated = (localeVersion == null || localeVersion !== version || locale == null); - if (localeOutdated) { - const res = await window.fetch(`/assets/locales/${lang}.${version}.json`); + + async function fetchAndUpdateLocale({ useCache } = { useCache: true }) { + const fetchOptions: RequestInit | undefined = useCache ? undefined : { cache: 'no-store' }; + const res = await window.fetch(`/assets/locales/${lang}.${version}.json`, fetchOptions); if (res.status === 200) { const newLocale = await res.text(); const parsedNewLocale = JSON.parse(newLocale); @@ -92,6 +94,23 @@ export async function common(createVue: () => Promise>) { updateI18n(parsedNewLocale); } } + + if (localeOutdated) { + fetchAndUpdateLocale(); + } + + if (import.meta.hot) { + import.meta.hot.on('language-update', async (updatedLang: string) => { + console.info(`Language updated: ${updatedLang}`); + if (updatedLang === lang) { + await new Promise(resolve => { + window.setTimeout(resolve, 500); + }); + await fetchAndUpdateLocale({ useCache: false }); + window.location.reload(); + } + }); + } //#endregion // タッチデバイスでCSSの:hoverを機能させる diff --git a/packages/frontend/vite.config.ts b/packages/frontend/vite.config.ts index b0ccbfb65c..77b5b48a18 100644 --- a/packages/frontend/vite.config.ts +++ b/packages/frontend/vite.config.ts @@ -13,6 +13,7 @@ import pluginUnwindCssModuleClassName from './lib/rollup-plugin-unwind-css-modul import pluginJson5 from './vite.json5.js'; import pluginCreateSearchIndex from './lib/vite-plugin-create-search-index.js'; import type { Options as SearchIndexOptions } from './lib/vite-plugin-create-search-index.js'; +import pluginWatchLocales from './lib/vite-plugin-watch-locales.js'; const url = process.env.NODE_ENV === 'development' ? yaml.load(await fsp.readFile('../../.config/default.yml', 'utf-8')).url : null; const host = url ? (new URL(url)).hostname : undefined; @@ -102,6 +103,7 @@ export function getConfig(): UserConfig { }, plugins: [ + pluginWatchLocales(), ...searchIndexes.map(options => pluginCreateSearchIndex(options)), pluginVue(), pluginUnwindCssModuleClassName(), From dc97357e5e821832700edc08f0167c7d27dbce2a Mon Sep 17 00:00:00 2001 From: tai-cha Date: Mon, 23 Jun 2025 19:55:22 +0900 Subject: [PATCH 2/4] fix message --- packages/frontend/lib/vite-plugin-watch-locales.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/frontend/lib/vite-plugin-watch-locales.ts b/packages/frontend/lib/vite-plugin-watch-locales.ts index ddb47a218d..f4f1bea780 100644 --- a/packages/frontend/lib/vite-plugin-watch-locales.ts +++ b/packages/frontend/lib/vite-plugin-watch-locales.ts @@ -4,7 +4,7 @@ import locales from '../../../locales/index.js'; const localesDir = path.resolve(__dirname, '../../../locales') /** - * 外部ファイルを監視し、必要に応じてサーバを再起動するViteプラグイン + * 外部ファイルを監視し、必要に応じてwebSocketでメッセージを送るViteプラグイン * @returns {import('vite').Plugin} */ export default function pluginWatchLocales() { From 6fb1ab03ca4d15e99efa5e5865e719f6c8b82566 Mon Sep 17 00:00:00 2001 From: tai-cha Date: Mon, 23 Jun 2025 20:02:03 +0900 Subject: [PATCH 3/4] naming --- packages/frontend/lib/vite-plugin-watch-locales.ts | 2 +- packages/frontend/src/boot/common.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/frontend/lib/vite-plugin-watch-locales.ts b/packages/frontend/lib/vite-plugin-watch-locales.ts index f4f1bea780..4ecc99b8ff 100644 --- a/packages/frontend/lib/vite-plugin-watch-locales.ts +++ b/packages/frontend/lib/vite-plugin-watch-locales.ts @@ -21,7 +21,7 @@ export default function pluginWatchLocales() { if (localeYmlPaths.includes(filePath)) { server.ws.send({ type: 'custom', - event: 'language-update', + event: 'locale-update', data: filePath.match(/([^\/]+)\.yml$/)?.[1] || null, }) } diff --git a/packages/frontend/src/boot/common.ts b/packages/frontend/src/boot/common.ts index d62db98acf..992bde9bd1 100644 --- a/packages/frontend/src/boot/common.ts +++ b/packages/frontend/src/boot/common.ts @@ -100,8 +100,8 @@ export async function common(createVue: () => Promise>) { } if (import.meta.hot) { - import.meta.hot.on('language-update', async (updatedLang: string) => { - console.info(`Language updated: ${updatedLang}`); + import.meta.hot.on('locale-update', async (updatedLang: string) => { + console.info(`Locale updated: ${updatedLang}`); if (updatedLang === lang) { await new Promise(resolve => { window.setTimeout(resolve, 500); From 23d0219c0fb5e9da6902c3c6be978bfddd9c85d0 Mon Sep 17 00:00:00 2001 From: tai-cha Date: Mon, 23 Jun 2025 20:15:33 +0900 Subject: [PATCH 4/4] SPDX --- packages/frontend/lib/vite-plugin-watch-locales.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/frontend/lib/vite-plugin-watch-locales.ts b/packages/frontend/lib/vite-plugin-watch-locales.ts index 4ecc99b8ff..8e209d27bd 100644 --- a/packages/frontend/lib/vite-plugin-watch-locales.ts +++ b/packages/frontend/lib/vite-plugin-watch-locales.ts @@ -1,3 +1,8 @@ +/* + * SPDX-FileCopyrightText: syuilo and misskey-project + * SPDX-License-Identifier: AGPL-3.0-only + */ + import path from 'node:path' import locales from '../../../locales/index.js';