uni-app 国际化开发:解决 vue-i18n 占位符丢失的跨端格式化方法 背景 在 uni-app 开发过程中,许多开发者都遇到过跨端项目国际化时的各类问题。以典型场景为例:某个列表或详情页需要展示接口返回的数量、时间、状态等信息。 接口返回的数据本身是正常的,例如: 复制代码{ title:
在 uni-app 开发过程中,许多开发者都遇到过跨端项目国际化时的各类问题。以典型场景为例:某个列表或详情页需要展示接口返回的数量、时间、状态等信息。

长期稳定更新的攒劲资源: >>>点此立即查看<<<
接口返回的数据本身是正常的,例如:
复制代码{
title: '任务名称',
totalCount: 1,
pointCount: 1,
startTime: '2026-06-30 10:59:00',
endTime: '2026-06-30 10:59:48',
type: 1
}
但页面显示结果却出现异常:
复制代码总用时:
张地图
个点位
开始时间:
结束时间:
数字和时间数据均丢失。
项目中原来的格式化方法代码如下:
复制代码const formatCountText = (localeKey, count) => t(localeKey).replace('{count}', count)
const formatTimeText = (localeKey, time) => t(localeKey).replace('{time}', time)
语言包配置如下:
复制代码{
totalTime: '总用时:{time}',
durationMinutes: '{count}分钟',
mapCount: '{count}张地图',
markerCount: '{count}个点位',
startTime: '开始时间:{time}',
endTime: '结束时间:{time}',
}
表面上看代码逻辑没有问题,但在 uni-app 的多端环境中,问题逐渐暴露。
该问题的根本原因在于:
复制代码t(localeKey)
返回时,{count}、{time} 等占位符可能已被 vue-i18n 内部处理掉。
代码原本预期的结果是:
复制代码t('module.mapCount')
// "{count}张地图"
实际获取到的内容可能为:
复制代码"张地图"
此时再执行:
复制代码.replace('{count}', count)
已经没有可替换的内容,最终显示为:
复制代码张地图
时间字段同理,会显示为:
复制代码开始时间:
t(key, params)在普通 Vue 项目中,可以采用以下写法:
复制代码t('module.mapCount', { count: 1 })
但在 uni-app 的 App、小程序等非 H5 端,vue-i18n 的参数插值可能存在兼容性问题,导致 {count} 无法被正常替换。
因此不能简单依赖:
复制代码t(localeKey, { count })
需要封装一个跨端稳定的方法。
期望在组件中统一使用:
复制代码tf(localeKey, data)
例如:
复制代码tf('module.mapCount', { count: 1 })
// "1张地图"tf('module.startTime', { time: '2026-06-30 10:59:00' })
// "开始时间:2026-06-30 10:59:00"
首先封装一个获取翻译文本的方法:
复制代码export const translate = (key: string): string => {
return i18n.global.t(key) as string
}
再封装手动替换占位符的方法:
复制代码export function formatI18n(template: string, data: Record<string, any>): string {
if (!template || !data) {
return template
} const match = /{(.*)}/g.exec(template) if (match) {
const variableList = match[0].replace('{', '').replace('}', '').split('.')
let result: any = data for (let i = 0; i < variableList.length; i++) {
result = result.[variableList[i]] ''
} return formatI18n(template.replace(match[0], String(result)), data)
} return template
}
该方法支持普通占位符:
复制代码'{count}个点位'
也支持嵌套属性:
复制代码'身高 {detail.height}'
translateFormat 复制代码export function translateFormat(key: string, data: Record<string, any>): string {
if (!data) {
return translate(key)
} if (isH5) {
return i18n.global.t(key, data) as string
} const template = translate(key)
return formatI18n(template, data)
}
这里的策略是:
这样可以兼顾 H5 和 App、小程序等不同环境。
为方便在组件中使用,可以再封装一层 useI18nFormat:
复制代码import { translate, formatI18n, translateFormat } from '@/locales'export const useI18nFormat = () => {
return {
translate,
formatI18n,
tf: translateFormat,
translateFormat,
}
}
在组件中使用:
复制代码import { useI18nFormat } from '@/hooks/useI18nFormat'const { tf } = useI18nFormat()
修改前:
复制代码const formatCountText = (localeKey, count) => t(localeKey).replace('{count}', count)
const formatTimeText = (localeKey, time) => t(localeKey).replace('{time}', time)
修改后:
复制代码const formatCountText = (localeKey, count) => tf(localeKey, { count })
const formatTimeText = (localeKey, time) => tf(localeKey, { time })
业务映射时:
复制代码countText: formatCountText('module.countText', item.count),
timeText: formatTimeText('module.startTime', item.startTime),
模板中:
复制代码{{ item.countText }}
{{ formatTimeText('module.endTime', item.endTime) }}
这类问题容易被误判为接口字段问题。
排查时建议按以下顺序进行:
"张地图"、"分钟",说明问题出在 i18n 格式化环节"1张地图" 但页面不显示,才继续排查模板或样式问题本次问题正是通过打印映射后的数据,确认接口数据已正常进入前端,但 {count}、{time} 在格式化阶段被处理掉。
在 uni-app 多端项目中,应避免依赖以下写法:
复制代码t(key).replace('{count}', count)
也不建议直接改为:
复制代码t(key, { count })
更稳妥的方式是封装统一的跨端格式化方法:
复制代码tf(key, params)
让组件只关注业务语义,无需关心当前运行环境是 H5、App 还是小程序。
最终代码将更加稳定,也便于统一维护。
侠游戏发布此文仅为了传递信息,不代表侠游戏网站认同其观点或证实其描述