121 lines
3.2 KiB
Go
121 lines
3.2 KiB
Go
package utils
|
||
|
||
import (
|
||
"fmt"
|
||
"strings"
|
||
|
||
"github.com/mozillazg/go-pinyin"
|
||
)
|
||
|
||
// reportTypeTranslations locale 到 数据报告/竞品报告 翻译的映射
|
||
var reportTypeTranslations = map[string]map[string]string{
|
||
"zh-CN": {
|
||
"数据报告": "数据报告",
|
||
"竞品报告": "竞品报告",
|
||
},
|
||
"zh-TW": {
|
||
"数据报告": "數據報告",
|
||
"竞品报告": "競合報告",
|
||
},
|
||
"EN": {
|
||
"数据报告": "Data Report",
|
||
"竞品报告": "Competitor Report",
|
||
},
|
||
"de-DE": {
|
||
"数据报告": "Datenbericht",
|
||
"竞品报告": "Wettbewerbsanalyse",
|
||
},
|
||
"ja-JP": {
|
||
"数据报告": "データレポート",
|
||
"竞品报告": "競合分析レポート",
|
||
},
|
||
}
|
||
|
||
// pinyinTitles 中文称谓"老师"对应的拼音形式(用于检测)
|
||
var pinyinTitles = []string{"lao shi", "laoshi"}
|
||
|
||
// ConvertNameToPinyin 将中文姓名转换为拼音,单词首字母大写,单词间用空格分隔。
|
||
// 对于非中文 locale,会去除"老师"称谓。
|
||
// 示例:"王旗老师" -> "Wang Qi"
|
||
func ConvertNameToPinyin(name string) string {
|
||
args := pinyin.NewArgs()
|
||
slices := pinyin.Pinyin(name, args)
|
||
|
||
var result []string
|
||
for _, s := range slices {
|
||
if len(s) > 0 {
|
||
// 首字母大写
|
||
runes := []rune(s[0])
|
||
if len(runes) > 0 {
|
||
uppered := string([]rune{runes[0] - 32}) + string(runes[1:])
|
||
result = append(result, uppered)
|
||
}
|
||
}
|
||
}
|
||
|
||
pinyinName := joinWithSpace(result)
|
||
|
||
// 对于非中文 locale,去除拼音 "Lao Shi"
|
||
lowerName := strings.ToLower(pinyinName)
|
||
for _, pt := range pinyinTitles {
|
||
if strings.HasSuffix(lowerName, pt) {
|
||
// 提取去掉称谓后的姓名部分
|
||
nameWithoutTitle := strings.TrimSuffix(pinyinName, " Lao Shi")
|
||
nameWithoutTitle = strings.TrimSuffix(nameWithoutTitle, " Laoshi")
|
||
return nameWithoutTitle
|
||
}
|
||
}
|
||
|
||
return pinyinName
|
||
}
|
||
|
||
// joinWithSpace 用空格连接字符串,过滤空字符串
|
||
func joinWithSpace(parts []string) string {
|
||
var nonEmpty []string
|
||
for _, p := range parts {
|
||
if p != "" {
|
||
nonEmpty = append(nonEmpty, p)
|
||
}
|
||
}
|
||
if len(nonEmpty) == 0 {
|
||
return ""
|
||
}
|
||
result := nonEmpty[0]
|
||
for i := 1; i < len(nonEmpty); i++ {
|
||
result += " " + nonEmpty[i]
|
||
}
|
||
return result
|
||
}
|
||
|
||
// ConvertReportName 将报告名转换为指定语言版本。
|
||
// prefix: YYYY-MM 格式,如 "2026-03"
|
||
// personName: 中文姓名,如 "王旗老师"
|
||
// reportType: "数据报告" 或 "竞品报告"
|
||
// locale: "zh-CN", "zh-TW", "EN", "de-DE", 或 "ja-JP"
|
||
func ConvertReportName(prefix, personName, reportType, locale string) string {
|
||
reportTypeTrans, ok := reportTypeTranslations[locale]
|
||
if !ok {
|
||
reportTypeTrans = reportTypeTranslations["zh-CN"]
|
||
}
|
||
|
||
translatedReportType, ok := reportTypeTrans[reportType]
|
||
if !ok {
|
||
translatedReportType = reportType
|
||
}
|
||
|
||
// 对于中文 locale(zh-CN, zh-TW),保持原样
|
||
if locale == "zh-CN" || locale == "zh-TW" {
|
||
displayName := personName
|
||
if locale == "zh-TW" {
|
||
// zh-TW 去掉"老师",繁体报告类型已通过 translatedReportType 获取
|
||
displayName = strings.ReplaceAll(personName, "老师", "")
|
||
}
|
||
return fmt.Sprintf("%s%s%s", prefix, displayName, translatedReportType)
|
||
}
|
||
|
||
// 对于其他 locale,转换为拼音
|
||
pinyinName := ConvertNameToPinyin(personName)
|
||
return fmt.Sprintf("%s %s %s", prefix, pinyinName, translatedReportType)
|
||
}
|
||
|