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

feat(i18n): init locales

上级 14f199dc
...@@ -1654,9 +1654,12 @@ var serviceContext = (function () { ...@@ -1654,9 +1654,12 @@ var serviceContext = (function () {
} }
} }
const i18n = initVueI18n(locale, messages ); const i18n = initVueI18n(
locale,
messages
);
const t = i18n.t; const t = i18n.t;
const i18nMixin = i18n.mixin = { const i18nMixin = (i18n.mixin = {
beforeCreate () { beforeCreate () {
const unwatch = i18n.i18n.watchLocale(() => { const unwatch = i18n.i18n.watchLocale(() => {
this.$forceUpdate(); this.$forceUpdate();
...@@ -1670,7 +1673,7 @@ var serviceContext = (function () { ...@@ -1670,7 +1673,7 @@ var serviceContext = (function () {
return t(key, values) return t(key, values)
} }
} }
}; });
const getLocale = i18n.getLocale; const getLocale = i18n.getLocale;
function initAppLocale (Vue, appVm, locale) { function initAppLocale (Vue, appVm, locale) {
...@@ -1678,7 +1681,7 @@ var serviceContext = (function () { ...@@ -1678,7 +1681,7 @@ var serviceContext = (function () {
locale: locale || i18n.getLocale() locale: locale || i18n.getLocale()
}); });
const localeWatchers = []; const localeWatchers = [];
appVm.$watchLocale = (fn) => { appVm.$watchLocale = fn => {
localeWatchers.push(fn); localeWatchers.push(fn);
}; };
Object.defineProperty(appVm, '$locale', { Object.defineProperty(appVm, '$locale', {
...@@ -1709,10 +1712,7 @@ var serviceContext = (function () { ...@@ -1709,10 +1712,7 @@ var serviceContext = (function () {
return message return message
} }
function resolveJsonObj ( function resolveJsonObj (jsonObj, names) {
jsonObj,
names
) {
if (names.length === 1) { if (names.length === 1) {
if (jsonObj) { if (jsonObj) {
const value = jsonObj[names[0]]; const value = jsonObj[names[0]];
...@@ -1726,11 +1726,8 @@ var serviceContext = (function () { ...@@ -1726,11 +1726,8 @@ var serviceContext = (function () {
return resolveJsonObj(jsonObj && jsonObj[name], names) return resolveJsonObj(jsonObj && jsonObj[name], names)
} }
function defineI18nProperties ( function defineI18nProperties (obj, names) {
obj, return names.map(name => defineI18nProperty(obj, name))
names
) {
return names.map((name) => defineI18nProperty(obj, name))
} }
function defineI18nProperty (obj, names) { function defineI18nProperty (obj, names) {
...@@ -1762,6 +1759,15 @@ var serviceContext = (function () { ...@@ -1762,6 +1759,15 @@ var serviceContext = (function () {
['searchInput', 'placeholder'] ['searchInput', 'placeholder']
]) ])
} }
}
function initI18n () {
const localeKeys = Object.keys(__uniConfig.locales || {});
if (localeKeys.length) {
localeKeys.forEach((locale) =>
i18n.add(locale, __uniConfig.locales[locale])
);
}
} }
const setClipboardData = { const setClipboardData = {
...@@ -8538,53 +8544,53 @@ var serviceContext = (function () { ...@@ -8538,53 +8544,53 @@ var serviceContext = (function () {
const WEBVIEW_REMOVED = 'webviewRemoved'; const WEBVIEW_REMOVED = 'webviewRemoved';
const WEBVIEW_ID_PREFIX = 'webviewId'; const WEBVIEW_ID_PREFIX = 'webviewId';
function createButtonOnClick(index) { function createButtonOnClick (index) {
return function onClick(btn) { return function onClick (btn) {
const pages = getCurrentPages(); const pages = getCurrentPages();
if (!pages.length) { if (!pages.length) {
return; return
} }
btn.index = index; btn.index = index;
const page = pages[pages.length - 1]; const page = pages[pages.length - 1];
page.$vm && page.$vm &&
page.$vm.__call_hook && page.$vm.__call_hook &&
page.$vm.__call_hook("onNavigationBarButtonTap", btn); page.$vm.__call_hook('onNavigationBarButtonTap', btn);
}; }
} }
function parseTitleNViewButtons(titleNView) { function parseTitleNViewButtons (titleNView) {
const buttons = titleNView.buttons; const buttons = titleNView.buttons;
if (!Array.isArray(buttons)) { if (!Array.isArray(buttons)) {
return titleNView; return titleNView
} }
buttons.forEach((btn, index) => { buttons.forEach((btn, index) => {
btn.onclick = createButtonOnClick(index); btn.onclick = createButtonOnClick(index);
}); });
return titleNView; return titleNView
} }
function parseTitleNView(id, routeOptions) { function parseTitleNView (id, routeOptions) {
const windowOptions = routeOptions.window; const windowOptions = routeOptions.window;
const titleNView = windowOptions.titleNView; const titleNView = windowOptions.titleNView;
routeOptions.meta.statusBarStyle = routeOptions.meta.statusBarStyle =
windowOptions.navigationBarTextStyle === "black" ? "dark" : "light"; windowOptions.navigationBarTextStyle === 'black' ? 'dark' : 'light';
if ( if (
// 无头 // 无头
titleNView === false || titleNView === false ||
titleNView === "false" || titleNView === 'false' ||
(windowOptions.navigationStyle === "custom" && (windowOptions.navigationStyle === 'custom' &&
!isPlainObject(titleNView)) || !isPlainObject(titleNView)) ||
(windowOptions.transparentTitle === "always" && !isPlainObject(titleNView)) (windowOptions.transparentTitle === 'always' && !isPlainObject(titleNView))
) { ) {
return false; return false
} }
const titleImage = windowOptions.titleImage || ""; const titleImage = windowOptions.titleImage || '';
const transparentTitle = windowOptions.transparentTitle || "none"; const transparentTitle = windowOptions.transparentTitle || 'none';
const titleNViewTypeList = { const titleNViewTypeList = {
none: "default", none: 'default',
auto: "transparent", auto: 'transparent',
always: "float" always: 'float'
}; };
const navigationBarBackgroundColor = const navigationBarBackgroundColor =
...@@ -8592,52 +8598,52 @@ var serviceContext = (function () { ...@@ -8592,52 +8598,52 @@ var serviceContext = (function () {
const ret = { const ret = {
autoBackButton: !routeOptions.meta.isQuit, autoBackButton: !routeOptions.meta.isQuit,
titleText: titleText:
titleImage === "" ? windowOptions.navigationBarTitleText || "" : "", titleImage === '' ? windowOptions.navigationBarTitleText || '' : '',
titleColor: titleColor:
windowOptions.navigationBarTextStyle === "black" ? "#000000" : "#ffffff", windowOptions.navigationBarTextStyle === 'black' ? '#000000' : '#ffffff',
type: titleNViewTypeList[transparentTitle], type: titleNViewTypeList[transparentTitle],
backgroundColor: backgroundColor:
/^#[a-z0-9]{6}$/i.test(navigationBarBackgroundColor) || /^#[a-z0-9]{6}$/i.test(navigationBarBackgroundColor) ||
navigationBarBackgroundColor === "transparent" navigationBarBackgroundColor === 'transparent'
? navigationBarBackgroundColor ? navigationBarBackgroundColor
: "#f7f7f7", : '#f7f7f7',
tags: tags:
titleImage === "" titleImage === ''
? [] ? []
: [ : [
{ {
tag: "img", tag: 'img',
src: titleImage, src: titleImage,
position: { position: {
left: "auto", left: 'auto',
top: "auto", top: 'auto',
width: "auto", width: 'auto',
height: "26px" height: '26px'
}
} }
] }
]
}; };
if (isPlainObject(titleNView)) { if (isPlainObject(titleNView)) {
return initTitleNViewI18n( return initTitleNViewI18n(
id, id,
Object.assign(ret, parseTitleNViewButtons(titleNView)) Object.assign(ret, parseTitleNViewButtons(titleNView))
); )
} }
return initTitleNViewI18n(id, ret); return initTitleNViewI18n(id, ret)
} }
function initTitleNViewI18n(id, titleNView) { function initTitleNViewI18n (id, titleNView) {
const i18nResult = initNavigationBarI18n(titleNView); const i18nResult = initNavigationBarI18n(titleNView);
if (!i18nResult) { if (!i18nResult) {
return titleNView; return titleNView
} }
const [titleTextI18n, searchInputPlaceholderI18n] = i18nResult; const [titleTextI18n, searchInputPlaceholderI18n] = i18nResult;
if (titleTextI18n || searchInputPlaceholderI18n) { if (titleTextI18n || searchInputPlaceholderI18n) {
uni.onLocaleChange(() => { uni.onLocaleChange(() => {
const webview = plus.webview.getWebviewById(id + ""); const webview = plus.webview.getWebviewById(id + '');
if (!webview) { if (!webview) {
return; return
} }
const newTitleNView = {}; const newTitleNView = {};
if (titleTextI18n) { if (titleTextI18n) {
...@@ -8648,15 +8654,15 @@ var serviceContext = (function () { ...@@ -8648,15 +8654,15 @@ var serviceContext = (function () {
placeholder: titleNView.searchInput.placeholder placeholder: titleNView.searchInput.placeholder
}; };
} }
if (process.env.NODE_ENV !== "production") { if (process.env.NODE_ENV !== 'production') {
console.log("[uni-app] updateWebview", webview.id, newTitleNView); console.log('[uni-app] updateWebview', webview.id, newTitleNView);
} }
webview.setStyle({ webview.setStyle({
titleNView: newTitleNView titleNView: newTitleNView
}); });
}); });
} }
return titleNView; return titleNView
} }
function parsePullToRefresh (routeOptions) { function parsePullToRefresh (routeOptions) {
...@@ -22794,6 +22800,8 @@ var serviceContext = (function () { ...@@ -22794,6 +22800,8 @@ var serviceContext = (function () {
} }
}; };
initI18n();
// 挂靠在uni上,暂不做全局导出 // 挂靠在uni上,暂不做全局导出
uni$1.__$wx__ = wx; uni$1.__$wx__ = wx;
...@@ -22808,7 +22816,8 @@ var serviceContext = (function () { ...@@ -22808,7 +22816,8 @@ var serviceContext = (function () {
__registerPage: registerPage, __registerPage: registerPage,
uni: uni$1, uni: uni$1,
getApp: getApp$1, getApp: getApp$1,
getCurrentPages: getCurrentPages$1 getCurrentPages: getCurrentPages$1,
EventChannel
}; };
return index$1; return index$1;
......
const fs = require("fs"); const fs = require('fs')
const path = require("path"); const path = require('path')
const loaderUtils = require("loader-utils"); const loaderUtils = require('loader-utils')
const { const {
parsePages, parsePages,
normalizePath, normalizePath,
parsePagesJson, parsePagesJson,
parseManifestJson parseManifestJson
} = require("@dcloudio/uni-cli-shared"); } = require('@dcloudio/uni-cli-shared')
const { const {
updateAppJson, updateAppJson,
updatePageJson, updatePageJson,
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,
initAutoImportComponents initAutoImportComponents
} = require("@dcloudio/uni-cli-shared/lib/pages"); } = require('@dcloudio/uni-cli-shared/lib/pages')
const uniI18n = require("@dcloudio/uni-cli-i18n"); 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 { initI18nOptions } = require('@dcloudio/uni-cli-shared/lib/i18n')
const { parseI18nJson } = require("@dcloudio/uni-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
} }
return jsonObj; return jsonObj
} }
module.exports = function(content, map) { module.exports = function (content, map) {
this.cacheable && this.cacheable(); this.cacheable && this.cacheable()
initTheme(); initTheme()
let isAppView = false; let isAppView = false
if (this.resourceQuery) { if (this.resourceQuery) {
const params = loaderUtils.parseQuery(this.resourceQuery); const params = loaderUtils.parseQuery(this.resourceQuery)
isAppView = params.type === "view"; isAppView = params.type === 'view'
} }
// const pagesJsonJsPath = path.resolve(process.env.UNI_INPUT_DIR, pagesJsonJsFileName) // const pagesJsonJsPath = path.resolve(process.env.UNI_INPUT_DIR, pagesJsonJsFileName)
const manifestJsonPath = path.resolve( const manifestJsonPath = path.resolve(
process.env.UNI_INPUT_DIR, process.env.UNI_INPUT_DIR,
"manifest.json" 'manifest.json'
); )
const manifestJson = parseManifestJson( const manifestJson = parseManifestJson(
fs.readFileSync(manifestJsonPath, "utf8") fs.readFileSync(manifestJsonPath, 'utf8')
); )
// this.addDependency(pagesJsonJsPath) // this.addDependency(pagesJsonJsPath)
this.addDependency(manifestJsonPath); this.addDependency(manifestJsonPath)
let pagesJson = parsePagesJson(content, { let pagesJson = parsePagesJson(content, {
addDependency: file => { addDependency: file => {
(process.UNI_PAGES_DEPS || (process.UNI_PAGES_DEPS = new Set())).add( (process.UNI_PAGES_DEPS || (process.UNI_PAGES_DEPS = new Set())).add(
normalizePath(file) normalizePath(file)
); )
this.addDependency(file); this.addDependency(file)
} }
}); })
if (!pagesJson.pages || pagesJson.pages.length === 0) { if (!pagesJson.pages || pagesJson.pages.length === 0) {
console.error(uniI18n.__("pagesLoader.pagesNodeCannotNull")); console.error(uniI18n.__('pagesLoader.pagesNodeCannotNull'))
process.exit(0); process.exit(0)
} }
if (global.uniPlugin.defaultTheme) { if (global.uniPlugin.defaultTheme) {
pagesJson = parseTheme(pagesJson); pagesJson = parseTheme(pagesJson)
this.addDependency(path.resolve(process.env.UNI_INPUT_DIR, "theme.json")); this.addDependency(path.resolve(process.env.UNI_INPUT_DIR, 'theme.json'))
} }
// 组件自动导入配置 // 组件自动导入配置
process.UNI_AUTO_SCAN_COMPONENTS = !( process.UNI_AUTO_SCAN_COMPONENTS = !(
pagesJson.easycom && pagesJson.easycom.autoscan === false pagesJson.easycom && pagesJson.easycom.autoscan === false
); )
initAutoImportComponents(pagesJson.easycom); initAutoImportComponents(pagesJson.easycom)
// TODO 与 usingComponents 放在一块读取设置 // TODO 与 usingComponents 放在一块读取设置
if (manifestJson.transformPx === false) { if (manifestJson.transformPx === false) {
process.UNI_TRANSFORM_PX = false; process.UNI_TRANSFORM_PX = false
} else { } else {
process.UNI_TRANSFORM_PX = true; process.UNI_TRANSFORM_PX = true
} }
if (process.env.UNI_PLATFORM === "h5") { if (process.env.UNI_PLATFORM === 'h5') {
return this.callback( return this.callback(
null, null,
require("./platforms/h5")(pagesJson, manifestJson, this), require('./platforms/h5')(pagesJson, manifestJson, this),
map map
); )
} }
if (process.env.UNI_PLATFORM === "quickapp-native") { if (process.env.UNI_PLATFORM === 'quickapp-native') {
return this.callback( return this.callback(
null, null,
require("./platforms/quickapp-native")(pagesJson, manifestJson, this), require('./platforms/quickapp-native')(pagesJson, manifestJson, this),
map map
); )
} }
// 仅限小程序 // 仅限小程序
if (process.env.UNI_PLATFORM !== "app-plus") { if (process.env.UNI_PLATFORM !== 'app-plus') {
const i18nOptions = initI18nOptions( const i18nOptions = initI18nOptions(
process.env.UNI_PLATFORM, process.env.UNI_PLATFORM,
process.env.UNI_INPUT_DIR, process.env.UNI_INPUT_DIR,
true, true,
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))
); )
} }
); )
} }
const jsonFiles = require("./platforms/" + process.env.UNI_PLATFORM)( const jsonFiles = require('./platforms/' + process.env.UNI_PLATFORM)(
pagesJson, pagesJson,
manifestJson, manifestJson,
isAppView isAppView
); )
if (jsonFiles && jsonFiles.length) { if (jsonFiles && jsonFiles.length) {
if (process.env.UNI_USING_V3) { if (process.env.UNI_USING_V3) {
let appConfigContent = ""; let appConfigContent = ''
jsonFiles.forEach(jsonFile => { jsonFiles.forEach(jsonFile => {
if (jsonFile) { if (jsonFile) {
if (!isAppView && jsonFile.name === "manifest.json") { if (!isAppView && jsonFile.name === 'manifest.json') {
const content = JSON.parse(jsonFile.content); const content = JSON.parse(jsonFile.content)
if ( if (
!content.launch_path && !content.launch_path &&
content.plus["uni-app"].nvueLaunchMode === "fast" content.plus['uni-app'].nvueLaunchMode === 'fast'
) { ) {
console.log( console.log(
uniI18n.__("pagesLoader.nvueFirstPageStartModeIsFast", { uniI18n.__('pagesLoader.nvueFirstPageStartModeIsFast', {
0: "https://ask.dcloud.net.cn/article/36749" 0: 'https://ask.dcloud.net.cn/article/36749'
}) })
); )
} }
} }
if (jsonFile.name === "define-pages.js") { if (jsonFile.name === 'define-pages.js') {
appConfigContent = jsonFile.content; appConfigContent = jsonFile.content
} else { } else {
// app-view 不需要生成 app-config-service.js,manifest.json // app-view 不需要生成 app-config-service.js,manifest.json
!isAppView && this.emitFile(jsonFile.name, jsonFile.content); !isAppView && this.emitFile(jsonFile.name, jsonFile.content)
} }
} }
}); })
return this.callback(null, appConfigContent, map); return this.callback(null, appConfigContent, map)
} }
if (process.env.UNI_USING_NATIVE || process.env.UNI_USING_V3_NATIVE) { if (process.env.UNI_USING_NATIVE || process.env.UNI_USING_V3_NATIVE) {
let appConfigContent = ""; let appConfigContent = ''
jsonFiles.forEach(jsonFile => { jsonFiles.forEach(jsonFile => {
if (jsonFile) { if (jsonFile) {
if ( if (
jsonFile.name === "app-config.js" || jsonFile.name === 'app-config.js' ||
jsonFile.name === "define-pages.js" jsonFile.name === 'define-pages.js'
) { ) {
appConfigContent = jsonFile.content; appConfigContent = jsonFile.content
} else { } else {
this.emitFile(jsonFile.name, jsonFile.content); this.emitFile(jsonFile.name, jsonFile.content)
} }
} }
}); })
return this.callback(null, appConfigContent, map); return this.callback(null, appConfigContent, map)
} }
jsonFiles.forEach(jsonFile => { jsonFiles.forEach(jsonFile => {
if (jsonFile) { if (jsonFile) {
if (jsonFile.name === "app") { if (jsonFile.name === 'app') {
updateAppJson(jsonFile.name, renameUsingComponents(jsonFile.content)); updateAppJson(jsonFile.name, renameUsingComponents(jsonFile.content))
} else { } else {
updateProjectJson(jsonFile.name, jsonFile.content); updateProjectJson(jsonFile.name, jsonFile.content)
} }
} }
}); })
} }
this.callback(null, "", map); this.callback(null, '', map)
}; }
import { initVueI18n, isI18nStr } from '@dcloudio/uni-i18n' import {
import { isStr } from 'uni-shared' initVueI18n,
isI18nStr
} from '@dcloudio/uni-i18n'
import {
isStr
} from 'uni-shared'
import en from './en.json' import en from './en.json'
import es from './es.json' import es from './es.json'
...@@ -154,3 +159,12 @@ export function initTabBarI18n (tabBar) { ...@@ -154,3 +159,12 @@ export function initTabBarI18n (tabBar) {
} }
return tabBar return tabBar
} }
export function initI18n () {
const localeKeys = Object.keys(__uniConfig.locales || {})
if (localeKeys.length) {
localeKeys.forEach((locale) =>
i18n.add(locale, __uniConfig.locales[locale])
)
}
}
import { import {
uni uni
} from 'uni-core/service/uni' } from 'uni-core/service/uni'
import EventChannel from 'uni-helpers/EventChannel'
import { import {
invokeCallbackHandler, invokeCallbackHandler,
removeCallbackHandler removeCallbackHandler
...@@ -31,6 +31,12 @@ import { ...@@ -31,6 +31,12 @@ import {
import vuePlugin from './framework/plugins' import vuePlugin from './framework/plugins'
import {
initI18n
} from 'uni-helpers/i18n'
initI18n()
// 挂靠在uni上,暂不做全局导出 // 挂靠在uni上,暂不做全局导出
uni.__$wx__ = wx uni.__$wx__ = wx
...@@ -45,5 +51,6 @@ export default { ...@@ -45,5 +51,6 @@ export default {
__registerPage: registerPage, __registerPage: registerPage,
uni, uni,
getApp, getApp,
getCurrentPages getCurrentPages,
} EventChannel
}
...@@ -10,6 +10,11 @@ import { ...@@ -10,6 +10,11 @@ import {
getCurrentPages getCurrentPages
} }
from 'uni-core/service/plugins/app' from 'uni-core/service/plugins/app'
import {
initI18n
} from 'uni-helpers/i18n'
initI18n()
initOn(UniServiceJSBridge.on, { initOn(UniServiceJSBridge.on, {
getApp, getApp,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册