github.com/oam-dev/kubevela@v1.9.11/references/docgen/i18n.go (about) 1 /* 2 Copyright 2022 The KubeVela Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package docgen 18 19 import ( 20 "encoding/json" 21 "log" 22 "strings" 23 24 "github.com/oam-dev/kubevela/pkg/utils" 25 ) 26 27 // Language is used to define the language 28 type Language string 29 30 var ( 31 // En is english, the default language 32 En = I18n{lang: LangEn} 33 // Zh is Chinese 34 Zh = I18n{lang: LangZh} 35 ) 36 37 const ( 38 // LangEn is english, the default language 39 LangEn Language = "English" 40 // LangZh is Chinese 41 LangZh Language = "Chinese" 42 ) 43 44 // I18n will automatically get translated data 45 type I18n struct { 46 lang Language 47 } 48 49 // LoadI18nData will load i18n data for the package 50 func LoadI18nData(path string) { 51 52 log.Printf("loading i18n data from %s", path) 53 data, err := utils.ReadRemoteOrLocalPath(path, false) 54 if err != nil { 55 log.Println("ignore using the i18n data", err) 56 return 57 } 58 var dat = map[string]map[Language]string{} 59 err = json.Unmarshal(data, &dat) 60 if err != nil { 61 log.Println("ignore using the i18n data", err) 62 return 63 } 64 65 for k, v := range dat { 66 if _, ok := v[LangEn]; !ok { 67 v[LangEn] = k 68 } 69 k = strings.ToLower(k) 70 ed, ok := i18nDoc[k] 71 if !ok { 72 ed = map[Language]string{} 73 } 74 for sk, sv := range v { 75 sv = strings.TrimSpace(sv) 76 if sv == "" { 77 continue 78 } 79 ed[sk] = sv 80 } 81 i18nDoc[k] = ed 82 } 83 } 84 85 // Language return the language used in i18n instance 86 func (i *I18n) Language() Language { 87 if i == nil || i.lang == "" { 88 return En.Language() 89 } 90 return i.lang 91 } 92 93 func (i *I18n) trans(str string) (string, bool) { 94 dd, ok := i18nDoc[str] 95 if !ok { 96 return str, false 97 } 98 data := dd[i.lang] 99 if data == "" { 100 return str, true 101 } 102 return data, true 103 } 104 105 // Get translate for the string 106 func (i *I18n) Get(str string) string { 107 if i == nil || i.lang == "" { 108 return En.Get(str) 109 } 110 if data, ok := i.trans(str); ok { 111 return data 112 } 113 str = strings.TrimSpace(str) 114 if data, ok := i.trans(str); ok { 115 return data 116 } 117 str = strings.TrimSuffix(str, ".") 118 if data, ok := i.trans(str); ok { 119 return data 120 } 121 str = strings.TrimSuffix(str, "。") 122 if data, ok := i.trans(str); ok { 123 return data 124 } 125 raw := str 126 str = strings.TrimSpace(str) 127 if data, ok := i.trans(str); ok { 128 return data 129 } 130 str = strings.ToLower(str) 131 if data, ok := i.trans(str); ok { 132 return data 133 } 134 return raw 135 } 136 137 // Definitions are all the words and phrases for internationalization in cli and docs 138 var i18nDoc = map[string]map[Language]string{ 139 ".": { 140 LangZh: "。", 141 LangEn: ".", 142 }, 143 "Description": { 144 LangZh: "描述", 145 LangEn: "Description", 146 }, 147 "Scope": { 148 LangZh: "适用范围", 149 LangEn: "Scope", 150 }, 151 "Examples": { 152 LangZh: "示例", 153 LangEn: "Examples", 154 }, 155 "Specification": { 156 LangZh: "参数说明", 157 LangEn: "Specification", 158 }, 159 "AlibabaCloud": { 160 LangZh: "阿里云", 161 LangEn: "Alibaba Cloud", 162 }, 163 "AWS": { 164 LangZh: "AWS", 165 LangEn: "AWS", 166 }, 167 "Azure": { 168 LangZh: "Azure", 169 LangEn: "Azure", 170 }, 171 "Name": { 172 LangZh: "名称", 173 LangEn: "Name", 174 }, 175 "Type": { 176 LangZh: "类型", 177 LangEn: "Type", 178 }, 179 "Required": { 180 LangZh: "是否必须", 181 LangEn: "Required", 182 }, 183 "Default": { 184 LangZh: "默认值", 185 LangEn: "Default", 186 }, 187 "Apply To Component Types": { 188 LangZh: "适用于组件类型", 189 LangEn: "Apply To Component Types", 190 }, 191 }