1- def searchSourceFiles (javaFiles , en_lang , langMap ) {
1+ import java.util.regex.Matcher
2+
3+ def searchSourceFiles (SourceDirectorySet javaFiles , Map<String , String > en_lang , Map<String , Map<String , String > > langMap ) {
24 // Source File Search Start
35 // 源文件搜索使用的状态变量
4- def state = null
5- def trMatch = null
6+ String state = null
7+ Matcher trMatch = null
68
79 def colorCodeMap = project. ext. colorCodeMap
810
@@ -15,7 +17,7 @@ def searchSourceFiles(javaFiles, en_lang, langMap) {
1517 def matcher = line =~ / \/\/\s *#([a-z]{2}_[A-Z]{2})?\s +(.+)/
1618
1719 if (matcher) {
18- println " Match line: $line "
20+ logger . debug( " Match line: $line " )
1921
2022 def groupCount = matcher. groupCount()
2123 // 如果匹配到一组或两组
@@ -31,9 +33,9 @@ def searchSourceFiles(javaFiles, en_lang, langMap) {
3133 }
3234 def key = trMatch. group(1 ) as String
3335 def value = matcher. group(2 )
34- def lang1 = langMap[lang] as Map
36+ def lang1 = langMap[lang]
3537 if (lang1 == null ) {
36- lang1 = [:]
38+ lang1 = new HashMap< String , String > ()
3739 langMap[lang] = lang1
3840 }
3941
@@ -44,13 +46,27 @@ def searchSourceFiles(javaFiles, en_lang, langMap) {
4446 return replacement ?: match
4547 })
4648
49+ if (lang1. containsKey(key)) { // warn if a key is reassigned
50+ def originalText = lang1[key]
51+ if (originalText == value1) {
52+ logger. warn(" [Key Overwrite|Duplicated] Key '${ key} ' is reassigned with same text in file '${ file} '" )
53+ }
54+ else {
55+ logger. warn(" [Key Overwrite|Overwrited] Key '${ key} ' is reassigned from '${ lang1[key]} ' to '${ value1} ' in file '${ file} '" )
56+ }
57+ }
58+
59+ if (! checkFormatPattern(value1)) {
60+ logger. warn(" [Invalid Formatter] Key '${ key} ' has an invalid formatter string from '${ lang} ' in '${ file.name} ': ${ value1} " )
61+ }
62+
4763 lang1[key] = value1
4864
4965 }
5066 } else {
5167 trMatch = null
5268 state = null
53- println " Waring: The wrong format has been captured: $line "
69+ logger . warn( " Waring: The wrong format has been captured: $line " )
5470 }
5571 break
5672 } else {
@@ -61,7 +77,7 @@ def searchSourceFiles(javaFiles, en_lang, langMap) {
6177 def matcher = line =~ / \/\/\s *#tr\s +([a-zA-Z0-9._]+)\s *(.+)?\s */
6278 // 检查是否匹配了#tr模式
6379 if (matcher) {
64- println " Match line: $line "
80+ logger . debug( " Match line: $line " )
6581
6682 def groupCount = matcher. groupCount()
6783
@@ -85,7 +101,7 @@ def searchSourceFiles(javaFiles, en_lang, langMap) {
85101 en_lang[key] = value1
86102 }
87103 } else {
88- println " Waring: The wrong format has been captured: $line "
104+ logger . warn( " Waring: The wrong format has been captured: $line " )
89105 }
90106 }
91107 break
@@ -126,8 +142,8 @@ def searchLanguageFiles(namespace, sort, langMap) {
126142 if (needSort) sort. add(key)
127143 if (lang1. containsKey(key)) {
128144 if (value != lang1[key]) {
129- println " The key: $key is already defined in the code! The Lang file will be overwritten!"
130- println " Code: ${ lang1[key]} Lang file: $value "
145+ logger . info( " The key: $key is already defined in the code! The Lang file will be overwritten!" )
146+ logger . info( " Code: ${ lang1[key]} Lang file: $value " )
131147 }
132148 } else {
133149 lang1[key] = value
@@ -140,7 +156,7 @@ def searchLanguageFiles(namespace, sort, langMap) {
140156 // Language File Search Finish
141157}
142158
143- def writeLanguageFiles (namespace , sort , langMap , ignoreKeyInFile , onlyFile , en_lang ) {
159+ def writeLanguageFiles (String namespace , Set< String > sort , Map< String , Map< String , String > > langMap , boolean ignoreKeyInFile , Set< String > onlyFile , Map< String , String > en_lang ) {
144160 langMap. each { fileName , innerMap ->
145161 def outputFile = new File (" src/main/resources/assets/$namespace /lang/${ fileName} .lang" )
146162 outputFile. getParentFile(). mkdirs()
@@ -152,12 +168,12 @@ def writeLanguageFiles(namespace, sort, langMap, ignoreKeyInFile, onlyFile, en_l
152168 else {
153169 if (innerMap. containsKey(line)) {
154170 if (ignoreKeyInFile && onlyFile. contains(line)) {
155- println " Ignore the $line , Because it only exists in the language file!"
171+ logger . info( " Ignore the $line , because it only exists in the language file!" )
156172 } else {
157173 writer. write(" ${ line} =${ innerMap[line]} \n " )
158174 }
159175 } else {
160- println " The $fileName file does not contain the $line "
176+ logger . warn( " [Translation Missing] The $fileName file does not contain the $line " )
161177 writer. write(" ${ line} =${ en_lang[line]} \n " )
162178 }
163179 }
@@ -171,99 +187,22 @@ def writeLanguageFiles(namespace, sort, langMap, ignoreKeyInFile, onlyFile, en_l
171187 }
172188}
173189
174- // def searchSourceFiles(javaFiles, en_lang){
175- // def state = null
176- // def trMatch = null
177- //
178- // def colorCodeMap = project.ext.colorCodeMap
179- //
180- // javaFiles.each { file ->
181- // def content = file.text
182- // content.eachLine { line ->
183- // switch (state) {
184- // case 'tr':
185- // // 如果上一行匹配了#tr模式
186- // def matcher = line =~ /\/\/\s*#([a-z]{2}_[A-Z]{2})?\s*(.+)/
187- //
188- // if (matcher) {
189- // println "Match line: $line"
190- //
191- // def groupCount = matcher.groupCount()
192- // // 如果匹配到一组或两组
193- // if (groupCount == 1 || groupCount == 2) {
194- // if (groupCount == 1) {
195- // def key = trMatch.group(1) as String
196- // def value = matcher.group(1)
197- // en_lang[key] = value
198- // } else {
199- // def lang = matcher.group(1)
200- // if (lang == null || lang == '') {
201- // lang = 'en_US'
202- // }
203- // def key = trMatch.group(1) as String
204- // def value = matcher.group(2)
205- // def lang1 = langMap[lang] as Map
206- // if (lang1 == null) {
207- // lang1 = [:]
208- // langMap[lang] = lang1
209- // }
210- //
211- // // 替换颜色代码
212- // def value1 = value.replaceAll("\\{\\\\([A-Z_]+)\\}", { match ->
213- // def colorValue = match[1]
214- // def replacement = colorCodeMap[colorValue]
215- // return replacement ?: match
216- // })
217- //
218- // lang1[key] = value1
219- //
220- // }
221- // } else {
222- // trMatch = null
223- // state = null
224- // println "Waring: The wrong format has been captured: $line"
225- // }
226- // } else {
227- // trMatch = null
228- // state = null
229- // }
230- // break
231- // default:
232- // def matcher = line =~ /\/\/\s*#tr\s+([a-zA-Z0-9._]+)\s*(.+)?\s*/
233- // // 检查是否匹配了#tr模式
234- // if (matcher) {
235- // println "Match line: $line"
236- //
237- // def groupCount = matcher.groupCount()
238- //
239- // // 如果匹配到一组或两组
240- // if (groupCount == 1) {
241- // state = 'tr'
242- // trMatch = matcher
243- // } else if (groupCount == 2) {
244- // def value = matcher.group(2)
245- // if (value == null) {
246- // state = 'tr'
247- // trMatch = matcher
248- // } else {
249- // def key = matcher.group(1)
250- //
251- // def value1 = value.replaceAll("\\{\\\\([A-Z_]+)\\}", { match ->
252- // def colorValue = match[1]
253- // def replacement = colorCodeMap[colorValue]
254- // return replacement ?: match
255- // })
256- // en_lang[key] = value1
257- // }
258- // } else {
259- // println "Waring: The wrong format has been captured: $line"
260- // }
261- // }
262- // break
263- // }
264- // }
265- // }
266- // }
190+ static checkFormatPattern (String str ) {
191+ def parseMethod = Formatter . metaClass. pickMethod(" parse" , String . class)
192+ // println(parseMethod)
193+
194+ try {
195+ if (parseMethod. isStatic()) {
196+ parseMethod. invoke(null , str)
197+ } else {
198+ parseMethod. invoke(new Formatter (), str)
199+ }
200+ return true
201+ } catch (IllegalFormatException ignored) {
202+ return false
203+ }
204+ }
205+
267206ext {
268207 colorCodeMap = [
269208 " BLACK" : " \u 00a70" ,
@@ -305,42 +244,53 @@ tasks.register('preprocessLangInJavaFiles') {
305244
306245 // 搜索源文件
307246 def javaFiles = sourceSets. main. java
308- def langMap = [:]
309- def en_lang = [:]
310- langMap[' en_US' ] = en_lang
311- def keySet = [] as Set
312247
313- searchSourceFiles(javaFiles, en_lang, langMap)
248+ Map<String , Map<String , String > > langMaps = [:]
249+ Map<String , String > en_lang = [:]
250+ langMaps[' en_US' ] = en_lang
251+ Set<String > allLangKeys = [] as Set
252+
253+ searchSourceFiles(javaFiles, en_lang, langMaps)
314254
315255 // 收集所有源文件中的键
316- langMap. each { name , map ->
317- keySet. addAll(map. keySet())
256+ langMaps. each { name , map ->
257+ allLangKeys. addAll(map. keySet())
258+ }
259+ logger. debug(" LangMap: $langMaps " )
260+
261+ // check if the translation values exist in other languages
262+ en_lang. keySet(). each { String key ->
263+ langMaps. each { langName , langMap ->
264+ if (! langMap. containsKey(key)) {
265+ getLogger(). warn(" [Translation Missing] Key '${ key} ' is missing in language '${ langName} ' (en_US = ${ en_lang[key]} )" . toString())
266+ }
267+ }
318268 }
319- println " LangMap: $langMap "
320269
321270 // Language File Search Start
322- def sort = [] // en_US的键序
271+ Set< String > enUsKeys = [] // en_US的键序
323272
324273 // 获取所有资源文件
325274
326- searchLanguageFiles(namespace, sort, langMap )
275+ searchLanguageFiles(namespace, enUsKeys, langMaps )
327276 // Source File Search Finish
328277
329278 // 只存在于语言文件中的键
330- def onlyFile = [] as Set
331- onlyFile. addAll(sort)
332- onlyFile. removeAll(keySet)
333- if (! onlyFile. empty) {
334- println " Some of the keys don't exist in the code comments, so they may have been deprecated!"
335- println onlyFile
279+ Set<String > keysOnlyInLangFiles = []
280+ keysOnlyInLangFiles. addAll(enUsKeys)
281+ keysOnlyInLangFiles. removeAll(allLangKeys)
282+ if (! keysOnlyInLangFiles. empty) {
283+ keysOnlyInLangFiles. each {
284+ logger. warn(" [#tr Missing] Key '${ it} ' only exists in language files, but not code comments. (en_US = ${ langMaps["en_US"][it]} , zh_CN = ${ langMaps["zh_CN"][it]} )" )
285+ }
336286 }
337287
338- keySet . removeAll(sort ) // 只存在代码中的键
288+ allLangKeys . removeAll(enUsKeys ) // 只存在代码中的键
339289
340- sort . addAll(keySet ) // 将只存在代码中的键添加到最后
290+ enUsKeys . addAll(allLangKeys ) // 将只存在代码中的键添加到最后
341291
342- println " File key sequence: $s ort "
343- writeLanguageFiles(namespace, sort, langMap , ignoreKeyInFile, onlyFile , en_lang)
292+ logger . debug( " File key sequence: $e nUsKeys " )
293+ writeLanguageFiles(namespace, enUsKeys, langMaps , ignoreKeyInFile, keysOnlyInLangFiles , en_lang)
344294
345295 }
346296}
0 commit comments