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

feat(i18n): init locales

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