File size: 3,607 Bytes
530729e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
// Copyright 2019 GoAdmin Core Team. All rights reserved.
// Use of this source code is governed by a Apache-2.0 style
// license that can be found in the LICENSE file.
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}
// Get return the value of default scope.
func Get(value string) string {
return GetWithScope(value)
}
// GetWithScope return the value of given scopes.
func GetWithScope(value string, scopes ...string) string {
if config.GetLanguage() == "" {
return value
}
return GetWithScopeAndLanguageSet(value, config.GetLanguage(), scopes...)
}
// GetWithLang return the value of given language set.
func GetWithLang(value, lang string) string {
if lang == "" {
lang = config.GetLanguage()
}
return GetWithScopeAndLanguageSet(value, lang)
}
// GetWithScopeAndLanguageSet return the value of given scopes and language set.
func GetWithScopeAndLanguageSet(value, lang string, scopes ...string) string {
if locale, ok := Lang[lang][JoinScopes(scopes)+strings.ToLower(value)]; ok {
return locale
}
return value
}
// GetFromHtml return the value of given scopes and template.HTML 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
}
// WithScopes join scopes prefix and the 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
}
// LangMap is the map of language packages.
type LangMap map[string]LangSet
// Lang is the global LangMap.
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,
}
// Get get the value from LangMap.
func (lang LangMap) Get(value string) string {
return lang.GetWithScope(value)
}
// GetWithScope get the value from LangMap with given scopes.
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
}
// Add add a language package to the Lang.
func Add(key string, lang map[string]string) {
Lang[key] = lang
}
// AppendTo add more language translations to the given language set.
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
}
|