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