|
|
|
|
|
|
|
|
|
package language |
|
|
|
import ( |
|
"html/template" |
|
"strings" |
|
|
|
"github.com/GoAdminGroup/go-admin/modules/config" |
|
"golang.org/x/text/language" |
|
) |
|
|
|
var ( |
|
EN = language.English.String() |
|
CN = language.Chinese.String() |
|
JP = language.Japanese.String() |
|
TC = language.TraditionalChinese.String() |
|
PTBR = language.BrazilianPortuguese.String() |
|
) |
|
|
|
func FixedLanguageKey(key string) string { |
|
if key == "en" { |
|
return EN |
|
} |
|
if key == "cn" { |
|
return CN |
|
} |
|
if key == "jp" { |
|
return JP |
|
} |
|
if key == "tc" { |
|
return TC |
|
} |
|
if key == "pt-br" { |
|
return PTBR |
|
} |
|
return key |
|
} |
|
|
|
var Langs = [...]string{EN, CN, JP, TC} |
|
|
|
|
|
func Get(value string) string { |
|
return GetWithScope(value) |
|
} |
|
|
|
|
|
func GetWithScope(value string, scopes ...string) string { |
|
if config.GetLanguage() == "" { |
|
return value |
|
} |
|
|
|
return GetWithScopeAndLanguageSet(value, config.GetLanguage(), scopes...) |
|
} |
|
|
|
|
|
func GetWithLang(value, lang string) string { |
|
if lang == "" { |
|
lang = config.GetLanguage() |
|
} |
|
return GetWithScopeAndLanguageSet(value, lang) |
|
} |
|
|
|
|
|
func GetWithScopeAndLanguageSet(value, lang string, scopes ...string) string { |
|
if locale, ok := Lang[lang][JoinScopes(scopes)+strings.ToLower(value)]; ok { |
|
return locale |
|
} |
|
|
|
return value |
|
} |
|
|
|
|
|
func GetFromHtml(value template.HTML, scopes ...string) template.HTML { |
|
if config.GetLanguage() == "" { |
|
return value |
|
} |
|
|
|
if locale, ok := Lang[config.GetLanguage()][JoinScopes(scopes)+strings.ToLower(string(value))]; ok { |
|
return template.HTML(locale) |
|
} |
|
|
|
return value |
|
} |
|
|
|
|
|
func WithScopes(value string, scopes ...string) string { |
|
return JoinScopes(scopes) + strings.ToLower(value) |
|
} |
|
|
|
type LangSet map[string]string |
|
|
|
func (l LangSet) Add(key, value string) { |
|
l[key] = value |
|
} |
|
|
|
func (l LangSet) Combine(set LangSet) LangSet { |
|
for k, s := range set { |
|
l[k] = s |
|
} |
|
return l |
|
} |
|
|
|
|
|
type LangMap map[string]LangSet |
|
|
|
|
|
var Lang = LangMap{ |
|
language.Chinese.String(): cn, |
|
language.English.String(): en, |
|
language.Japanese.String(): jp, |
|
language.TraditionalChinese.String(): tc, |
|
language.BrazilianPortuguese.String(): ptbr, |
|
|
|
"cn": cn, |
|
"en": en, |
|
"jp": jp, |
|
"tc": tc, |
|
"pt-br": ptbr, |
|
} |
|
|
|
|
|
func (lang LangMap) Get(value string) string { |
|
return lang.GetWithScope(value) |
|
} |
|
|
|
|
|
func (lang LangMap) GetWithScope(value string, scopes ...string) string { |
|
if config.GetLanguage() == "" { |
|
return value |
|
} |
|
|
|
if locale, ok := lang[config.GetLanguage()][JoinScopes(scopes)+strings.ToLower(value)]; ok { |
|
return locale |
|
} |
|
|
|
return value |
|
} |
|
|
|
|
|
func Add(key string, lang map[string]string) { |
|
Lang[key] = lang |
|
} |
|
|
|
|
|
func AppendTo(lang string, set map[string]string) { |
|
for key, value := range set { |
|
Lang[lang][key] = value |
|
} |
|
} |
|
|
|
func JoinScopes(scopes []string) string { |
|
j := "" |
|
for _, scope := range scopes { |
|
if scope != "" { |
|
j += scope + "." |
|
} |
|
} |
|
return j |
|
} |
|
|