Skip to content

Commit

Permalink
Remove Plural Entries Missing the 'Other' Form
Browse files Browse the repository at this point in the history
Not having the 'other' form in plural resources can cause the app to crash.

This commit ensures that any plural entries lacking the 'other' form are removed to prevent crashes.
  • Loading branch information
ja-openai committed Nov 21, 2024
1 parent 04689c5 commit 7f8870e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -502,9 +502,17 @@ public String execute(String xmlContent) {
Element plurals = (Element) pluralsElements.item(i);
NodeList items = plurals.getElementsByTagName("item");
boolean hasTranslated = false;
boolean hasOther = false;

for (int j = 0; j < items.getLength(); j++) {
Element item = (Element) items.item(j);

if ("other".equals(item.getAttribute("quantity"))) {
hasOther =
!RemoveUntranslatedStrategy.UNTRANSLATED_PLACEHOLDER.equals(
item.getTextContent());
}

if (hasRemoveUntranslated()
&& item.getTextContent()
.equals(RemoveUntranslatedStrategy.UNTRANSLATED_PLACEHOLDER)) {
Expand All @@ -515,7 +523,7 @@ public String execute(String xmlContent) {
}
}

if (!hasTranslated) {
if (!hasOther || !hasTranslated) {
plurals.getParentNode().removeChild(plurals);
i--;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ public void testPostProcessingKeepDescription() {
<item quantity="other">@#$untranslated$#@</item>
</plurals>
<plurals description="testing plural attr" name="pins2">
<item quantity="one">translated</item>
<item quantity="other">@#$untranslated$#@</item>
<item quantity="one">@#$untranslated$#@</item>
<item quantity="other">translated</item>
</plurals>
<plurals name="pins3">
<item quantity="one">pin fr</item>
Expand All @@ -143,7 +143,7 @@ public void testPostProcessingKeepDescription() {
<string description="a description" name="pinterest2">somestring to keep</string>
<!--testing plural-->
<plurals description="testing plural attr" name="pins2">
<item quantity="one">translated</item>
<item quantity="other">translated</item>
</plurals>
<plurals name="pins3">
<item quantity="one">pin fr</item>
Expand All @@ -170,8 +170,8 @@ public void testPostProcessingRemoveDescription() {
<item quantity="other">@#$untranslated$#@</item>
</plurals>
<plurals description="testing plural attr" name="pins2">
<item quantity="one">translated</item>
<item quantity="other">@#$untranslated$#@</item>
<item quantity="one">@#$untranslated$#@</item>
<item quantity="other">translated</item>
</plurals>
<plurals name="pins3">
<item quantity="one">pin fr</item>
Expand All @@ -187,7 +187,7 @@ public void testPostProcessingRemoveDescription() {
<string name="pinterest2">somestring to keep</string>
<!--testing plural-->
<plurals name="pins2">
<item quantity="one">translated</item>
<item quantity="other">translated</item>
</plurals>
<plurals name="pins3">
<item quantity="one">pin fr</item>
Expand Down Expand Up @@ -245,8 +245,8 @@ public void testPostProcessingRemoveTranslatableFalse() {
<item quantity="other">@#$untranslated$#@</item>
</plurals>
<plurals description="testing plural attr" name="pins2">
<item quantity="one">translated</item>
<item quantity="other">@#$untranslated$#@</item>
<item quantity="one">@#$untranslated$#@</item>
<item quantity="other">translated</item>
</plurals>
<plurals name="pins3" translatable="false">
<item quantity="one">pin fr</item>
Expand All @@ -261,13 +261,58 @@ public void testPostProcessingRemoveTranslatableFalse() {
<resources>
<!--testing plural-->
<plurals name="pins2">
<item quantity="one">translated</item>
<item quantity="other">translated</item>
</plurals>
</resources>
""";
assertEquals(expected, output);
}

@Test
public void testPostProcessingRemoveMissingOther() {
AndroidFilter.AndroidFilePostProcessor androidFilePostProcessor =
new AndroidFilter.AndroidFilePostProcessor(true, true, 2, true, false);
String input =
"""
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<plurals name="pins">
<item quantity="one">pin fr</item>
</plurals>
</resources>
""";
String output = androidFilePostProcessor.execute(input);
String expected =
"""
<?xml version="1.0" encoding="UTF-8"?>
<resources/>
""";
assertEquals(expected, output);
}

@Test
public void testPostProcessingRemoveMissingOtherUntranslated() {
AndroidFilter.AndroidFilePostProcessor androidFilePostProcessor =
new AndroidFilter.AndroidFilePostProcessor(true, true, 2, true, false);
String input =
"""
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<plurals name="pins">
<item quantity="one">pin fr</item>
<item quantity="other">@#$untranslated$#@</item>
</plurals>
</resources>
""";
String output = androidFilePostProcessor.execute(input);
String expected =
"""
<?xml version="1.0" encoding="UTF-8"?>
<resources/>
""";
assertEquals(expected, output);
}

@Test
public void testPostProcessingStandaloneNo() {
AndroidFilter.AndroidFilePostProcessor androidFilePostProcessor =
Expand All @@ -284,8 +329,8 @@ public void testPostProcessingStandaloneNo() {
<item quantity="other">@#$untranslated$#@</item>
</plurals>
<plurals description="testing plural attr" name="pins2">
<item quantity="one">translated</item>
<item quantity="other">@#$untranslated$#@</item>
<item quantity="one">@#$untranslated$#@</item>
<item quantity="other">translated</item>
</plurals>
<plurals name="pins3" translatable="false">
<item quantity="one">pin fr</item>
Expand All @@ -300,7 +345,7 @@ public void testPostProcessingStandaloneNo() {
<resources>
<!--testing plural-->
<plurals name="pins2">
<item quantity="one">translated</item>
<item quantity="other">translated</item>
</plurals>
</resources>
""";
Expand All @@ -323,8 +368,8 @@ public void testPostProcessingStandaloneYes() {
<item quantity="other">@#$untranslated$#@</item>
</plurals>
<plurals description="testing plural attr" name="pins2">
<item quantity="one">translated</item>
<item quantity="other">@#$untranslated$#@</item>
<item quantity="one">@#$untranslated$#@</item>
<item quantity="other">translated</item>
</plurals>
<plurals name="pins3" translatable="false">
<item quantity="one">pin fr</item>
Expand All @@ -339,7 +384,7 @@ public void testPostProcessingStandaloneYes() {
<resources>
<!--testing plural-->
<plurals name="pins2">
<item quantity="one">translated</item>
<item quantity="other">translated</item>
</plurals>
</resources>
""";
Expand Down

0 comments on commit 7f8870e

Please sign in to comment.