提交 3c47420f 编写于 作者: D DCloud_LXH

feat(i18n): h5 titleNView buttons text

上级 2b84f65d
......@@ -8,6 +8,7 @@ export function initNavigationBarI18n(
return defineI18nProperties(navigationBar, [
['titleText'],
['searchInput', 'placeholder'],
['buttons', 'text'],
]) as [boolean, boolean]
}
}
......
......@@ -31,9 +31,18 @@ export function formatI18n(message: string) {
function resolveJsonObj(
jsonObj: Record<string, any> | undefined,
names: string[]
): Record<string, any> | undefined {
): Record<string, any> | Array<Record<string, any>> | 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<string, any>, 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
}
......
......@@ -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<typeof usePageHeadSearchInput>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册