Skip to content

Commit

Permalink
Merge pull request #78 from transifex/TX-8671-json-plurals-custom-value
Browse files Browse the repository at this point in the history
TX-8671 Parse strings with custom plural values (e.g. =1) as non-pluralized
  • Loading branch information
dbendilas committed Sep 26, 2017
2 parents 5411909 + c7b2b54 commit 2dcac28
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
15 changes: 15 additions & 0 deletions openformats/formats/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,21 @@ def _parse_pluralized_string(self, key, keyword, value, value_position,
e.g. 'one { I ate {count} apple. } other { I ate {count} apples. }'
:return: A pluralized OpenString instance or None
"""
# The official plurals format supports defining an integer instead
# of the name of the plural rule, using a syntax like "=1" or "=2"
# We do not support this at the moment, but we want to have these
# strings be handled as non pluralized.
equality_item = (
pyparsing.Literal('=') + pyparsing.Word(pyparsing.alphanums) +
pyparsing.nestedExpr('{', '}')
)
equality_matches = pyparsing.originalTextFor(equality_item)\
.searchString(serialized_strings)

# If any match is found using this syntax, do not parse this
# as pluralized
if len(equality_matches) > 0:
return None

# Each item should be like '<proper_plurality_rule_str> {<content>}'
# Nested braces ({}) inside <content> are allowed.
Expand Down
3 changes: 2 additions & 1 deletion openformats/tests/formats/keyvaluejson/files/1_el.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"going_on": "el:Something is going on.",
"wrong": "{ sth, plural, one {el:Something is wrong.} other {el:Somethings are wrong.} }"
}
}
},
"custom_plural_value": "el:{number, plural, =1 {1 New}, =2 {# New}}"
}
3 changes: 2 additions & 1 deletion openformats/tests/formats/keyvaluejson/files/1_en.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"going_on": "Something is going on.",
"wrong": "{ sth, plural, one {Something is wrong.} other {Somethings are wrong.} }"
}
}
},
"custom_plural_value": "{number, plural, =1 {1 New}, =2 {# New}}"
}
3 changes: 2 additions & 1 deletion openformats/tests/formats/keyvaluejson/files/1_tpl.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"going_on": "38eb89d2562cc44ff2061a045b05ebca_tr",
"wrong": "{ sth, plural, a125aead7129e03261500e0cade68557_pl }"
}
}
},
"custom_plural_value": "ee829fd77061d65f1f18982a577b915b_tr"
}

0 comments on commit 2dcac28

Please sign in to comment.