From b9244ea2d36cb28e32e1135fee57a1d9fe615766 Mon Sep 17 00:00:00 2001 From: yuanzh Date: Sun, 21 Apr 2024 00:27:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BD=BF=E7=94=A8=E6=9B=B2=E7=BA=BF?= =?UTF-8?q?=E6=95=91=E5=9B=BD=E7=9A=84=E6=96=B9=E5=BC=8F=E8=AE=A9=E5=B0=8F?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E6=94=AF=E6=8C=81=E5=B8=A6=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E7=9A=84=E5=A4=9A=E8=AF=AD=E7=B3=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/locale/en.json | 1 + src/locale/index.ts | 15 +++++++++++++++ src/locale/zh-Hans.json | 3 ++- src/manifest.json | 2 +- src/pages/index/i18n.vue | 3 ++- 5 files changed, 21 insertions(+), 3 deletions(-) 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 @@