Skip to content

Commit

Permalink
enh(locale): コミットハッシュ変更時に言語アセットを更新する
Browse files Browse the repository at this point in the history
  • Loading branch information
taiyme committed Apr 26, 2024
1 parent e27d7b2 commit 53727e6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
11 changes: 6 additions & 5 deletions packages/frontend/src/boot/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { compareVersions } from 'compare-versions';
import widgets from '@/widgets/index.js';
import directives from '@/directives/index.js';
import components from '@/components/index.js';
import { version, lang, updateLocale, locale } from '@/config.js';
import { version, lang, updateLocale, locale, commitHash } from '@/config.js';
import { applyTheme } from '@/scripts/theme.js';
import { isDeviceDarkmode } from '@/scripts/is-device-darkmode.js';
import { updateI18n } from '@/i18n.js';
Expand Down Expand Up @@ -81,15 +81,16 @@ export async function common(createVue: () => App<Element>) {
//#endregion

//#region Detect language & fetch translations
const localeVersion = miLocalStorage.getItem('localeVersion');
const localeOutdated = (localeVersion == null || localeVersion !== version || locale == null);
const revision = commitHash ?? 'unknown';
const newLocaleVersion = `${version}+REV:${revision}`;
const localeOutdated = miLocalStorage.getItem('localeVersion') !== newLocaleVersion || locale == null;
if (localeOutdated) {
const res = await window.fetch(`/assets/locales/${lang}.${version}.json`);
const res = await window.fetch(`/assets/locales/${lang}.${version}.json?rev=${revision}`);
if (res.status === 200) {
const newLocale = await res.text();
const parsedNewLocale = JSON.parse(newLocale);
miLocalStorage.setItem('locale', newLocale);
miLocalStorage.setItem('localeVersion', version);
miLocalStorage.setItem('localeVersion', newLocaleVersion);
updateLocale(parsedNewLocale);
updateI18n(parsedNewLocale);
}
Expand Down
17 changes: 5 additions & 12 deletions packages/frontend/src/pages/tms/flags/index.main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { defineAsyncComponent, readonly, ref, watch } from 'vue';
import { commitHash, lang, version } from '@/config.js';
import { i18n } from '@/i18n.js';
import { miLocalStorage } from '@/local-storage.js';
import { confirm, inputText, popup, waiting } from '@/os.js';
import { confirm, popup, waiting } from '@/os.js';
import FormSection from '@/components/form/section.vue';
import MkButton from '@/components/MkButton.vue';
import MkFolder from '@/components/MkFolder.vue';
Expand All @@ -47,22 +47,15 @@ const confirmDialog = async (): Promise<boolean> => {
};

const forceFetchLocale = async (): Promise<void> => {
const defaultValue = commitHash ?? Date.now().toString();
const { canceled, result } = await inputText({
type: 'text',
text: 'Revision?',
placeholder: defaultValue,
default: defaultValue,
});
if (canceled) return;
if (!(await confirmDialog())) return;
waiting();
const revision = result || defaultValue;
const res = await window.fetch(`/assets/locales/${lang}.${version}.json?rev=${revision}`);
const revision = commitHash ?? 'unknown';
const newLocaleVersion = `${version}+REV:${revision}`;
const res = await window.fetch(`/assets/locales/${lang}.${version}.json?date=${Date.now().toString()}`);
if (res.status === 200) {
const newLocale = await res.text();
miLocalStorage.setItem('locale', newLocale);
miLocalStorage.setItem('localeVersion', version);
miLocalStorage.setItem('localeVersion', newLocaleVersion);
}
location.reload();
};
Expand Down

0 comments on commit 53727e6

Please sign in to comment.