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

fix(mp): i18n

上级 82fe3bc3
......@@ -1482,7 +1482,6 @@ var serviceContext = (function (vue) {
}
}
const ignoreVueI18n = true;
function watchAppLocale(appVm, i18n) {
// 需要保证 watch 的触发在组件渲染之前
if (appVm.$watchLocale) {
......@@ -1497,6 +1496,16 @@ var serviceContext = (function (vue) {
});
}
}
function getDefaultLocale() {
if (typeof uni !== 'undefined' && uni.getLocale) {
return uni.getLocale();
}
// 小程序平台,uni 和 uni-i18n 互相引用,导致访问不到 uni,故在 window 上挂了 getLocale
if (typeof window !== 'undefined' && window.getLocale) {
return window.getLocale();
}
return LOCALE_EN;
}
function initVueI18n(locale, messages = {}, fallbackLocale, watcher) {
// 兼容旧版本入参
if (typeof locale !== 'string') {
......@@ -1506,9 +1515,8 @@ var serviceContext = (function (vue) {
];
}
if (typeof locale !== 'string') {
locale =
(typeof uni !== 'undefined' && uni.getLocale && uni.getLocale()) ||
LOCALE_EN;
// 因为小程序平台,uni-i18n 和 uni 互相引用,导致此时访问 uni 时,为 undefined
locale = getDefaultLocale();
}
if (typeof fallbackLocale !== 'string') {
fallbackLocale =
......@@ -1530,33 +1538,32 @@ var serviceContext = (function (vue) {
};
}
else {
const appVm = getApp().$vm;
watchAppLocale(appVm, i18n);
if (!appVm.$t || !appVm.$i18n || ignoreVueI18n) {
// if (!locale) {
// i18n.setLocale(getDefaultLocale())
// }
/* eslint-disable no-func-assign */
t = function (key, values) {
let isWatchedAppLocale = false;
t = function (key, values) {
const appVm = getApp().$vm;
// 可能$vm还不存在,比如在支付宝小程序中,组件定义较早,在props的default里使用了t()函数(如uni-goods-nav),此时app还未初始化
// options: {
// type: Array,
// default () {
// return [{
// icon: 'shop',
// text: t("uni-goods-nav.options.shop"),
// }, {
// icon: 'cart',
// text: t("uni-goods-nav.options.cart")
// }]
// }
// },
if (appVm) {
// 触发响应式
appVm.$locale;
return i18n.t(key, values);
};
}
else {
/* eslint-disable no-func-assign */
t = function (key, values) {
const $i18n = appVm.$i18n;
const silentTranslationWarn = $i18n.silentTranslationWarn;
$i18n.silentTranslationWarn = true;
const msg = appVm.$t(key, values);
$i18n.silentTranslationWarn = silentTranslationWarn;
if (msg !== key) {
return msg;
if (!isWatchedAppLocale) {
isWatchedAppLocale = true;
watchAppLocale(appVm, i18n);
}
return i18n.t(key, $i18n.locale, values);
};
}
}
return i18n.t(key, values);
};
}
return t(key, values);
};
......
......@@ -2612,26 +2612,28 @@ var index$w = /* @__PURE__ */ defineBuiltInComponent({
const {
fixSize
} = useImageSize(rootRef, props2, state);
useImageLoader(state, {
trigger,
fixSize
});
useImageLoader(state, fixSize, trigger);
return () => {
const {
mode: mode2
} = props2;
const {
imgSrc,
modeStyle
modeStyle,
src
} = state;
let imgTsx;
{
imgTsx = vue.createVNode("img", {
"src": src,
"draggable": props2.draggable
}, null, 8, ["src", "draggable"]);
}
return vue.createVNode("uni-image", {
"ref": rootRef
}, [vue.createVNode("div", {
"style": modeStyle
}, null, 4), imgSrc ? vue.createVNode("img", {
"src": imgSrc,
"draggable": props2.draggable
}, null, 8, ["src", "draggable"]) : vue.createVNode("img", null, null), FIX_MODES[mode2] ? vue.createVNode(ResizeSensor, {
}, null, 4), imgTsx, FIX_MODES[mode2] ? vue.createVNode(ResizeSensor, {
"onResize": fixSize
}, null, 8, ["onResize"]) : vue.createVNode("span", null, null)], 512);
};
......@@ -2650,8 +2652,9 @@ function useImageState(rootRef, props2) {
opts[0] && (position = opts[0]);
opts[1] && (size = opts[1]);
}
const srcVal = imgSrc.value;
return `background-image:${srcVal ? 'url("' + srcVal + '")' : "none"};background-position:${position};background-size:${size};background-repeat:no-repeat;`;
return `background-image:${imgSrc.value ? 'url("' + imgSrc.value + '")' : "none"};
background-position:${position};
background-size:${size};`;
});
const state = vue.reactive({
rootEl: rootRef,
......@@ -2667,10 +2670,7 @@ function useImageState(rootRef, props2) {
});
return state;
}
function useImageLoader(state, {
trigger,
fixSize
}) {
function useImageLoader(state, fixSize, trigger) {
let img;
const setState = (width = 0, height = 0, imgSrc = "") => {
state.origWidth = width;
......@@ -2683,9 +2683,7 @@ function useImageLoader(state, {
setState();
return;
}
if (!img) {
img = new Image();
}
img = img || new Image();
img.onload = (evt) => {
const {
width,
......
......@@ -7759,26 +7759,28 @@ var index$r = /* @__PURE__ */ defineBuiltInComponent({
const {
fixSize
} = useImageSize(rootRef, props2, state2);
useImageLoader(state2, {
trigger,
fixSize
});
useImageLoader(state2, fixSize, trigger);
return () => {
const {
mode: mode2
} = props2;
const {
imgSrc,
modeStyle
modeStyle,
src
} = state2;
let imgTsx;
{
imgTsx = imgSrc ? createVNode("img", {
"src": imgSrc,
"draggable": props2.draggable
}, null, 8, ["src", "draggable"]) : createVNode("img", null, null);
}
return createVNode("uni-image", {
"ref": rootRef
}, [createVNode("div", {
"style": modeStyle
}, null, 4), imgSrc ? createVNode("img", {
"src": imgSrc,
"draggable": props2.draggable
}, null, 8, ["src", "draggable"]) : createVNode("img", null, null), FIX_MODES[mode2] ? createVNode(ResizeSensor, {
}, null, 4), imgTsx, FIX_MODES[mode2] ? createVNode(ResizeSensor, {
"onResize": fixSize
}, null, 8, ["onResize"]) : createVNode("span", null, null)], 512);
};
......@@ -7797,8 +7799,9 @@ function useImageState(rootRef, props2) {
opts[0] && (position = opts[0]);
opts[1] && (size = opts[1]);
}
const srcVal = imgSrc.value;
return `background-image:${srcVal ? 'url("' + srcVal + '")' : "none"};background-position:${position};background-size:${size};background-repeat:no-repeat;`;
return `background-image:${imgSrc.value ? 'url("' + imgSrc.value + '")' : "none"};
background-position:${position};
background-size:${size};`;
});
const state2 = reactive({
rootEl: rootRef,
......@@ -7820,10 +7823,7 @@ function useImageState(rootRef, props2) {
});
return state2;
}
function useImageLoader(state2, {
trigger,
fixSize
}) {
function useImageLoader(state2, fixSize, trigger) {
let img;
const setState = (width = 0, height = 0, imgSrc = "") => {
state2.origWidth = width;
......@@ -7836,9 +7836,7 @@ function useImageLoader(state2, {
setState();
return;
}
if (!img) {
img = new Image();
}
img = img || new Image();
img.onload = (evt) => {
const {
width,
......
......@@ -2,6 +2,9 @@
"input": {
"src/index.ts": ["dist/uni-i18n.es.js", "dist/uni-i18n.cjs.js"]
},
"replacements": {
"global": "global"
},
"compilerOptions": {
"declaration": true
}
......
......@@ -221,7 +221,6 @@ class I18n {
}
}
const ignoreVueI18n = true;
function watchAppLocale(appVm, i18n) {
// 需要保证 watch 的触发在组件渲染之前
if (appVm.$watchLocale) {
......@@ -236,6 +235,16 @@ function watchAppLocale(appVm, i18n) {
});
}
}
function getDefaultLocale() {
if (typeof uni !== 'undefined' && uni.getLocale) {
return uni.getLocale();
}
// 小程序平台,uni 和 uni-i18n 互相引用,导致访问不到 uni,故在 global 上挂了 getLocale
if (typeof global !== 'undefined' && global.getLocale) {
return global.getLocale();
}
return LOCALE_EN;
}
function initVueI18n(locale, messages = {}, fallbackLocale, watcher) {
// 兼容旧版本入参
if (typeof locale !== 'string') {
......@@ -245,9 +254,8 @@ function initVueI18n(locale, messages = {}, fallbackLocale, watcher) {
];
}
if (typeof locale !== 'string') {
locale =
(typeof uni !== 'undefined' && uni.getLocale && uni.getLocale()) ||
LOCALE_EN;
// 因为小程序平台,uni-i18n 和 uni 互相引用,导致此时访问 uni 时,为 undefined
locale = getDefaultLocale();
}
if (typeof fallbackLocale !== 'string') {
fallbackLocale =
......@@ -269,33 +277,32 @@ function initVueI18n(locale, messages = {}, fallbackLocale, watcher) {
};
}
else {
const appVm = getApp().$vm;
watchAppLocale(appVm, i18n);
if (!appVm.$t || !appVm.$i18n || ignoreVueI18n) {
// if (!locale) {
// i18n.setLocale(getDefaultLocale())
// }
/* eslint-disable no-func-assign */
t = function (key, values) {
let isWatchedAppLocale = false;
t = function (key, values) {
const appVm = getApp().$vm;
// 可能$vm还不存在,比如在支付宝小程序中,组件定义较早,在props的default里使用了t()函数(如uni-goods-nav),此时app还未初始化
// options: {
// type: Array,
// default () {
// return [{
// icon: 'shop',
// text: t("uni-goods-nav.options.shop"),
// }, {
// icon: 'cart',
// text: t("uni-goods-nav.options.cart")
// }]
// }
// },
if (appVm) {
// 触发响应式
appVm.$locale;
return i18n.t(key, values);
};
}
else {
/* eslint-disable no-func-assign */
t = function (key, values) {
const $i18n = appVm.$i18n;
const silentTranslationWarn = $i18n.silentTranslationWarn;
$i18n.silentTranslationWarn = true;
const msg = appVm.$t(key, values);
$i18n.silentTranslationWarn = silentTranslationWarn;
if (msg !== key) {
return msg;
if (!isWatchedAppLocale) {
isWatchedAppLocale = true;
watchAppLocale(appVm, i18n);
}
return i18n.t(key, $i18n.locale, values);
};
}
}
return i18n.t(key, values);
};
}
return t(key, values);
};
......
......@@ -217,7 +217,6 @@ class I18n {
}
}
const ignoreVueI18n = true;
function watchAppLocale(appVm, i18n) {
// 需要保证 watch 的触发在组件渲染之前
if (appVm.$watchLocale) {
......@@ -232,6 +231,16 @@ function watchAppLocale(appVm, i18n) {
});
}
}
function getDefaultLocale() {
if (typeof uni !== 'undefined' && uni.getLocale) {
return uni.getLocale();
}
// 小程序平台,uni 和 uni-i18n 互相引用,导致访问不到 uni,故在 global 上挂了 getLocale
if (typeof global !== 'undefined' && global.getLocale) {
return global.getLocale();
}
return LOCALE_EN;
}
function initVueI18n(locale, messages = {}, fallbackLocale, watcher) {
// 兼容旧版本入参
if (typeof locale !== 'string') {
......@@ -241,9 +250,8 @@ function initVueI18n(locale, messages = {}, fallbackLocale, watcher) {
];
}
if (typeof locale !== 'string') {
locale =
(typeof uni !== 'undefined' && uni.getLocale && uni.getLocale()) ||
LOCALE_EN;
// 因为小程序平台,uni-i18n 和 uni 互相引用,导致此时访问 uni 时,为 undefined
locale = getDefaultLocale();
}
if (typeof fallbackLocale !== 'string') {
fallbackLocale =
......@@ -265,33 +273,32 @@ function initVueI18n(locale, messages = {}, fallbackLocale, watcher) {
};
}
else {
const appVm = getApp().$vm;
watchAppLocale(appVm, i18n);
if (!appVm.$t || !appVm.$i18n || ignoreVueI18n) {
// if (!locale) {
// i18n.setLocale(getDefaultLocale())
// }
/* eslint-disable no-func-assign */
t = function (key, values) {
let isWatchedAppLocale = false;
t = function (key, values) {
const appVm = getApp().$vm;
// 可能$vm还不存在,比如在支付宝小程序中,组件定义较早,在props的default里使用了t()函数(如uni-goods-nav),此时app还未初始化
// options: {
// type: Array,
// default () {
// return [{
// icon: 'shop',
// text: t("uni-goods-nav.options.shop"),
// }, {
// icon: 'cart',
// text: t("uni-goods-nav.options.cart")
// }]
// }
// },
if (appVm) {
// 触发响应式
appVm.$locale;
return i18n.t(key, values);
};
}
else {
/* eslint-disable no-func-assign */
t = function (key, values) {
const $i18n = appVm.$i18n;
const silentTranslationWarn = $i18n.silentTranslationWarn;
$i18n.silentTranslationWarn = true;
const msg = appVm.$t(key, values);
$i18n.silentTranslationWarn = silentTranslationWarn;
if (msg !== key) {
return msg;
if (!isWatchedAppLocale) {
isWatchedAppLocale = true;
watchAppLocale(appVm, i18n);
}
return i18n.t(key, $i18n.locale, values);
};
}
}
return i18n.t(key, values);
};
}
return t(key, values);
};
......
......@@ -6,8 +6,6 @@ import {
LocaleWatcher,
} from './I18n'
const ignoreVueI18n = true
type Interpolate = (
key: string,
values?: Record<string, unknown> | Array<unknown>
......@@ -30,18 +28,16 @@ function watchAppLocale(appVm: any, i18n: I18n) {
}
}
// function getDefaultLocale() {
// if (typeof navigator !== 'undefined') {
// return (navigator as any).userLanguage || navigator.language
// }
// if (typeof plus !== 'undefined') {
// // TODO 待调整为最新的获取语言代码
// return plus.os.language
// }
// return uni.getSystemInfoSync().language
// }
const i18nInstances: I18n[] = []
function getDefaultLocale(): string {
if (typeof uni !== 'undefined' && uni.getLocale) {
return uni.getLocale()
}
// 小程序平台,uni 和 uni-i18n 互相引用,导致访问不到 uni,故在 global 上挂了 getLocale
if (typeof global !== 'undefined' && (global as any).getLocale) {
return (global as any).getLocale()
}
return LOCALE_EN
}
export function initVueI18n(
locale?: string,
......@@ -57,9 +53,8 @@ export function initVueI18n(
]
}
if (typeof locale !== 'string') {
locale =
(typeof uni !== 'undefined' && uni.getLocale && uni.getLocale()) ||
LOCALE_EN
// 因为小程序平台,uni-i18n 和 uni 互相引用,导致此时访问 uni 时,为 undefined
locale = getDefaultLocale()
}
if (typeof fallbackLocale !== 'string') {
fallbackLocale =
......@@ -74,8 +69,6 @@ export function initVueI18n(
watcher,
})
i18nInstances.push(i18n)
let t: Interpolate = (key, values) => {
if (typeof getApp !== 'function') {
// app view
......@@ -84,31 +77,31 @@ export function initVueI18n(
return i18n.t(key, values)
}
} else {
const appVm = getApp().$vm
watchAppLocale(appVm, i18n)
if (!appVm.$t || !appVm.$i18n || ignoreVueI18n) {
// if (!locale) {
// i18n.setLocale(getDefaultLocale())
// }
/* eslint-disable no-func-assign */
t = function (key, values) {
let isWatchedAppLocale = false
t = function (key, values) {
const appVm = getApp().$vm
// 可能$vm还不存在,比如在支付宝小程序中,组件定义较早,在props的default里使用了t()函数(如uni-goods-nav),此时app还未初始化
// options: {
// type: Array,
// default () {
// return [{
// icon: 'shop',
// text: t("uni-goods-nav.options.shop"),
// }, {
// icon: 'cart',
// text: t("uni-goods-nav.options.cart")
// }]
// }
// },
if (appVm) {
// 触发响应式
appVm.$locale
return i18n.t(key, values)
}
} else {
/* eslint-disable no-func-assign */
t = function (key, values) {
const $i18n = appVm.$i18n
const silentTranslationWarn = $i18n.silentTranslationWarn
$i18n.silentTranslationWarn = true
const msg = appVm.$t(key, values)
$i18n.silentTranslationWarn = silentTranslationWarn
if (msg !== key) {
return msg
if (!isWatchedAppLocale) {
isWatchedAppLocale = true
watchAppLocale(appVm, i18n)
}
return i18n.t(key, $i18n.locale, values)
}
return i18n.t(key, values)
}
}
return t(key, values)
......
......@@ -16,6 +16,7 @@
]
},
"replacements": {
"global": "global",
"__GLOBAL__": "my",
"__PLATFORM__": "\"mp-alipay\"",
"__PLATFORM_TITLE__": "支付宝小程序"
......
......@@ -754,7 +754,10 @@ const onLocaleChange = (fn) => {
if (onLocaleChangeCallbacks.indexOf(fn) === -1) {
onLocaleChangeCallbacks.push(fn);
}
};
};
if (typeof global !== 'undefined') {
global.getLocale = getLocale;
}
const baseApis = {
$on,
......
......@@ -16,6 +16,7 @@
]
},
"replacements": {
"global": "global",
"__GLOBAL__": "swan",
"__PLATFORM__": "\"mp-baidu\"",
"__PLATFORM_TITLE__": "百度小程序"
......
......@@ -754,7 +754,10 @@ const onLocaleChange = (fn) => {
if (onLocaleChangeCallbacks.indexOf(fn) === -1) {
onLocaleChangeCallbacks.push(fn);
}
};
};
if (typeof global !== 'undefined') {
global.getLocale = getLocale;
}
const baseApis = {
$on,
......
......@@ -28,3 +28,7 @@ export const onLocaleChange: typeof uni.onLocaleChange = (fn) => {
onLocaleChangeCallbacks.push(fn)
}
}
if (typeof global !== 'undefined') {
;(global as any).getLocale = getLocale
}
......@@ -16,6 +16,7 @@
]
},
"replacements": {
"global": "global",
"__GLOBAL__": "ks",
"__PLATFORM__": "\"mp-kuaishou\"",
"__PLATFORM_TITLE__": "快手小程序"
......
......@@ -754,7 +754,10 @@ const onLocaleChange = (fn) => {
if (onLocaleChangeCallbacks.indexOf(fn) === -1) {
onLocaleChangeCallbacks.push(fn);
}
};
};
if (typeof global !== 'undefined') {
global.getLocale = getLocale;
}
const baseApis = {
$on,
......
......@@ -16,6 +16,7 @@
]
},
"replacements": {
"global": "global",
"__GLOBAL__": "qq",
"__PLATFORM__": "\"mp-qq\"",
"__PLATFORM_TITLE__": "QQ小程序"
......
......@@ -754,7 +754,10 @@ const onLocaleChange = (fn) => {
if (onLocaleChangeCallbacks.indexOf(fn) === -1) {
onLocaleChangeCallbacks.push(fn);
}
};
};
if (typeof global !== 'undefined') {
global.getLocale = getLocale;
}
const baseApis = {
$on,
......
......@@ -16,6 +16,7 @@
]
},
"replacements": {
"global": "global",
"__GLOBAL__": "tt",
"__PLATFORM__": "\"mp-toutiao\"",
"__PLATFORM_TITLE__": "字节跳动小程序"
......
......@@ -754,7 +754,10 @@ const onLocaleChange = (fn) => {
if (onLocaleChangeCallbacks.indexOf(fn) === -1) {
onLocaleChangeCallbacks.push(fn);
}
};
};
if (typeof global !== 'undefined') {
global.getLocale = getLocale;
}
const baseApis = {
$on,
......
......@@ -16,6 +16,7 @@
]
},
"replacements": {
"global": "global",
"__GLOBAL__": "wx",
"__PLATFORM__": "\"mp-weixin\"",
"__PLATFORM_TITLE__": "微信小程序"
......
......@@ -718,7 +718,10 @@ const onLocaleChange = (fn) => {
if (onLocaleChangeCallbacks.indexOf(fn) === -1) {
onLocaleChangeCallbacks.push(fn);
}
};
};
if (typeof global !== 'undefined') {
global.getLocale = getLocale;
}
const baseApis = {
$on,
......
......@@ -16,6 +16,7 @@
]
},
"replacements": {
"global": "global",
"__GLOBAL__": "qa",
"__PLATFORM__": "\"quickapp-webview\"",
"__PLATFORM_TITLE__": "快应用(Webview)版"
......
......@@ -754,7 +754,10 @@ const onLocaleChange = (fn) => {
if (onLocaleChangeCallbacks.indexOf(fn) === -1) {
onLocaleChangeCallbacks.push(fn);
}
};
};
if (typeof global !== 'undefined') {
global.getLocale = getLocale;
}
const baseApis = {
$on,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册