Skip to content

Commit

Permalink
When a po entry has plurals, the translation does not work
Browse files Browse the repository at this point in the history
Fixes #13
  • Loading branch information
leolivier committed Sep 21, 2024
1 parent 0186c92 commit 1cb3fea
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
34 changes: 30 additions & 4 deletions src/auto_po_lyglot/clients/client_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,12 @@ def translate(self, phrase, context_translation):
def translate_pofile(self, input_file, output_file):
logger.info(f"Translating {input_file} to {self.target_language} in {output_file}")
input_path = Path(input_file)
app_name = input_path.parents[4].name.capitalize()
wr_input_file = '/'.join(input_path.parts[-6:]) # don't keep the beginning of the file name to put in the header
if str(input_path.parents[1]) == 'LC_MESSAGES':
app_name = input_path.parents[4].name.capitalize()
wr_input_file = '/'.join(input_path.parts[-6:]) # don't keep the beginning of the file name to put in the header
else:
app_name = "NO NAME FOUND"
wr_input_file = input_file
po = polib.pofile(input_file)
out_po = polib.pofile(output_file) if Path(output_file).exists() else None
po.header = f"""{self.target_language} Translations for {app_name} app.
Expand Down Expand Up @@ -243,21 +247,43 @@ def translate_pofile(self, input_file, output_file):
already_translated += 1
continue

context_translation = entry.msgstr if entry.msgstr else entry.msgid
original_phrase = entry.msgid
if entry.msgid_plural: # entry with plural management. First manage the singular case
context_translation = entry.msgstr_plural[0] if entry.msgstr_plural else entry.msgid_plural
else:
context_translation = entry.msgstr if entry.msgstr else entry.msgid
translation, explanation = self.translate(original_phrase, context_translation)
# Add explanation to comment
if explanation:
entry.comment = explanation
# Update translation
entry.msgstr = translation
if entry.msgid_plural: # entry with plural management. Update the singular case
entry.msgstr_plural[0] = translation
else:
entry.msgstr = translation
logger.info(f"""==================
{self.params.original_language}: "{original_phrase}"
{self.params.context_language}: "{context_translation}"
{self.target_language}: "{translation}"
Comment:{explanation if explanation else ''}
""")
sleep(1.0) # Sleep for 1 second to avoid rate limiting

if entry.msgid_plural: # entry with plural management. Now manage the plural case
original_phrase = entry.msgid_plural
context_translation = entry.msgstr_plural[1] if entry.msgstr_plural else entry.msgid_plural
translation, explanation = self.translate(original_phrase, context_translation)
# Update translation
entry.msgstr_plural[1] = translation
# Note: the plural explanation is not stored in the out po file.
logger.info(f"""================== PLURAL CASE ==================
{self.params.original_language}: "{original_phrase}"
{self.params.context_language}: "{context_translation}"
{self.target_language}: "{translation}"
Comment:{explanation if explanation else ''}
""")
sleep(1.0) # Sleep for 1 second to avoid rate limiting

nb_translations += 1
except Exception as e:
logger.error(f"Error: {e}")
Expand Down
8 changes: 8 additions & 0 deletions tests/input/input-small.po
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,11 @@ msgid ""
msgstr ""
"%(follower_name)s a créé un nouveau %(followed_type)s "
"\"%(followed_object_name)s\""

#: chat/templates/chat/chat_rooms.html:47 chat/tests/tests_private.py:46
#: chat/tests/tests_public.py:64 chat/tests/tests_public.py:94
#, python-format
msgid "%(nmsgs)s message"
msgid_plural "%(nmsgs)s messages"
msgstr[0] "%(nmsgs)s message"
msgstr[1] "%(nmsgs)s messages"

0 comments on commit 1cb3fea

Please sign in to comment.