mirror of
https://github.com/Afilmory/afilmory
synced 2026-04-24 23:05:05 +00:00
- Added i18next and react-i18next for multi-language support in the application. - Created localization files for English, Japanese, Korean, Traditional Chinese, and Simplified Chinese. - Implemented translation hooks in various components to replace hardcoded strings with translatable keys. - Updated ESLint configuration to include new i18n JSON validation rules. - Introduced a new event bus for handling i18n updates during development. Signed-off-by: Innei <tukon479@gmail.com>
69 lines
1.9 KiB
TypeScript
69 lines
1.9 KiB
TypeScript
import fs from 'node:fs'
|
|
import path, { dirname } from 'node:path'
|
|
import { fileURLToPath } from 'node:url'
|
|
|
|
import { set } from 'es-toolkit/compat'
|
|
import type { Plugin } from 'vite'
|
|
|
|
export function localesPlugin(): Plugin {
|
|
return {
|
|
name: 'locales-merge',
|
|
enforce: 'post',
|
|
generateBundle(_options, bundle) {
|
|
const __dirname = dirname(fileURLToPath(import.meta.url))
|
|
|
|
const localesDir = path.resolve(__dirname, '../../locales')
|
|
|
|
const namespaces = fs
|
|
.readdirSync(localesDir)
|
|
.filter((dir) => dir !== '.DS_Store')
|
|
const languageResources = {} as any
|
|
|
|
namespaces.forEach((namespace) => {
|
|
const namespacePath = path.join(localesDir, namespace)
|
|
const files = fs
|
|
.readdirSync(namespacePath)
|
|
.filter((file) => file.endsWith('.json'))
|
|
|
|
files.forEach((file) => {
|
|
const lang = path.basename(file, '.json')
|
|
const filePath = path.join(namespacePath, file)
|
|
const content = JSON.parse(fs.readFileSync(filePath, 'utf-8'))
|
|
|
|
if (!languageResources[lang]) {
|
|
languageResources[lang] = {}
|
|
}
|
|
|
|
const obj = {}
|
|
|
|
const keys = Object.keys(content as object)
|
|
for (const accessorKey of keys) {
|
|
set(obj, accessorKey, (content as any)[accessorKey])
|
|
}
|
|
|
|
languageResources[lang][namespace] = obj
|
|
})
|
|
})
|
|
|
|
Object.entries(languageResources).forEach(([lang, resources]) => {
|
|
const fileName = `locales/${lang}.js`
|
|
|
|
const content = `export default ${JSON.stringify(resources)};`
|
|
|
|
this.emitFile({
|
|
type: 'asset',
|
|
fileName,
|
|
source: content,
|
|
})
|
|
})
|
|
|
|
// Remove original JSON chunks
|
|
Object.keys(bundle).forEach((key) => {
|
|
if (key.startsWith('locales/') && key.endsWith('.json')) {
|
|
delete bundle[key]
|
|
}
|
|
})
|
|
},
|
|
}
|
|
}
|