Files
backend_v2/internal/service/locale.go
toom1996 30c9f21da4 update
2026-08-26 10:45:21 +08:00

34 lines
945 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package service
// 默认语言与受支持语言(与前端 i18n/config.ts 的 LOCALES 保持一致)。
const (
defaultLocale = "en"
localeCN = "cn"
localeEN = "en"
)
// normalizeLocale 把任意输入收敛为受支持的语言;非法或空回落默认 en。
func normalizeLocale(locale string) string {
if locale == localeCN || locale == localeEN {
return locale
}
return defaultLocale
}
// pickLocale 按 locale 选语言字段,缺翻译时回落另一语言,保证出参永远非空(除非两者皆空)。
//
// 这是 i18n 选列的唯一入口:repository 返回 en/cn 两列裸数据,service 在此按 locale 收敛成
// 对外单字段(name / title / description / brand_name),前端契约因此保持不变。
func pickLocale(en, cn, locale string) string {
if normalizeLocale(locale) == localeCN {
if cn != "" {
return cn
}
return en
}
if en != "" {
return en
}
return cn
}