Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle more mess created by Google translation #17

Merged
merged 1 commit into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/translations.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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