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

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

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