Skip to content

Commit

Permalink
Handle more mess created by Google translation (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
ashishb committed Mar 28, 2023
1 parent 1d56de6 commit bceca68
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/translations.py
Expand Up @@ -46,11 +46,11 @@ def _get_target_languages(res_dir: str) -> typing.Dict[str, str]:

def _normalize_response(text: str) -> str:
# Fix responses like \ "%1 $ S \" -> \"%1$s\"
pattern = r'%\s*([\d*])\s*\$\s*([sdfSDF])'
text = re.sub(pattern, r'%\1$\2', text)
# Fix responses like %4 $ .1f -> %4$.1f
pattern = r'%\s*([\d*])\s*\$\s*(\d*\.\d+)([fF])'
pattern = r'%\s*([\d*])\s*\$(,?)\s*([sdfSDF])'
text = re.sub(pattern, r'%\1$\2\3', text)
# Fix responses like %4 $ .1f -> %4$.1f
pattern = r'%\s*([\d*])\s*\$(,?)\s*(\d*\.\d+)([fF])'
text = re.sub(pattern, r'%\1$\2\3\4', text)

text = text.replace('" ', '"')
# text = text.replace(" \"", "\"")
Expand All @@ -62,6 +62,8 @@ def _normalize_response(text: str) -> str:
text = text.replace("$D", "$d")
text = text.replace("$S", "$s")
text = text.replace("d/ %", "d/%")
text = text.replace("$,S", "$,s")
text = text.replace("$,D", "$,d")
text = text.replace("f/ %", "f/%")
# Replace Chinese % sign with standard English one or "%d" and "%s" won't work
text = text.replace("%", "%")
Expand Down
4 changes: 4 additions & 0 deletions tests/test_translations.py
Expand Up @@ -26,6 +26,10 @@ def test_normalize_responses():
assert translations._normalize_response("% 1$ D") == "%1$d"
assert translations._normalize_response("% 1 $D") == "%1$d"

assert translations._normalize_response("%1 $, D") == "%1$,d"
assert translations._normalize_response("%1 $, d") == "%1$,d"
assert translations._normalize_response("%2 $, s") == "%2$,s"

assert translations._normalize_response("%4 $ .1f") == "%4$.1f"
# E.g. 200/300
assert translations._normalize_response("d/ %") == "d/%"
Expand Down

0 comments on commit bceca68

Please sign in to comment.