提交 0ebdc0ce 编写于 作者: fxy060608's avatar fxy060608

feat(i18n): add uni-app.*.json

上级 1fc4725b
const fs = require('fs') const fs = require('fs')
const path = require('path') const path = require('path')
const { parseJson } = require('./json') const {
const { getManifestJson } = require('./manifest') parseJson
} = require('./json')
const {
getManifestJson
} = require('./manifest')
const delimiters = ['%', '%'] const delimiters = ['%', '%']
...@@ -36,17 +40,40 @@ function initI18nOptions ( ...@@ -36,17 +40,40 @@ function initI18nOptions (
} }
} }
const localeJsonRE = /uni-app.*.json/
function isUniAppLocaleFile (filepath) {
if (!filepath) {
return false
}
return localeJsonRE.test(path.basename(filepath))
}
function parseLocaleJson (filepath) {
let jsonObj = parseJson(fs.readFileSync(filepath, 'utf8'))
if (isUniAppLocaleFile(filepath)) {
jsonObj = jsonObj.common || {}
}
return jsonObj
}
function initLocales (dir, withMessages = true) { function initLocales (dir, withMessages = true) {
if (!fs.existsSync(dir)) { if (!fs.existsSync(dir)) {
return {} return {}
} }
return fs.readdirSync(dir).reduce((res, filename) => { return fs.readdirSync(dir).reduce((res, filename) => {
if (path.extname(filename) === '.json') { if (path.extname(filename) === '.json') {
try { const locale = path
res[path.basename(filename).replace('.json', '')] = withMessages .basename(filename)
? parseJson(fs.readFileSync(path.join(dir, filename), 'utf8')) .replace(/(uni-app.)?(.*).json/, '$2')
: {} if (withMessages) {
} catch (e) {} Object.assign(
res[locale] || (res[locale] = {}),
parseLocaleJson(path.join(dir, filename))
)
} else {
res[locale] = {}
}
} }
return res return res
}, {}) }, {})
...@@ -69,4 +96,4 @@ function resolveI18nLocale (platfrom, locales, locale) { ...@@ -69,4 +96,4 @@ function resolveI18nLocale (platfrom, locales, locale) {
module.exports = { module.exports = {
initLocales, initLocales,
initI18nOptions initI18nOptions
} }
#!/usr/bin/env node #!/usr/bin/env node
const path = require('path') const path = require('path')
const Program = require('commander') const Program = require('commander')
......
...@@ -16,7 +16,10 @@ const { ...@@ -16,7 +16,10 @@ const {
updateProjectJson updateProjectJson
} = require('@dcloudio/uni-cli-shared/lib/cache') } = require('@dcloudio/uni-cli-shared/lib/cache')
const { initTheme, parseTheme } = require('@dcloudio/uni-cli-shared/lib/theme') const {
initTheme,
parseTheme
} = require('@dcloudio/uni-cli-shared/lib/theme')
const { const {
// pagesJsonJsFileName, // pagesJsonJsFileName,
...@@ -27,11 +30,15 @@ const uniI18n = require('@dcloudio/uni-cli-i18n') ...@@ -27,11 +30,15 @@ const uniI18n = require('@dcloudio/uni-cli-i18n')
const parseStyle = require('./util').parseStyle const parseStyle = require('./util').parseStyle
const { initI18nOptions } = require('@dcloudio/uni-cli-shared/lib/i18n') const {
const { parseI18nJson } = require('@dcloudio/uni-i18n') initI18nOptions
} = require('@dcloudio/uni-cli-shared/lib/i18n')
const {
parseI18nJson
} = require('@dcloudio/uni-i18n')
// 将开发者手动设置的 usingComponents 调整名称,方便与自动解析到的 usingComponents 做最后合并 // 将开发者手动设置的 usingComponents 调整名称,方便与自动解析到的 usingComponents 做最后合并
function renameUsingComponents (jsonObj) { function renameUsingComponents(jsonObj) {
if (jsonObj.usingComponents) { if (jsonObj.usingComponents) {
jsonObj.customUsingComponents = jsonObj.usingComponents jsonObj.customUsingComponents = jsonObj.usingComponents
delete jsonObj.usingComponents delete jsonObj.usingComponents
...@@ -39,7 +46,7 @@ function renameUsingComponents (jsonObj) { ...@@ -39,7 +46,7 @@ function renameUsingComponents (jsonObj) {
return jsonObj return jsonObj
} }
module.exports = function (content, map) { module.exports = function(content, map) {
this.cacheable && this.cacheable() this.cacheable && this.cacheable()
initTheme() initTheme()
...@@ -60,6 +67,7 @@ module.exports = function (content, map) { ...@@ -60,6 +67,7 @@ module.exports = function (content, map) {
) )
// this.addDependency(pagesJsonJsPath) // this.addDependency(pagesJsonJsPath)
this.addContextDependency(path.resolve(process.env.UNI_INPUT_DIR, 'locale'))
this.addDependency(manifestJsonPath) this.addDependency(manifestJsonPath)
let pagesJson = parsePagesJson(content, { let pagesJson = parsePagesJson(content, {
...@@ -117,20 +125,24 @@ module.exports = function (content, map) { ...@@ -117,20 +125,24 @@ module.exports = function (content, map) {
true true
) )
if (i18nOptions) { if (i18nOptions) {
const { locale, locales, delimiters } = i18nOptions const {
locale,
locales,
delimiters
} = i18nOptions
parseI18nJson(pagesJson, locales[locale], delimiters) parseI18nJson(pagesJson, locales[locale], delimiters)
} }
} }
if (!process.env.UNI_USING_V3) { if (!process.env.UNI_USING_V3) {
parsePages( parsePages(
pagesJson, pagesJson,
function (page) { function(page) {
updatePageJson( updatePageJson(
page.path, page.path,
renameUsingComponents(parseStyle(page.style)) renameUsingComponents(parseStyle(page.style))
) )
}, },
function (root, page) { function(root, page) {
updatePageJson( updatePageJson(
normalizePath(path.join(root, page.path)), normalizePath(path.join(root, page.path)),
renameUsingComponents(parseStyle(page.style, root)) renameUsingComponents(parseStyle(page.style, root))
...@@ -202,4 +214,4 @@ module.exports = function (content, map) { ...@@ -202,4 +214,4 @@ module.exports = function (content, map) {
} }
this.callback(null, '', map) this.callback(null, '', map)
} }
...@@ -3,10 +3,20 @@ const fsExtra = require('fs-extra') ...@@ -3,10 +3,20 @@ const fsExtra = require('fs-extra')
const path = require('path') const path = require('path')
const merge = require('merge') const merge = require('merge')
const { normalizePath, getFlexDirection } = require('@dcloudio/uni-cli-shared') const {
const { compileI18nJsonStr } = require('@dcloudio/uni-i18n') normalizePath,
const { initI18nOptions } = require('@dcloudio/uni-cli-shared/lib/i18n') getFlexDirection
const { hasOwn, parseStyle } = require('../../util') } = require('@dcloudio/uni-cli-shared')
const {
compileI18nJsonStr
} = require('@dcloudio/uni-i18n')
const {
initI18nOptions
} = require('@dcloudio/uni-cli-shared/lib/i18n')
const {
hasOwn,
parseStyle
} = require('../../util')
const wxPageOrientationMapping = { const wxPageOrientationMapping = {
auto: [ auto: [
...@@ -95,7 +105,9 @@ function updateFileFlag (appJson) { ...@@ -95,7 +105,9 @@ function updateFileFlag (appJson) {
} }
module.exports = function (pagesJson, userManifestJson, isAppView) { module.exports = function (pagesJson, userManifestJson, isAppView) {
const { app } = require('../mp')(pagesJson, userManifestJson) const {
app
} = require('../mp')(pagesJson, userManifestJson)
const manifest = { const manifest = {
name: 'manifest' name: 'manifest'
...@@ -123,8 +135,7 @@ module.exports = function (pagesJson, userManifestJson, isAppView) { ...@@ -123,8 +135,7 @@ module.exports = function (pagesJson, userManifestJson, isAppView) {
// 用户配置覆盖默认配置 // 用户配置覆盖默认配置
manifestJson = merge.recursive( manifestJson = merge.recursive(
true, true,
manifestJson, manifestJson, {
{
id: userManifestJson.appid || '', id: userManifestJson.appid || '',
name: userManifestJson.name || '', name: userManifestJson.name || '',
description: userManifestJson.description || '', description: userManifestJson.description || '',
...@@ -133,8 +144,7 @@ module.exports = function (pagesJson, userManifestJson, isAppView) { ...@@ -133,8 +144,7 @@ module.exports = function (pagesJson, userManifestJson, isAppView) {
code: userManifestJson.versionCode code: userManifestJson.versionCode
}, },
language: userManifestJson.locale language: userManifestJson.locale
}, }, {
{
plus: userManifestJson['app-plus'] plus: userManifestJson['app-plus']
} }
) )
...@@ -201,7 +211,10 @@ module.exports = function (pagesJson, userManifestJson, isAppView) { ...@@ -201,7 +211,10 @@ module.exports = function (pagesJson, userManifestJson, isAppView) {
if (nvuePages && nvuePages.length) { if (nvuePages && nvuePages.length) {
const pages = {} const pages = {}
nvuePages.forEach(({ path, style }) => { nvuePages.forEach(({
path,
style
}) => {
pages[path] = { pages[path] = {
window: parseStyle(style), window: parseStyle(style),
nvue: true nvue: true
...@@ -388,14 +401,20 @@ module.exports = function (pagesJson, userManifestJson, isAppView) { ...@@ -388,14 +401,20 @@ module.exports = function (pagesJson, userManifestJson, isAppView) {
return ( return (
path.replace(/\.html$/, '.nvue') === key || path.replace(/\.html$/, '.nvue') === key ||
path.replace(/\.html$/, '.nvue') + '.nvue' === key || path.replace(/\.html$/, '.nvue') + '.nvue' === key ||
subNVues.find(({ path }) => path === key.replace(/\.nvue$/, '')) subNVues.find(({
path
}) => path === key.replace(/\.nvue$/, ''))
) )
}) && }) &&
!pagesJson.pages.find(({ style = {} }) => { !pagesJson.pages.find(({
style = {}
}) => {
style = Object.assign(style, style['app-plus']) style = Object.assign(style, style['app-plus'])
const subNVues = style.subNVues || [] const subNVues = style.subNVues || []
return subNVues.find( return subNVues.find(
({ path }) => path === key.replace(/\.nvue$/, '') ({
path
}) => path === key.replace(/\.nvue$/, '')
) )
}) })
) { ) {
...@@ -491,8 +510,7 @@ module.exports = function (pagesJson, userManifestJson, isAppView) { ...@@ -491,8 +510,7 @@ module.exports = function (pagesJson, userManifestJson, isAppView) {
pagesJson.tabBar.list && pagesJson.tabBar.list &&
pagesJson.tabBar.list.length pagesJson.tabBar.list.length
) { ) {
const tabBar = (manifestJson.plus.tabBar = Object.assign( const tabBar = (manifestJson.plus.tabBar = Object.assign({},
{},
pagesJson.tabBar pagesJson.tabBar
)) ))
const borderStyles = { const borderStyles = {
...@@ -518,9 +536,9 @@ module.exports = function (pagesJson, userManifestJson, isAppView) { ...@@ -518,9 +536,9 @@ module.exports = function (pagesJson, userManifestJson, isAppView) {
const item = tabBar.list.find( const item = tabBar.list.find(
page => page =>
page.pagePath === page.pagePath ===
(process.env.UNI_USING_NATIVE (process.env.UNI_USING_NATIVE
? appJson.entryPagePath ? appJson.entryPagePath
: entryPagePath) : entryPagePath)
) )
if (item) { if (item) {
tabBar.child = ['lauchwebview'] tabBar.child = ['lauchwebview']
...@@ -535,8 +553,8 @@ module.exports = function (pagesJson, userManifestJson, isAppView) { ...@@ -535,8 +553,8 @@ module.exports = function (pagesJson, userManifestJson, isAppView) {
true true
) )
if (i18nOptions) { if (i18nOptions) {
manifestJson.plus.tabBar = JSON.parse( manifestJson = JSON.parse(
compileI18nJsonStr(JSON.stringify(tabBar), i18nOptions) compileI18nJsonStr(JSON.stringify(manifestJson), i18nOptions)
) )
manifestJson.fallbackLocale = i18nOptions.locale manifestJson.fallbackLocale = i18nOptions.locale
} }
...@@ -579,8 +597,7 @@ module.exports = function (pagesJson, userManifestJson, isAppView) { ...@@ -579,8 +597,7 @@ module.exports = function (pagesJson, userManifestJson, isAppView) {
} }
return require('./index.v3')( return require('./index.v3')(
appJson, appJson,
manifestJson, manifestJson, {
{
manifest, manifest,
pagesJson, pagesJson,
normalizeNetworkTimeout normalizeNetworkTimeout
...@@ -589,4 +606,4 @@ module.exports = function (pagesJson, userManifestJson, isAppView) { ...@@ -589,4 +606,4 @@ module.exports = function (pagesJson, userManifestJson, isAppView) {
) )
} }
return [app, manifest] return [app, manifest]
} }
...@@ -458,7 +458,7 @@ global.__uniConfig.sdkConfigs = ${JSON.stringify(sdkConfigs)}; ...@@ -458,7 +458,7 @@ global.__uniConfig.sdkConfigs = ${JSON.stringify(sdkConfigs)};
global.__uniConfig.qqMapKey = ${JSON.stringify(qqMapKey)}; global.__uniConfig.qqMapKey = ${JSON.stringify(qqMapKey)};
global.__uniConfig.locale = ${JSON.stringify(locale)}; global.__uniConfig.locale = ${JSON.stringify(locale)};
global.__uniConfig.fallbackLocale = ${JSON.stringify(manifestJson.fallbackLocale)}; global.__uniConfig.fallbackLocale = ${JSON.stringify(manifestJson.fallbackLocale)};
global.__uniConfig.locales = locales.keys().reduce((res,key)=>{res[key.replace(/\\.\\/(.*).json/,'$1')]=locales(key);return res},{}); global.__uniConfig.locales = locales.keys().reduce((res,key)=>{const locale=key.replace(/\\.\\/(uni-app.)?(.*).json/,'$2');const messages = locales(key);Object.assign(res[locale]||(res[locale]={}),messages.common||messages);return res},{});
global.__uniConfig.nvue = ${JSON.stringify({ 'flex-direction': getFlexDirection(manifestJson['app-plus']) })} global.__uniConfig.nvue = ${JSON.stringify({ 'flex-direction': getFlexDirection(manifestJson['app-plus']) })}
global.__uniConfig.__webpack_chunk_load__ = __webpack_chunk_load__ global.__uniConfig.__webpack_chunk_load__ = __webpack_chunk_load__
${genRegisterPageVueComponentsCode(pageComponents)} ${genRegisterPageVueComponentsCode(pageComponents)}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册