refactor: use lazy loading for localization files

This commit is contained in:
pa
2025-11-18 20:59:41 +09:00
committed by Natsumi
parent fdeb8fa38f
commit 0bc9980cae
19 changed files with 51 additions and 52 deletions
+1 -1
View File
@@ -7,7 +7,7 @@
"dev": "cross-env PLATFORM=windows vite serve src", "dev": "cross-env PLATFORM=windows vite serve src",
"dev-linux": "cross-env PLATFORM=linux vite serve src", "dev-linux": "cross-env PLATFORM=linux vite serve src",
"dev:test": "concurrently \"npm run dev\" \"jest --watchAll\"", "dev:test": "concurrently \"npm run dev\" \"jest --watchAll\"",
"localization": "node ./src/localization/localizationHelperCLI.js", "localization": "node ./src/shared/utils/localizationHelperCLI.js",
"test": "jest", "test": "jest",
"test:coverage": "jest --coverage", "test:coverage": "jest --coverage",
"prod": "cross-env PLATFORM=windows vite build src", "prod": "cross-env PLATFORM=windows vite build src",
+37
View File
@@ -0,0 +1,37 @@
const langCodes = [
'cs',
'en',
'es',
'fr',
'hu',
'ja',
'ko',
'pl',
'pt',
'ru',
'th',
'vi',
'zh-CN',
'zh-TW'
];
async function getLocalizationStrings() {
const urlPromises = Promise.all(
langCodes.map((code) =>
import(`./${code}.json?url`).then((m) => m.default)
)
);
const urls = await urlPromises;
const fetchPromises = Promise.all(
urls.map((url) => fetch(url).then((res) => res.json()))
);
const results = await fetchPromises;
const entries = langCodes.map((code, index) => {
return [code, results[index]];
});
return Object.fromEntries(entries);
}
export { getLocalizationStrings };
-44
View File
@@ -1,44 +0,0 @@
import en from './en/en.json' assert { type: 'JSON' };
import es from './es/en.json' assert { type: 'JSON' };
import fr from './fr/en.json' assert { type: 'JSON' };
import hu from './hu/en.json' assert { type: 'JSON' };
import ja from './ja/en.json' assert { type: 'JSON' };
import ko from './ko/en.json' assert { type: 'JSON' };
import pl from './pl/en.json' assert { type: 'JSON' };
import pt from './pt/en.json' assert { type: 'JSON' };
import cs from './cs/en.json' assert { type: 'JSON' };
import ru from './ru/en.json' assert { type: 'JSON' };
import vi from './vi/en.json' assert { type: 'JSON' };
import zh_CN from './zh-CN/en.json' assert { type: 'JSON' };
import zh_TW from './zh-TW/en.json' assert { type: 'JSON' };
import th from './th/en.json' assert { type: 'JSON' };
export {
en,
es,
fr,
hu,
ja,
ko,
pl,
pt,
cs,
ru,
vi,
zh_CN,
zh_TW,
th
};
+3 -1
View File
@@ -1,6 +1,8 @@
import { createI18n } from 'vue-i18n'; import { createI18n } from 'vue-i18n';
import * as localizedStrings from '../localization/localizedStrings'; import { getLocalizationStrings } from '../localization/index.js';
const localizedStrings = await getLocalizationStrings();
const i18n = createI18n({ const i18n = createI18n({
locale: 'en', locale: 'en',
@@ -9,11 +9,15 @@ const { hideBin } = require('yargs/helpers');
const getLocalizationObjects = function* () { const getLocalizationObjects = function* () {
const localeFolder = './src/localization'; const localeFolder = './src/localization';
const folders = fs const files = fs
.readdirSync(localeFolder, { withFileTypes: true }) .readdirSync(localeFolder, { withFileTypes: true })
.filter((file) => file.isDirectory()); .filter(
for (const folder of folders) { (file) =>
const filePath = path.join(localeFolder, folder.name, 'en.json'); file.isFile() &&
path.extname(file.name).toLowerCase() === '.json'
);
for (const file of files) {
const filePath = path.join(localeFolder, file.name);
const jsonStr = fs.readFileSync(filePath); const jsonStr = fs.readFileSync(filePath);
yield [filePath, JSON.parse(jsonStr)]; yield [filePath, JSON.parse(jsonStr)];
} }
@@ -119,8 +123,8 @@ const removeLocalizationKey = (key) => {
// Yes this code is extremely slow, but it doesn't run very often so. // Yes this code is extremely slow, but it doesn't run very often so.
const Validate = function () { const Validate = function () {
const files = [...getLocalizationObjects()]; const files = [...getLocalizationObjects()];
const enIndex = files.findIndex((file) => const enIndex = files.findIndex(
path.dirname(file[0]).endsWith('en') (file) => path.basename(file[0]) === 'en.json'
); );
const [_, enObj] = files.splice(enIndex, 1)[0]; const [_, enObj] = files.splice(enIndex, 1)[0];