-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
196 lines (161 loc) · 4.79 KB
/
main.go
File metadata and controls
196 lines (161 loc) · 4.79 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Excel to JSON converter
// Author: RobinYang
// Date: 2026.1.16
// Description: This program reads an Excel file and converts each row into a separate JSON file.
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"path/filepath"
"regexp"
"slices"
"strings"
"github.com/mozillazg/go-pinyin"
"github.com/xuri/excelize/v2"
)
var (
version = "dev"
buildTime = "unknown"
gitCommit = "unknown"
)
const dart_file_template = `class LocaleKeys {
%s
}
`
// truncateTextBeforePinyin 在转换为拼音之前,如果文本转换为拼音后长度会超过8,则截取前4个和后4个字符
func truncateTextBeforePinyin(text string) string {
// 先转换为拼音检查长度
keySlice := pinyin.LazyPinyin(text, pinyin.NewArgs())
pinyinKey := strings.Join(keySlice, "")
// 如果拼音长度超过8,则对原始文本截取前4个和后4个字符
if len(pinyinKey) > 8 {
runes := []rune(text)
if len(runes) > 8 {
first4 := string(runes[:4])
last4 := string(runes[len(runes)-4:])
return first4 + "_" + last4
}
}
return text
}
// cleanText 清理文本:去掉空格和特殊字符,只保留字母、数字和中文字符
func cleanText(text string) string {
// 使用正则表达式,只保留字母、数字和中文字符
// \p{L} 匹配所有字母(包括中文),\p{N} 匹配所有数字
// 或者使用 [a-zA-Z0-9] 匹配英文和数字,[\u4e00-\u9fff] 匹配中文
re := regexp.MustCompile(`[^a-zA-Z0-9\p{Han}]`)
cleaned := re.ReplaceAllString(text, "")
return cleaned
}
// generateKey 生成key:先截取文本(如果需要),然后转换为拼音
func generateKey(text string) string {
truncatedText := truncateTextBeforePinyin(text)
keySlice := pinyin.LazyPinyin(truncatedText, pinyin.NewArgs())
key := strings.Join(keySlice, "")
return key
}
func main() {
input_file := flag.String("file", "./assets/abc.xlsx", "input excel file")
output_path := flag.String("output_path", "./", "output json file path")
dart_file_path := flag.String("dart_file_path", "./", "dart file output directory")
showVersion := flag.Bool("version", false, "show version")
flag.Parse()
if *showVersion {
fmt.Printf("Version: %s\nBuild Time: %s\nGit Commit: %s\n", version, buildTime, gitCommit)
return
}
fmt.Println("input_file:", *input_file)
fmt.Println("output_path:", *output_path)
f, err := excelize.OpenFile(*input_file)
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
cols, err := f.GetCols("Sheet1")
if err != nil {
fmt.Println(err)
return
}
for _, col := range cols[0] {
res := generateKey(col)
fmt.Println(res)
}
rows, err := f.GetRows("Sheet1")
if err != nil {
fmt.Println(err)
return
}
len_of_col := len(cols)
var dart_keys []string
// forech first row , create map use first row as key
for row_index, row := range rows[0] {
file_data := make(map[string]string, len_of_col-1)
col := cols[row_index]
for i := 1; i < len(col); i++ {
originalText := cols[0][i]
// 1. 先去空格和特殊字符
cleanedText := cleanText(originalText)
// 2. 再翻译(转换为拼音)
key := generateKey(cleanedText)
// 如果key为空,使用清理后的文本作为key
if key == "" {
key = cleanedText
}
// 如果key以数字开头,在前面添加 auto_gen_ 前缀
if key != "" && len(key) > 0 && key[0] >= '0' && key[0] <= '9' {
key = "auto_gen_" + key
}
// Collect keys for dart file
if row_index == 0 {
// 3. 跳过空字符的key
if key == "" {
fmt.Printf("跳过空key: %v\n", originalText)
fmt.Print("hello")
continue
}
// 去重
if slices.Contains(dart_keys, key) {
// fmt.Println("key already exists:", key)
dart_keys = append(dart_keys, key+"_dup")
continue
}
dart_keys = append(dart_keys, key)
}
// fmt.Printf("key: %v\n", key)
file_data[key] = col[i]
}
WriteJson(file_data, fmt.Sprintf("%s/%s.json", *output_path, string(row)))
}
// Generate dart file
var dart_content strings.Builder
for _, key := range dart_keys {
dart_content.WriteString(fmt.Sprintf(" static const %s = '%s';\n", key, key))
}
final_dart := fmt.Sprintf(dart_file_template, strings.TrimRight(dart_content.String(), "\n"))
dart_file_full_path := filepath.Join(*dart_file_path, "locale_keys.dart")
dart_file, err := os.Create(dart_file_full_path)
if err != nil {
fmt.Println("create dart file error:", err)
return
}
defer dart_file.Close()
dart_file.WriteString(final_dart)
}
func WriteJson(data map[string]string, name string) {
fmt.Println("write json file:", name)
file, err := os.Create(name)
if err != nil {
fmt.Println("create file error:", err)
return
}
defer file.Close()
jsonData, err := json.MarshalIndent(data, "", " ")
if err != nil {
fmt.Println("marshal error:", err)
return
}
file.WriteString(string(jsonData))
}