提交 099cc1b3 编写于 作者: fxy060608's avatar fxy060608

feat(i18n): support locale/uni-app.*.json

上级 5b322033
......@@ -3,6 +3,7 @@ import { Plugin } from 'vite'
import {
defineUniManifestJsonPlugin,
getLocaleFiles,
normalizeAppManifestJson,
parseJson,
parsePagesJsonOnce,
......@@ -21,6 +22,11 @@ export function uniManifestJsonPlugin(): Plugin {
this.addWatchFile(
path.resolve(process.env.UNI_INPUT_DIR, 'manifest.json')
)
getLocaleFiles(
path.resolve(process.env.UNI_INPUT_DIR, 'locale')
).forEach((filepath) => {
this.addWatchFile(filepath)
})
manifestJson = normalizeAppManifestJson(
parseJson(code),
parsePagesJsonOnce(
......
......@@ -7,6 +7,7 @@ import {
normalizeAppConfigService,
normalizePagesJson,
parseManifestJsonOnce,
getLocaleFiles,
} from '@dcloudio/uni-cli-shared'
export function uniPagesJsonPlugin(): Plugin {
......@@ -20,6 +21,11 @@ export function uniPagesJsonPlugin(): Plugin {
return
}
this.addWatchFile(path.resolve(process.env.UNI_INPUT_DIR, 'pages.json'))
getLocaleFiles(
path.resolve(process.env.UNI_INPUT_DIR, 'locale')
).forEach((filepath) => {
this.addWatchFile(filepath)
})
pagesJson = normalizePagesJson(code, process.env.UNI_PLATFORM)
// TODO subpackages
pagesJson.pages.forEach((page) => {
......
......@@ -24,6 +24,7 @@
"compare-versions": "^3.6.0",
"debug": "^4.3.1",
"estree-walker": "^2.0.2",
"fast-glob": "^3.2.7",
"fs-extra": "^10.0.0",
"hash-sum": "^2.0.0",
"jsonc-parser": "^3.0.0",
......
import fs from 'fs'
import path from 'path'
import { sync } from 'fast-glob'
import { extend } from '@vue/shared'
import { I18N_JSON_DELIMITERS, once } from '@dcloudio/uni-shared'
import { parseJson, parseManifestJsonOnce } from './json'
import { M } from './messages'
......@@ -39,6 +42,27 @@ export function initI18nOptions(
export const initI18nOptionsOnce = once(initI18nOptions)
const localeJsonRE = /uni-app.*.json/
export function isUniAppLocaleFile(filepath: string) {
if (!filepath) {
return false
}
return localeJsonRE.test(path.basename(filepath))
}
function parseLocaleJson(filepath: string) {
let jsonObj = parseJson(fs.readFileSync(filepath, 'utf8'))
if (isUniAppLocaleFile(filepath)) {
jsonObj = jsonObj.common || {}
}
return jsonObj
}
export function getLocaleFiles(cwd: string) {
return sync('*.json', { cwd, absolute: true })
}
export function initLocales(dir: string, withMessages: boolean = true) {
if (!fs.existsSync(dir)) {
return {}
......@@ -46,9 +70,17 @@ export function initLocales(dir: string, withMessages: boolean = true) {
return fs.readdirSync(dir).reduce((res, filename) => {
if (path.extname(filename) === '.json') {
try {
res[path.basename(filename).replace('.json', '')] = withMessages
? parseJson(fs.readFileSync(path.join(dir, filename), 'utf8'))
: {}
const locale = path
.basename(filename)
.replace(/(uni-app.)?(.*).json/, '$2')
if (withMessages) {
extend(
res[locale] || (res[locale] = {}),
parseLocaleJson(path.join(dir, filename))
)
} else {
res[locale] = {}
}
} catch (e) {}
}
return res
......
......@@ -9,14 +9,10 @@ export function initI18n(manifestJson: Record<string, any>) {
true
)
if (i18nOptions) {
if (manifestJson.plus.tabBar) {
manifestJson.plus.tabBar = JSON.parse(
compileI18nJsonStr(
JSON.stringify(manifestJson.plus.tabBar),
i18nOptions
)
)
}
manifestJson = JSON.parse(
compileI18nJsonStr(JSON.stringify(manifestJson), i18nOptions)
)
manifestJson.fallbackLocale = i18nOptions.locale
}
return manifestJson
}
......@@ -33,8 +33,8 @@ export function normalizeAppManifestJson(
manifestJson,
pagesJson
)
initI18n(manifestJson)
return manifestJson
return initI18n(manifestJson)
}
export * from './env'
......
import fs from 'fs'
import path from 'path'
import { parseJson } from '../../json'
export function getLocales(inputDir: string) {
const localesDir = path.resolve(inputDir, 'locale')
if (fs.existsSync(localesDir)) {
return fs.readdirSync(localesDir).reduce((res, filename) => {
if (path.extname(filename) === '.json') {
res[filename.replace('.json', '')] =
parseJson(fs.readFileSync(path.join(localesDir, filename), 'utf8')) ||
{}
}
return res
}, {} as Record<string, Record<string, string>>)
}
return {}
}
......@@ -262,7 +262,7 @@ delete ${globalName}['____'+appid+'____']
nvue,
locale,
fallbackLocale,
locales:Object.keys(locales).reduce((res,name)=>{res[name.replace(/\\.\\/locale\\/(.*).json/,'$1')]=locales[name].default;return res},{}),
locales:Object.keys(locales).reduce((res,name)=>{const locale=name.replace(/\\.\\/locale\\/(uni-app.)?(.*).json/,'$2');extend(res[locale]||(res[locale]={}),locales[name].default);return res},{}),
router,
})
`
......
'use strict';
var version = "3.0.0-alpha-3000020210827004";
var version = "3.0.0-alpha-3000020210913001";
const STAT_VERSION = version;
const STAT_URL = 'https://tongji.dcloud.io/uni/stat';
......
var version = "3.0.0-alpha-3000020210827004";
var version = "3.0.0-alpha-3000020210913001";
const STAT_VERSION = version;
const STAT_URL = 'https://tongji.dcloud.io/uni/stat';
......
import { Plugin } from 'vite'
import { parse } from 'jsonc-parser'
import { preJs } from '@dcloudio/uni-cli-shared'
import { preJs, isUniAppLocaleFile } from '@dcloudio/uni-cli-shared'
import { VitePluginUniResolvedOptions } from '../..'
const jsonExtRE = /\.json($|\?)(?!commonjs-proxy)/
......@@ -17,8 +17,12 @@ export function uniJsonPlugin(options: VitePluginUniResolvedOptions): Plugin {
if (code.includes('#endif')) {
code = preJs(code)
}
let jsonObj = parse(code)
if (isUniAppLocaleFile(id)) {
jsonObj = jsonObj.common || {}
}
return {
code: JSON.stringify(parse(code)),
code: JSON.stringify(jsonObj),
map: null,
}
},
......
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册