Skip to content

Commit 37ae6b5

Browse files
committed
增加arb的支持,如果是arb翻译文件,会显示description,方便用户添加注释
1 parent 41b194e commit 37ae6b5

File tree

5 files changed

+93
-16
lines changed

5 files changed

+93
-16
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ jobs:
4949
tag_name: v${{ env.version_name }}
5050
release_name: v${{ env.version_name }}
5151
body: |
52-
update to support the latest Android Studio version 2024.3.1
53-
更新支持最新Android Studio版本2024.3.1
52+
增加arb的支持,如果是arb翻译文件,会显示description,方便用户添加注释
53+
Add support for arb. If it is an arb translation file, a description will be displayed to facilitate users to add comments.
5454
5555
- name: Upload Release Asset
5656
id: upload-release-asset

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group 'org.example'
7-
version '1.9.3-SNAPSHOT'
7+
version '1.9.4-SNAPSHOT'
88

99

1010
repositories {

src/main/kotlin/com/awesome/plugins/language/LanguageResDialog.form

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
44
<margin top="10" left="10" bottom="10" right="10"/>
55
<constraints>
6-
<xy x="48" y="54" width="648" height="638"/>
6+
<xy x="48" y="54" width="648" height="713"/>
77
</constraints>
88
<properties/>
99
<border type="none"/>
@@ -68,7 +68,7 @@
6868
<properties/>
6969
<border type="none"/>
7070
<children>
71-
<grid id="7dbf0" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
71+
<grid id="7dbf0" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
7272
<margin top="0" left="0" bottom="0" right="0"/>
7373
<constraints>
7474
<grid row="1" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
@@ -78,20 +78,38 @@
7878
<children>
7979
<component id="2f7be" class="javax.swing.JLabel">
8080
<constraints>
81-
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
81+
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
8282
</constraints>
8383
<properties>
8484
<text value="Key:"/>
8585
</properties>
8686
</component>
8787
<component id="a8c76" class="javax.swing.JTextField" binding="tvKey">
8888
<constraints>
89-
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
89+
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
9090
<preferred-size width="150" height="-1"/>
9191
</grid>
9292
</constraints>
9393
<properties/>
9494
</component>
95+
<component id="5f819" class="javax.swing.JTextField" binding="tvDescription">
96+
<constraints>
97+
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
98+
<preferred-size width="150" height="-1"/>
99+
</grid>
100+
</constraints>
101+
<properties>
102+
<text value=""/>
103+
</properties>
104+
</component>
105+
<component id="85d35" class="javax.swing.JLabel" binding="textDescription">
106+
<constraints>
107+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
108+
</constraints>
109+
<properties>
110+
<text value="Description"/>
111+
</properties>
112+
</component>
95113
</children>
96114
</grid>
97115
<grid id="c59dc" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">

src/main/kotlin/com/awesome/plugins/language/LanguageResDialog.kt

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ class LanguageResDialog(
4747
//带翻译的内容
4848
var tvChinese: JTextField? = null
4949

50+
//如果是arb(Applicatio Resource Bundle)需要支持设置描述词
51+
var tvDescription: JTextField? = null
52+
53+
var textDescription: JLabel? = null
54+
5055
//选择的语言内容
5156
var tvLanguages: JTextField? = null
5257

@@ -62,6 +67,8 @@ class LanguageResDialog(
6267
//语言码跟翻译后的文本
6368
val mapValues = HashMap<String, String?>()
6469

70+
val descriptionValues = HashMap<String, String?>()
71+
6572
//通过读取properties读取初始化语种和需要翻译的语种
6673
private var properties: PropertiesHelper? = null
6774

@@ -81,6 +88,7 @@ class LanguageResDialog(
8188
private fun onTranslate() {
8289
builder.clear()
8390
mapValues.clear()
91+
descriptionValues.clear()
8492
psiElement.runWriteCmd(::runTranslate)
8593
}
8694

@@ -92,12 +100,28 @@ class LanguageResDialog(
92100
} else {
93101
for (languageCode in languages!!) {
94102
val value = HttpApi.translate(tvChinese!!.text, languageCode)
103+
var description = tvDescription?.text
104+
if (!TextUtils.isEmpty(description)) {
105+
description = HttpApi.translate(description!!, languageCode)?.trim()
106+
}
107+
95108
if (languageCode == "en") {
96109
mapValues.put(languageCode, value?.firstUpperCamel())
110+
if (!TextUtils.isEmpty(description)) {
111+
descriptionValues.put(languageCode, description?.firstUpperCamel())
112+
}
97113
} else {
98114
mapValues.put(languageCode, value)
115+
if (!TextUtils.isEmpty(description)) {
116+
descriptionValues.put(languageCode, description)
117+
}
99118
}
100-
builder.append(languageCode).append("\n").append(mapValues[languageCode]).append("\n\n")
119+
builder.append(languageCode)
120+
.append("\n")
121+
.append(mapValues[languageCode])
122+
.append("\n")
123+
.append(description)
124+
.append("\n")
101125
if (languageCode == "en") {
102126
tvKey?.text = value?.replace(" ", "_")?.trim().clearSymbol().toCamel()
103127
}
@@ -123,7 +147,7 @@ class LanguageResDialog(
123147
}
124148
}
125149

126-
if (isContainJson()) {
150+
if (isJsonOrArb()) {
127151
handleWrite2JsonFile()
128152
} else {
129153
LanguageDartWriter(
@@ -148,13 +172,19 @@ class LanguageResDialog(
148172
for (psiElement in psiElement.children) {
149173
if (psiElement is PsiFile) {
150174
val file = psiElement.virtualFile
175+
151176
try {
152-
if (file.name.endsWith(".json")) {
153-
val name = file.nameWithoutExtension
177+
if (file.name.endsWith(".json") || file.name.endsWith(".arb")) {
178+
val name = file.nameWithoutExtension.replace("app_", "")
154179
val jsonFile = File(file.path)
155180
val json = jsonFile.readText().toJSON() as JSONObject
156181
val value = mapValues[name]
157182
json.put(tvKey!!.text, value)
183+
184+
val description = descriptionValues[name]
185+
if (!TextUtils.isEmpty(description)) {
186+
json.put("@${tvKey!!.text}", JSONObject(mapOf(Pair("description", description))))
187+
}
158188
jsonFile.writeText(json.toJSONString())
159189
}
160190
} catch (e: Exception) {
@@ -168,10 +198,20 @@ class LanguageResDialog(
168198
/**
169199
* 是否包含json文件
170200
**/
171-
private fun isContainJson(): Boolean {
201+
private fun isJsonOrArb(): Boolean {
172202
for (psiElement in psiElement.children) {
173203
if (psiElement is PsiFile) {
174-
return psiElement.virtualFile.name.endsWith(".json")
204+
val name = psiElement.virtualFile.name
205+
return name.endsWith(".json") || name.endsWith(".arb")
206+
}
207+
}
208+
return false
209+
}
210+
211+
private fun isArb(): Boolean {
212+
for (psiElement in psiElement.children) {
213+
if (psiElement is PsiFile) {
214+
return psiElement.virtualFile.name.endsWith(".arb")
175215
}
176216
}
177217
return false
@@ -234,12 +274,17 @@ class LanguageResDialog(
234274
val languageString = properties?.getProperty(PluginProps.languages) ?: defaultLanguage
235275
rawLanguage = properties?.getProperty(PluginProps.rawLanguage) ?: rawLanguage
236276
needTranslate = "true" == properties?.getProperty(PluginProps.needTranslate)
237-
if (isContainJson()) {
277+
278+
val isArb = isArb();
279+
tvDescription!!.isVisible = isArb
280+
textDescription!!.isVisible = isArb
281+
282+
if (isJsonOrArb()) {
238283
languages = arrayListOf()
239284
val arrays = languages as ArrayList<String>
240285
for (psi in psiElement.children) {
241286
if (psi is PsiFile) {
242-
arrays.add(psi.virtualFile.nameWithoutExtension)
287+
arrays.add(psi.virtualFile.nameWithoutExtension.replace("app_", ""))
243288
}
244289
}
245290
tvLanguages?.text = arrays.joinToString(separator = ",")

src/main/kotlin/com/awesome/utils/JsonHelper.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,22 @@ fun String?.formatJson(): String? {
7676
return JSON.toJSONString(json, true)
7777
}
7878

79+
7980
fun String.toJSON(): JSON? {
80-
return (if (startsWith("{")) JSONObject.parseObject(this) else JSONArray.parse(this)) as JSON?
81+
return when {
82+
startsWith("{") -> {
83+
// 先用 LinkedHashMap 保顺序,再手动转成 JSONObject
84+
val map = JSONObject.parseObject(this, LinkedHashMap::class.java) as LinkedHashMap<String, *>
85+
JSONObject(map)
86+
}
87+
88+
startsWith("[") -> {
89+
// JSONArray 本身是 List,有序的,直接 parse 就行
90+
JSONArray.parseArray(this)
91+
}
92+
93+
else -> null
94+
}
8195
}
8296

8397
fun JSONArray.mergeKeys(): Any {

0 commit comments

Comments
 (0)