diff --git a/src/locale/en.json b/src/locale/en.json index 1973f89..9362478 100644 --- a/src/locale/en.json +++ b/src/locale/en.json @@ -1,4 +1,5 @@ { "weight": "{heavy}KG", + "weight2": "{0}KG", "app.name": "En Title" } diff --git a/src/locale/index.ts b/src/locale/index.ts index 5f96737..9169581 100644 --- a/src/locale/index.ts +++ b/src/locale/index.ts @@ -34,4 +34,19 @@ export const translate = (localeKey: string) => { } return localeKey } + +/** + * formatString('已阅读并同意{0}和{1}','用户协议','隐私政策') -> 已阅读并同意用户协议和隐私政策 + * @param template + * @param values + * @returns + */ +export function formatString(template: string, ...values: any) { + console.log(template, values) + // 使用map来替换{0}, {1}, ...等占位符 + return template.replace(/{(\d+)}/g, (match, index) => { + const value = values[index] + return value !== undefined ? value : match + }) +} export default i18n diff --git a/src/locale/zh-Hans.json b/src/locale/zh-Hans.json index bd397a1..09d414c 100644 --- a/src/locale/zh-Hans.json +++ b/src/locale/zh-Hans.json @@ -1,4 +1,5 @@ { "app.name": "中文标题", - "weight": "{heavy}公斤" + "weight": "{heavy}公斤", + "weight2": "{0}公斤" } diff --git a/src/manifest.json b/src/manifest.json index f6ad007..0d67c09 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -1,5 +1,5 @@ { - "name": "unibest-base", + "name": "unibest", "appid": "H57F2ACE4", "description": "", "versionName": "1.0.0", diff --git a/src/pages/index/i18n.vue b/src/pages/index/i18n.vue index acfbc75..baa55b1 100644 --- a/src/pages/index/i18n.vue +++ b/src/pages/index/i18n.vue @@ -16,6 +16,7 @@ 多语言测试 {{ $t('app.name') }} {{ $t('weight', { heavy: 100 }) }} + {{ formatString(translate('weight2'), 100) }} 切换语言 @@ -35,7 +36,7 @@