github.com/oam-dev/kubevela@v1.9.11/hack/docgen/def/collect-translation/collect.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 main 18 19 import ( 20 "encoding/json" 21 "flag" 22 "fmt" 23 "log" 24 "os" 25 "strings" 26 ) 27 28 var i18nDoc = map[string]map[string]string{} 29 30 const cnComp = "../kubevela.io/i18n/zh/docusaurus-plugin-content-docs/current/end-user/components/references.md" 31 const enComp = "../kubevela.io/docs/end-user/components/references.md" 32 33 /* 34 const enTrait = "../kubevela.io/docs/end-user/traits/references.md" 35 const cnTrait = "../kubevela.io/i18n/zh/docusaurus-plugin-content-docs/current/end-user/traits/references.md" 36 const enPolicy = "../kubevela.io/docs/end-user/policies/references.md" 37 const cnPolicy = "../kubevela.io/i18n/zh/docusaurus-plugin-content-docs/current/end-user/policies/references.md" 38 const cnWorkflow = "../kubevela.io/i18n/zh/docusaurus-plugin-content-docs/current/end-user/workflow/built-in-workflow-defs.md" 39 const enWorkflow = "../kubevela.io/docs/end-user/workflow/built-in-workflow-defs.md" 40 */ 41 42 func main() { 43 44 pathCN := flag.String("path-cn", cnComp, "specify the path of chinese reference doc.") 45 pathEN := flag.String("path-en", enComp, "specify the path of english reference doc.") 46 path := flag.String("path", "", "path of existing i18n json data, if specified, it will read the file and keep the old data with append only.") 47 48 flag.Parse() 49 50 if *path != "" { 51 data, err := os.ReadFile(*path) 52 if err == nil { 53 err = json.Unmarshal(data, &i18nDoc) 54 if err != nil { 55 log.Fatalln(err) 56 } 57 } 58 } 59 60 paths := strings.Split(*pathEN, ";") 61 var enbuff string 62 for _, v := range paths { 63 if strings.TrimSpace(v) == "" { 64 continue 65 } 66 data, err := os.ReadFile(v) 67 if err != nil { 68 log.Fatalln(err) 69 } 70 enbuff += string(data) + "\n" 71 } 72 73 cnpaths := strings.Split(*pathCN, ";") 74 var cnbuff string 75 for _, v := range cnpaths { 76 if strings.TrimSpace(v) == "" { 77 continue 78 } 79 data, err := os.ReadFile(v) 80 if err != nil { 81 log.Fatalln(err) 82 } 83 cnbuff += string(data) + "\n" 84 } 85 86 var entable, cntable = map[string]string{}, map[string]string{} 87 ens := strings.Split(enbuff, "\n") 88 for _, v := range ens { 89 values := strings.Split(v, "|") 90 if len(values) < 4 { 91 continue 92 } 93 var a, b = 0, 1 94 if values[0] == "" { 95 a, b = 1, 2 96 } 97 key := strings.TrimSpace(values[a]) 98 desc := strings.Trim(strings.TrimSpace(values[b]), ".") 99 if strings.Contains(key, "----") { 100 continue 101 } 102 if strings.TrimSpace(desc) == "" { 103 continue 104 } 105 if len(entable[key]) > len(desc) { 106 continue 107 } 108 entable[key] = desc 109 } 110 111 cns := strings.Split(cnbuff, "\n") 112 for _, v := range cns { 113 values := strings.Split(v, "|") 114 if len(values) < 5 { 115 continue 116 } 117 var a, b = 0, 1 118 if values[0] == "" { 119 a, b = 1, 2 120 } 121 key := strings.TrimSpace(values[a]) 122 desc := strings.Trim(strings.TrimSpace(values[b]), ".") 123 if strings.Contains(key, "----") { 124 continue 125 } 126 if strings.TrimSpace(desc) == "" { 127 continue 128 } 129 if len(cntable[key]) > len(desc) { 130 continue 131 } 132 cntable[key] = desc 133 } 134 135 for k, v := range entable { 136 137 trans := i18nDoc[v] 138 if trans == nil { 139 trans = map[string]string{} 140 } 141 trans["Chinese"] = cntable[k] 142 // fmt.Println("Key=", k, " | ", v, " | ", cntable[k]) 143 i18nDoc[v] = trans 144 } 145 output, err := json.MarshalIndent(i18nDoc, "", "\t") 146 if err != nil { 147 log.Fatalln(err) 148 } 149 fmt.Println(string(output)) 150 }