Skip to content

Commit c5533e2

Browse files
committed
Remove Plural Entries Missing the 'Other' Form
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.
1 parent 8127157 commit c5533e2

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

common/src/main/java/com/box/l10n/mojito/okapi/filters/AndroidFilter.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,9 +502,15 @@ public String execute(String xmlContent) {
502502
Element plurals = (Element) pluralsElements.item(i);
503503
NodeList items = plurals.getElementsByTagName("item");
504504
boolean hasTranslated = false;
505+
boolean hasOther = false;
505506

506507
for (int j = 0; j < items.getLength(); j++) {
507508
Element item = (Element) items.item(j);
509+
510+
if ("other".equals(item.getAttribute("quantity"))) {
511+
hasOther = true;
512+
}
513+
508514
if (hasRemoveUntranslated()
509515
&& item.getTextContent()
510516
.equals(RemoveUntranslatedStrategy.UNTRANSLATED_PLACEHOLDER)) {
@@ -515,7 +521,7 @@ public String execute(String xmlContent) {
515521
}
516522
}
517523

518-
if (!hasTranslated) {
524+
if (!hasOther || !hasTranslated) {
519525
plurals.getParentNode().removeChild(plurals);
520526
i--;
521527
}

common/src/test/java/com/box/l10n/mojito/okapi/filters/AndroidFilterTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,28 @@ public void testPostProcessingRemoveTranslatableFalse() {
268268
assertEquals(expected, output);
269269
}
270270

271+
@Test
272+
public void testPostProcessingRemoveMissingOther() {
273+
AndroidFilter.AndroidFilePostProcessor androidFilePostProcessor =
274+
new AndroidFilter.AndroidFilePostProcessor(true, true, 2, true, false);
275+
String input =
276+
"""
277+
<?xml version="1.0" encoding="UTF-8"?>
278+
<resources>
279+
<plurals name="pins">
280+
<item quantity="one">pin fr</item>
281+
</plurals>
282+
</resources>
283+
""";
284+
String output = androidFilePostProcessor.execute(input);
285+
String expected =
286+
"""
287+
<?xml version="1.0" encoding="UTF-8"?>
288+
<resources/>
289+
""";
290+
assertEquals(expected, output);
291+
}
292+
271293
@Test
272294
public void testPostProcessingStandaloneNo() {
273295
AndroidFilter.AndroidFilePostProcessor androidFilePostProcessor =

0 commit comments

Comments
 (0)