diff --git a/packages/uni-core/src/i18n/component.ts b/packages/uni-core/src/i18n/component.ts index d70ae459d72beeb0f2c9bd775369f49a73d80aec..231bc56d232d05e041d9e4e88a0a0ee0ba0f35f5 100644 --- a/packages/uni-core/src/i18n/component.ts +++ b/packages/uni-core/src/i18n/component.ts @@ -8,6 +8,7 @@ export function initNavigationBarI18n( return defineI18nProperties(navigationBar, [ ['titleText'], ['searchInput', 'placeholder'], + ['buttons', 'text'], ]) as [boolean, boolean] } } diff --git a/packages/uni-core/src/i18n/useI18n.ts b/packages/uni-core/src/i18n/useI18n.ts index 36e07481b1df8850d87790afe2c3398701d4f54d..952bdd1f3adb99ba6ce8a74e59313c14882e4589 100644 --- a/packages/uni-core/src/i18n/useI18n.ts +++ b/packages/uni-core/src/i18n/useI18n.ts @@ -31,9 +31,18 @@ export function formatI18n(message: string) { function resolveJsonObj( jsonObj: Record | undefined, names: string[] -): Record | undefined { +): Record | Array> | undefined { if (names.length === 1) { if (jsonObj) { + const _isI18nStr = (value: any) => + isString(value) && isI18nStr(value, I18N_JSON_DELIMITERS) + const _name = names[0] + if ( + Array.isArray(jsonObj) && + jsonObj.some((item) => _isI18nStr(item[_name])) + ) { + return jsonObj + } const value = jsonObj[names[0]] if (isString(value) && isI18nStr(value, I18N_JSON_DELIMITERS)) { return jsonObj @@ -58,15 +67,21 @@ export function defineI18nProperty(obj: Record, names: string[]) { return false } const prop = names[names.length - 1] - let value = jsonObj[prop] - Object.defineProperty(jsonObj, prop, { - get() { - return formatI18n(value) - }, - set(v) { - value = v - }, - }) + if (Array.isArray(jsonObj)) { + jsonObj + .filter((item) => isI18nStr(item[prop], I18N_JSON_DELIMITERS)) + .forEach((item) => defineI18nProperty(item, [prop])) + } else { + let value = jsonObj[prop] + Object.defineProperty(jsonObj, prop, { + get() { + return formatI18n(value) + }, + set(v) { + value = v + }, + }) + } return true } diff --git a/packages/uni-h5/src/framework/components/page/pageHead.tsx b/packages/uni-h5/src/framework/components/page/pageHead.tsx index 2d53a4efd84c468a343ff4526cda4ac07bf25361..bb44f958dfec8d82f509f767e5688c299b9feac4 100644 --- a/packages/uni-h5/src/framework/components/page/pageHead.tsx +++ b/packages/uni-h5/src/framework/components/page/pageHead.tsx @@ -363,27 +363,39 @@ function usePageHeadButton( if (btn.fontFamily) { iconStyle.fontFamily = btn.fontFamily } - return { - btnClass: { - // 类似这样的大量重复的字符串,会在gzip时压缩大小,无需在代码层考虑优化相同字符串 - 'uni-page-head-btn': true, - 'uni-page-head-btn-red-dot': !!(btn.redDot || btn.badgeText), - 'uni-page-head-btn-select': !!btn.select, - }, - btnStyle: { - backgroundColor: isTransparent ? btn.background : 'transparent', - width: btn.width, - }, - btnText: - btn.fontSrc && btn.fontFamily ? btn.text.replace('\\u', '&#x') : btn.text, - btnIconPath: ICON_PATHS[btn.type], - badgeText: btn.badgeText, - iconStyle, - onClick() { - invokeHook(pageId, ON_NAVIGATION_BAR_BUTTON_TAP, extend({ index }, btn)) + return new Proxy( + { + btnClass: { + // 类似这样的大量重复的字符串,会在gzip时压缩大小,无需在代码层考虑优化相同字符串 + 'uni-page-head-btn': true, + 'uni-page-head-btn-red-dot': !!(btn.redDot || btn.badgeText), + 'uni-page-head-btn-select': !!btn.select, + }, + btnStyle: { + backgroundColor: isTransparent ? btn.background : 'transparent', + width: btn.width, + }, + btnText: '', + btnIconPath: ICON_PATHS[btn.type], + badgeText: btn.badgeText, + iconStyle, + onClick() { + invokeHook(pageId, ON_NAVIGATION_BAR_BUTTON_TAP, extend({ index }, btn)) + }, + btnSelect: btn.select, }, - btnSelect: btn.select, - } + { + get(target, key, receiver) { + if (['btnText'].includes(key as string)) { + return btn.fontSrc && btn.fontFamily + ? btn.text.replace('\\u', '&#x') + : btn.text + } else { + return Reflect.get(target, key, receiver) + } + }, + } + ) } type PageHeadSearchInput = ReturnType