Skip to content

Commit e605263

Browse files
committed
Add support for xliff:g tags
1 parent af491bb commit e605263

File tree

5 files changed

+173
-47
lines changed

5 files changed

+173
-47
lines changed

src/logic/ParseStringXml.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818

1919
import com.intellij.openapi.progress.ProgressIndicator;
2020
import com.intellij.psi.PsiFile;
21-
import com.intellij.psi.xml.XmlDocument;
22-
import com.intellij.psi.xml.XmlFile;
23-
import com.intellij.psi.xml.XmlTag;
21+
import com.intellij.psi.xml.*;
2422
import module.AndroidString;
23+
import module.Content;
2524
import org.jetbrains.annotations.NotNull;
2625
import org.jetbrains.annotations.Nullable;
2726

@@ -62,10 +61,28 @@ public static List<AndroidString> parse(@Nullable ProgressIndicator progressIndi
6261
XmlTag[] stringTags = rootTag.findSubTags("string");
6362
for (XmlTag stringTag : stringTags) {
6463
String name = stringTag.getAttributeValue("name");
65-
String value = stringTag.getValue().getText();
6664
String translatableStr = stringTag.getAttributeValue("translatable");
6765
boolean translatable = Boolean.valueOf(translatableStr == null ? "true" : translatableStr);
68-
androidStrings.add(new AndroidString(name, value, translatable));
66+
67+
List<Content> contents = new ArrayList<>();
68+
XmlTagChild[] tags = stringTag.getValue().getChildren();
69+
for (XmlTagChild child : tags) {
70+
if (child instanceof XmlText) {
71+
XmlText xmlText = (XmlText) child;
72+
contents.add(new Content(xmlText.getValue()));
73+
} else if (child instanceof XmlTag) {
74+
XmlTag xmlTag = (XmlTag) child;
75+
if (!xmlTag.getName().equals("xliff:g")) continue;
76+
77+
String text = xmlTag.getValue().getText();
78+
String id = xmlTag.getAttributeValue("id");
79+
String example = xmlTag.getAttributeValue("example");
80+
contents.add(new Content(text, id, example, true));
81+
}
82+
}
83+
84+
androidStrings.add(new AndroidString(name, contents, translatable));
85+
6986
if (progressIndicator != null) {
7087
progressIndicator.setText("Loading " + name + " text from strings.xml...");
7188
}

src/module/AndroidString.java

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,21 @@
1616

1717
package module;
1818

19+
import java.util.ArrayList;
20+
import java.util.List;
21+
1922
/**
2023
* @author airsaid
2124
*/
22-
public class AndroidString {
25+
public class AndroidString implements Cloneable {
2326

24-
private String name;
25-
private String value;
26-
private boolean translatable;
27+
private String name;
28+
private List<Content> contents;
29+
private boolean translatable;
2730

28-
public AndroidString(String name, String value, boolean translatable) {
31+
public AndroidString(String name, List<Content> contents, boolean translatable) {
2932
this.name = name;
30-
this.value = value;
33+
this.contents = contents;
3134
this.translatable = translatable;
3235
}
3336

@@ -39,12 +42,12 @@ public void setName(String name) {
3942
this.name = name;
4043
}
4144

42-
public String getValue() {
43-
return value;
45+
public List<Content> getContents() {
46+
return contents;
4447
}
4548

46-
public void setValue(String value) {
47-
this.value = value;
49+
public void setContents(List<Content> contents) {
50+
this.contents = contents;
4851
}
4952

5053
public boolean isTranslatable() {
@@ -67,11 +70,28 @@ public boolean equals(Object obj) {
6770
return name.equals(androidString.name);
6871
}
6972

73+
@Override
74+
public AndroidString clone() {
75+
try {
76+
AndroidString clone = (AndroidString) super.clone();
77+
List<Content> contents = clone.getContents();
78+
List<Content> cloneContents = new ArrayList<>(contents.size());
79+
for (Content content : clone.getContents()) {
80+
cloneContents.add(content.clone());
81+
}
82+
clone.setContents(cloneContents);
83+
return clone;
84+
} catch (CloneNotSupportedException e) {
85+
e.printStackTrace();
86+
return null;
87+
}
88+
}
89+
7090
@Override
7191
public String toString() {
7292
return "AndroidString{" +
7393
"name='" + name + '\'' +
74-
", value='" + value + '\'' +
94+
", contents=" + contents +
7595
", translatable=" + translatable +
7696
'}';
7797
}

src/module/Content.java

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2018 Airsaid. https://github.com/airsaid
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 module;
18+
19+
/**
20+
* @author airsaid
21+
*/
22+
public class Content implements Cloneable {
23+
private String text;
24+
private String id;
25+
private String example;
26+
private boolean isIgnore;
27+
28+
public Content(String text) {
29+
this.text = text;
30+
}
31+
32+
public Content(String text, String id, String example, boolean isIgnore) {
33+
this.text = text;
34+
this.id = id;
35+
this.example = example;
36+
this.isIgnore = isIgnore;
37+
}
38+
39+
public String getText() {
40+
return text;
41+
}
42+
43+
public void setText(String text) {
44+
this.text = text;
45+
}
46+
47+
public String getId() {
48+
return id;
49+
}
50+
51+
public void setId(String id) {
52+
this.id = id;
53+
}
54+
55+
public String getExample() {
56+
return example;
57+
}
58+
59+
public void setExample(String example) {
60+
this.example = example;
61+
}
62+
63+
public boolean isIgnore() {
64+
return isIgnore;
65+
}
66+
67+
public void setIgnore(boolean ignore) {
68+
isIgnore = ignore;
69+
}
70+
71+
@Override
72+
public Content clone() {
73+
try {
74+
return (Content) super.clone();
75+
} catch (CloneNotSupportedException e) {
76+
e.printStackTrace();
77+
return null;
78+
}
79+
}
80+
81+
@Override
82+
public String toString() {
83+
return "Content{" +
84+
"text='" + text + '\'' +
85+
", id='" + id + '\'' +
86+
", example='" + example + '\'' +
87+
", isIgnore=" + isIgnore +
88+
'}';
89+
}
90+
}

src/task/TranslateTask.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import constant.Constants;
3030
import logic.ParseStringXml;
3131
import module.AndroidString;
32+
import module.Content;
3233
import org.jetbrains.annotations.Nls;
3334
import org.jetbrains.annotations.NotNull;
3435
import org.jetbrains.annotations.Nullable;
@@ -46,11 +47,11 @@
4647
*/
4748
public class TranslateTask extends Task.Backgroundable {
4849

49-
private List<LANG> mLanguages;
50-
private List<AndroidString> mAndroidStrings;
51-
private VirtualFile mSelectFile;
50+
private List<LANG> mLanguages;
51+
private List<AndroidString> mAndroidStrings;
52+
private VirtualFile mSelectFile;
5253
private Map<String, List<AndroidString>> mWriteData;
53-
private OnTranslateListener mOnTranslateListener;
54+
private OnTranslateListener mOnTranslateListener;
5455

5556
public interface OnTranslateListener {
5657
void onTranslateSuccess();
@@ -114,15 +115,23 @@ private void translate(Querier<AbstractTranslator> translator, LANG toLanguage,
114115
continue;
115116
}
116117

118+
// If the string to be translated already exists, use it directly
117119
if (list != null && list.contains(androidString)) {
118-
writeAndroidString.add(new AndroidString(
119-
androidString.getName(), list.get(list.indexOf(androidString)).getValue(), false));
120+
writeAndroidString.add(list.get(list.indexOf(androidString)));
120121
continue;
121122
}
122123

123-
translator.setParams(LANG.Auto, toLanguage, androidString.getValue());
124-
String resultValue = translator.executeSingle();
125-
writeAndroidString.add(new AndroidString(androidString.getName(), resultValue, false));
124+
AndroidString clone = androidString.clone();
125+
List<Content> contexts = clone.getContents();
126+
for (Content content : contexts) {
127+
if (content.isIgnore()) continue; // Ignore text with xliff:g tags set
128+
129+
translator.setParams(LANG.Auto, toLanguage, content.getText());
130+
String result = translator.executeSingle();
131+
content.setText(result);
132+
}
133+
134+
writeAndroidString.add(clone);
126135
}
127136
mWriteData.put(toLanguage.getCode(), writeAndroidString);
128137
}
@@ -203,7 +212,11 @@ private void write(File file, List<AndroidString> androidStrings) {
203212
bw.write("<resources>");
204213
bw.newLine();
205214
for (AndroidString androidString : androidStrings) {
206-
bw.write("\t<string name=\"" + androidString.getName() + "\">" + androidString.getValue() + "</string>");
215+
bw.write("\t<string name=\"" + androidString.getName() + "\">");
216+
for (Content content : androidString.getContents()) {
217+
bw.write(content.getText());
218+
}
219+
bw.write("</string>");
207220
bw.newLine();
208221
}
209222
bw.write("</resources>");

src/translate/trans/impl/GoogleTranslator.java

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public final class GoogleTranslator extends AbstractTranslator {
2121

2222
private static final String url = "https://translate.google.cn/translate_a/single";
2323

24-
public GoogleTranslator(){
24+
public GoogleTranslator() {
2525
super(url);
2626
}
2727

@@ -139,22 +139,9 @@ public void setFormData(LANG from, LANG to, String text) {
139139
formData.put("sl", from.getCode());
140140
formData.put("tl", to.getCode());
141141
formData.put("hl", "zh-CN");
142-
formData.put("dt", "at");
143-
formData.put("dt", "bd");
144-
formData.put("dt", "ex");
145-
formData.put("dt", "ld");
146-
formData.put("dt", "md");
147-
formData.put("dt", "qca");
148-
formData.put("dt", "rw");
149-
formData.put("dt", "rm");
150-
formData.put("dt", "ss");
151142
formData.put("dt", "t");
152143
formData.put("ie", "UTF-8");
153144
formData.put("oe", "UTF-8");
154-
formData.put("source", "btn");
155-
formData.put("ssel", "0");
156-
formData.put("tsel", "0");
157-
formData.put("kc", "0");
158145
formData.put("tk", token(text));
159146
formData.put("q", text);
160147
}
@@ -168,16 +155,15 @@ public String query() throws Exception {
168155
}
169156
HttpGet request = new HttpGet(uri.toString());
170157
RequestConfig config = RequestConfig.copy(RequestConfig.DEFAULT)
171-
.setSocketTimeout(5000)
172-
.setConnectTimeout(5000)
173-
.setConnectionRequestTimeout(5000)
174-
.build();
158+
.setSocketTimeout(5000)
159+
.setConnectTimeout(5000)
160+
.setConnectionRequestTimeout(5000)
161+
.build();
175162
request.setConfig(config);
176163
CloseableHttpResponse response = httpClient.execute(request);
177164
HttpEntity entity = response.getEntity();
178165

179-
String result = EntityUtils.toString(entity, "utf-8");
180-
166+
String result = EntityUtils.toString(entity, "UTF-8");
181167
EntityUtils.consume(entity);
182168
response.getEntity().getContent().close();
183169
response.close();
@@ -198,7 +184,7 @@ private String token(String text) {
198184
engine.eval(new InputStreamReader(inputStream));
199185

200186
if (engine instanceof Invocable) {
201-
Invocable invoke = (Invocable)engine;
187+
Invocable invoke = (Invocable) engine;
202188
tk = String.valueOf(invoke.invokeFunction("token", text));
203189
}
204190
} catch (Exception e) {

0 commit comments

Comments
 (0)