From 188554bf045cb0f2dd8888371e5052e936bb7459 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E8=8F=B2=E9=B8=BD?= <1020103647@qq.com>
Date: Sat, 4 May 2024 10:59:10 +0800
Subject: [PATCH] refine: i18n
---
src/locale/index.ts | 12 ++++++------
src/manifest.json | 2 +-
src/pages/index/i18n.vue | 4 ++--
3 files changed, 9 insertions(+), 9 deletions(-)
diff --git a/src/locale/index.ts b/src/locale/index.ts
index e3699d0..a3e9cb0 100644
--- a/src/locale/index.ts
+++ b/src/locale/index.ts
@@ -51,21 +51,21 @@ export function formatString(template: string, ...values: any) {
}
/**
- * formatStr('我是{name},身高{detail.height},体重{detail.weight}',{name:'张三',detail:{height:178,weight:'75kg'}})
+ * formatI18n('我是{name},身高{detail.height},体重{detail.weight}',{name:'张三',detail:{height:178,weight:'75kg'}})
* 暂不支持数组
- * @param template
- * @param data
+ * @param template 多语言模板字符串,eg: `我是{name}`
+ * @param obj 需要传递的数据对象,里面的key与多语言字符串对应,eg: `{name:'菲鸽'}`
* @returns
*/
-export function formatStr(template, data) {
+export function formatI18n(template, obj) {
const match = /\{(.*?)\}/g.exec(template)
if (match) {
const variableList = match[0].replace('{', '').replace('}', '').split('.')
- let result = data
+ let result = obj
for (let i = 0; i < variableList.length; i++) {
result = result[variableList[i]] || ''
}
- return formatStr(template.replace(match[0], result), data)
+ return formatI18n(template.replace(match[0], result), obj)
} else {
return template
}
diff --git a/src/manifest.json b/src/manifest.json
index 0d67c09..f6ad007 100644
--- a/src/manifest.json
+++ b/src/manifest.json
@@ -1,5 +1,5 @@
{
- "name": "unibest",
+ "name": "unibest-base",
"appid": "H57F2ACE4",
"description": "",
"versionName": "1.0.0",
diff --git a/src/pages/index/i18n.vue b/src/pages/index/i18n.vue
index c526f0c..e5dc40a 100644
--- a/src/pages/index/i18n.vue
+++ b/src/pages/index/i18n.vue
@@ -18,7 +18,7 @@
{{ $t('weight', { heavy: 100 }) }}
{{ formatString(translate('weight2'), 100) }}
- {{ formatStr(translate('introduction'), user) }}
+ {{ formatI18n(translate('introduction'), user) }}
切换语言
@@ -39,7 +39,7 @@