Skip to content

Commit

Permalink
Add post-processing to AndroidFilter to remove non-translatable elements
Browse files Browse the repository at this point in the history
Introduced a new post-processing option, postRemoveTranslatableFalse, in AndroidFilter.

This option removes elements with the attribute translatable="false", ensuring only translatable elements are written to the output.

Note: This does not prevent these elements from being pushed to Mojito, this is a post-processing step.

Due to an existing bug in the filter, singular elements are properly excluded from translation, but plural ones are not (to fix later).

But excluded elements from translation in Mojito are still outputted, while this new option ensures they are removed from the final output.

Typically used with pull -fo postRemoveTranslatableFalse=true.
  • Loading branch information
ja-openai committed Aug 31, 2024
1 parent 4c7e0d1 commit b36eb36
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public class AndroidFilter extends XMLFilter {

private static final String POST_PROCESS_INDENT = "postProcessIndent";

private static final String POST_PROCESS_REMOVE_TRANSLATABLE_FALSE =
"postRemoveTranslatableFalse";

private static final Pattern PATTERN_PLURAL_START = Pattern.compile("<plurals");
private static final Pattern PATTERN_PLURAL_END = Pattern.compile("</plurals>");
private static final Pattern PATTERN_XML_COMMENT = Pattern.compile("<!--(?<comment>.*?)-->");
Expand Down Expand Up @@ -110,6 +113,8 @@ public List<FilterConfiguration> getConfigurations() {

boolean removeDescription = false;

boolean removeTranslatableFalse = false;

int postProcessIndent = 2;

@Override
Expand All @@ -128,7 +133,9 @@ public void open(RawDocument input) {
// in the TranslateStep
input.setAnnotation(
new OutputDocumentPostProcessingAnnotation(
new AndroidFilePostProcessing(removeDescription, postProcessIndent)::execute,
new AndroidFilePostProcessing(
removeDescription, postProcessIndent, removeTranslatableFalse)
::execute,
removeDescription));
}

Expand All @@ -152,6 +159,12 @@ void applyFilterOptions(RawDocument input) {
removeDescription = b;
});

filterOptions.getBoolean(
POST_PROCESS_REMOVE_TRANSLATABLE_FALSE,
b -> {
removeTranslatableFalse = b;
});

filterOptions.getInteger(
POST_PROCESS_INDENT,
i -> {
Expand Down Expand Up @@ -424,10 +437,13 @@ void updateFormInSkeleton(ITextUnit textUnit) {
static class AndroidFilePostProcessing {
static final String DESCRIPTION_ATTRIBUTE = "description";
boolean removeDescription;
boolean removeTranslatableFalse;
int indent;

AndroidFilePostProcessing(boolean removeDescription, int indent) {
AndroidFilePostProcessing(
boolean removeDescription, int indent, boolean removeTranslatableFalse) {
this.removeDescription = removeDescription;
this.removeTranslatableFalse = removeTranslatableFalse;
this.indent = indent;
}

Expand Down Expand Up @@ -491,6 +507,9 @@ String execute(String xmlContent) {
}
}

if (removeTranslatableFalse) {
removeTranslatableFalseElements(document);
}
removeWhitespaceNodes(document);

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Expand Down Expand Up @@ -521,7 +540,7 @@ String execute(String xmlContent) {
}
}

public void removeWhitespaceNodes(Node node) {
void removeWhitespaceNodes(Node node) {
NodeList childNodes = node.getChildNodes();
for (int i = childNodes.getLength() - 1; i >= 0; i--) {
Node childNode = childNodes.item(i);
Expand All @@ -532,5 +551,21 @@ public void removeWhitespaceNodes(Node node) {
}
}
}

void removeTranslatableFalseElements(Node node) {
NodeList childNodes = node.getChildNodes();
for (int i = childNodes.getLength() - 1; i >= 0; i--) {
Node childNode = childNodes.item(i);
if (childNode.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) childNode;
if (element.hasAttribute("translatable")
&& element.getAttribute("translatable").equals("false")) {
node.removeChild(element);
} else {
removeTranslatableFalseElements(element);
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ void testUnescaping(String input, String expected) {
@Test
public void testPostProcessingKeepDescription() {
AndroidFilter.AndroidFilePostProcessing androidFilePostProcessing =
new AndroidFilter.AndroidFilePostProcessing(false, 2);
new AndroidFilter.AndroidFilePostProcessing(false, 2, false);
String input =
"""
<?xml version="1.0" encoding="UTF-8"?>
Expand Down Expand Up @@ -157,7 +157,7 @@ public void testPostProcessingKeepDescription() {
@Test
public void testPostProcessingRemoveDescription() {
AndroidFilter.AndroidFilePostProcessing androidFilePostProcessing =
new AndroidFilter.AndroidFilePostProcessing(true, 2);
new AndroidFilter.AndroidFilePostProcessing(true, 2, false);
String input =
"""
<?xml version="1.0" encoding="UTF-8"?>
Expand Down Expand Up @@ -201,7 +201,7 @@ public void testPostProcessingRemoveDescription() {
@Test
public void testPostProcessingEmptyFile() {
AndroidFilter.AndroidFilePostProcessing androidFilePostProcessing =
new AndroidFilter.AndroidFilePostProcessing(true, 2);
new AndroidFilter.AndroidFilePostProcessing(true, 2, false);
String input = "";
String output = androidFilePostProcessing.execute(input);
String expected = "";
Expand All @@ -211,7 +211,7 @@ public void testPostProcessingEmptyFile() {
@Test
public void testPostProcessingNoProlog() {
AndroidFilter.AndroidFilePostProcessing androidFilePostProcessing =
new AndroidFilter.AndroidFilePostProcessing(true, 2);
new AndroidFilter.AndroidFilePostProcessing(true, 2, false);
String input =
"""
<resources>
Expand All @@ -228,4 +228,43 @@ public void testPostProcessingNoProlog() {
""";
assertEquals(expected, output);
}

@Test
public void testPostProcessingRemoveTranslatableFalse() {
AndroidFilter.AndroidFilePostProcessing androidFilePostProcessing =
new AndroidFilter.AndroidFilePostProcessing(true, 2, true);
String input =
"""
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<string name="pinterest2" description="a description" translatable="false">somestring to keep</string>
<string name="pinterest">@#$untranslated$#@</string>
<!--testing plural-->
<plurals name="pins">
<item quantity="one">@#$untranslated$#@</item>
<item quantity="other">@#$untranslated$#@</item>
</plurals>
<plurals description="testing plural attr" name="pins2">
<item quantity="one">translated</item>
<item quantity="other">@#$untranslated$#@</item>
</plurals>
<plurals name="pins3" translatable="false">
<item quantity="one">pin fr</item>
<item quantity="other">pins fr</item>
</plurals>
</resources>
""";
String output = androidFilePostProcessing.execute(input);
String expected =
"""
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<resources>
<!--testing plural-->
<plurals name="pins2">
<item quantity="one">translated</item>
</plurals>
</resources>
""";
assertEquals(expected, output);
}
}

0 comments on commit b36eb36

Please sign in to comment.