Skip to content

Conversation

aryoda
Copy link
Contributor

@aryoda aryoda commented Aug 28, 2023

Don't forget the changelog entry! ;)

Done :-)

@buhtz I hope the gettext translation does still work with my heavy string concatenation (required to avoid duplicated sub strings/prefixes in the messages)

@aryoda
Copy link
Contributor Author

aryoda commented Aug 29, 2023

The format() is executed before the translation.

So which string does a translator see on Weblate for this code in line 1252:

 rsync_exit_code_msg = _("'rsync' ended with exit code {exit_code}".format(exit_code=rsync_exit_code))
  1. 'rsync' ended with exit code {exit_code}

or

  1. 'rsync' ended with exit code 24

I was hoping the translator does see 1.

If the translator sees 2. I am just curious how gettext/Weblate is able to extract all strings from the source code incl. applying all possible formats first (without executing the code with all possible values)!

@buhtz
Copy link
Member

buhtz commented Aug 29, 2023

Yes, this topic can make headaches. 😄

The format() is executed before the translation.

So which string does a translator see on Weblate for this code in line 1252:

 rsync_exit_code_msg = _("'rsync' ended with exit code {exit_code}".format(exit_code=rsync_exit_code))
  1. 'rsync' ended with exit code {exit_code}

or

  1. 'rsync' ended with exit code 24

I was hoping the translator does see 1.

The translator on weblate will see nothing because the gettext utilities (e.g. xgettext) will ignore that line. They ignore it because it is not a string only. Tools like xgettext looking for patterns like _("..."). But you have _("...".format(foo)) which is not only a string.

If you want number 1 then you have to give exact that string to _() and nothing else.

_("'rsync' ended with exit code {exit_code}")

But you will fill in the variable exit_code when BIT code is executed right? Just add .format() to the translated string returned by _().

_("'rsync' ended with exit code {exit_code}").format(exit_code=rsync_exit_code)

The _() is a function taking a string and return another one. In German it will return "'rsync' wurde mit exit code {exit code} beendet". So when the code is executed by Python with German local the format() is practically something like this:

"'rsync' wurde mit exit code {exit code} beendet".format(exit_code=rsync_exit_code)

If the translator sees 2. I am just curious how gettext/Weblate is able to extract all strings from the source code incl. applying all possible formats first (without executing the code with all possible values)!

The gettext tools (to my knowledge) all working on text-file-parsing. They do not execute code. Your example is ignored because it is not just a string.

I see no need for "applying all possible formats first". The opposite should be the case. Strings should be translated before applying format(). The all possible formats should be applied after translation.

@aryoda
Copy link
Contributor Author

aryoda commented Aug 29, 2023

@buhtz THX a lot, static code analysis (searching for plain strings) is the simple magic in providing strings to Weblate

So if I understand you right I should change line 1252 from

rsync_exit_code_msg = _("'rsync' ended with exit code {exit_code}".format(exit_code=rsync_exit_code))

to

rsync_exit_code_msg = _("'rsync' ended with exit code {exit_code}").format(exit_code=rsync_exit_code)

so that translators can rearrange the position of the {exit_code} variable in the translated text according to the target language?

@buhtz
Copy link
Member

buhtz commented Aug 29, 2023

So if I understand you right I should ...

Correct.

@aryoda
Copy link
Contributor Author

aryoda commented Aug 29, 2023

OK, all translations should now be enabled correctly

@aryoda aryoda requested a review from buhtz August 29, 2023 11:29
@aryoda aryoda merged commit a95cf2e into bit-team:dev Aug 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants