python-babel / babel

The official repository for Babel, the Python Internationalization Library
http://babel.pocoo.org/
BSD 3-Clause "New" or "Revised" License
1.31k stars 438 forks source link

Document how to deal with .desktop or AppData files #536

Open muelli opened 6 years ago

muelli commented 6 years ago

I'm curious to use Babel for my desktop application but I'm lost how to hook anything non-python up with it. So I'd appreciate some references to real-world examples using Babel for shipping translated software.

akx commented 6 years ago

Hi!

Babel itself does not ship translation machinery, since gettext is in the Python standard library. Babel does supply bits and pieces for replacing the msgmerge/msgfmt/... utilities that are part of the C-based gettext command-line toolchain though.

It does contain machinery for localizing datetimes, numbers, etc., though, which is out of scope for gettext (or similar libraries).

I'm not sure what you mean with ".desktop or AppData files" here – can you clarify?

muelli commented 6 years ago

sure.

At the end of the day, I want to produce a translated .desktop and appdata file, like modern applications do.

There are several steps that need to be performed. One of them is message extraction. Babel's documentation currently acknowledges that Python projects rarely use a Makefile and a file-based approach to produce translations. Now one question is: How do Python applications (using Babel) extract messages out of the .desktop and appdata files?

Eventually, you have a message catalogue and want to produce the translated files. Traditionally, gettext (or even more legacy: intltool) were used. More precisely, given existing PO files, msgfmt is being called to take an input file and produce a translated version. Babel's documentation claims that it's "similar to the GNU msgfmt tool". Hence a question is: How do I use Babel such that it translates the file in all languages possible during installation of the app?

akx commented 6 years ago

Alright, thanks for the clarification! I don't work much with the Linux desktop, so I wasn't aware of these formats. The more you learn...

So I delved into some more source to understand how things work – my notes from that delve:

I'm not aware of a Babel extractor for desktop or appdata files, but considering their (apparent!) simplicity, it shouldn't be too hard to write an extractor or two for them.

Likewise, I'm afraid you'd have to put some additional elbow grease into iterating through all of the message catalogs your application has and generating the .desktop/appdata files from their templates with all the applicable translations. You'd probably also have to use the extractor in tandem here, to identify the strings that should be "exploded" into translations in the final file.

I'll see if I can whip up some example code...

akx commented 6 years ago

... and some example code was whipped up. https://github.com/akx/babel-desktop-files

An excerpt from the generated file:

# ...
Name[ar]=پولاري
Comment[ar]=عميل المحادثة المنقولة بالإنترنت (IRC) لجنوم
Keywords[ar]=عميل محادثة منقولة;آي آر سي;اي ار سي;محادثة;منقولة;إنترنت;انترنت;
Comment[as]=GNOME ৰ বাবে এটা ইন্টাৰনেট ৰিলে চেট ক্লাএন্ট
Keywords[as]=IRC;ইন্টাৰনেট;ৰিলে;চেট;
Comment[bs]=Internet Relay Chat klijent za GNOME
Keywords[bs]=IRC, internet, Relay, Chat;
Comment[ca]=Un client d'IRC per al GNOME
Keywords[ca]=IRC;Internet;Relé;Xat;
Comment[ca]=Un client d'IRC per al GNOME
Keywords[ca]=IRC;Internet;Relé;Xat;
Comment[cs]=Komunikační klient IRC pro GNOME
Keywords[cs]=IRC;Internet;Relay;Chat;diskuze;diskuzní;komunikace;
Comment[da]=En Internet Relay Chat-klient for GNOME
Keywords[da]=IRC;Internet;Relay;Chat;Snak;
Comment[de]=Ein IRC-Client für GNOME
Keywords[de]=IRC;Internet;Chat;
Comment[el]=Ένας πελάτης συνομιλίας μεταγωγής διαδικτύου για το GNOME
Keywords[el]=IRC;διαδίκτυο;μεταγωγή;συνομιλία;Internet;Relay;Chat;
Name[eo]=Polario
Comment[eo]=IRC-a kliento por GNOME
Keywords[eo]=IRC;Interreta;relajsa;babilo;
Comment[es]=Un cliente de IRC para GNOME
# ...

HTH! :)

muelli commented 6 years ago

not too bad :) Now this is a Makefile approach and is thus a bit uncomfortable for people doing pip install https://github....#egg=foo, I believe.

I eventually managed to convince python setup.py extract_messages to extract desktop files. I was fighting with Babel not accepting a function defined in setup.py itself, so I had to put it somewhere else. Maybe that extractor helps towards a solution for using Babel for desktop apps.

I haven't been able to nicely generate the translated desktop (and appdata) files though. I'd appreciate something that hooks more or less nicely into the regular python setup.py install call that people do (they do, don't they..?).

akx commented 6 years ago

Sure, overriding the install command would be a good idea - I was just using makefiles because it was less trouble for a PoC. This Stack Overflow answer seems to be the ticket -- paraphrasing so as not to force people to follow the link:

from setuptools import setup
from setuptools.command.develop import develop
from setuptools.command.install import install

class PostInstallCommand(install):
    """Post-installation for installation mode."""
    def run(self):
        do_the_desktop() # do the .desktop generation hustle (/¯–‿・)/¯
        install.run(self)

setup(
    # ...
    cmdclass={
        'install': PostInstallCommand,
    },
)
akx commented 6 years ago

By the way, re funcname = key # FIXME: Why can I not assign that name to funcname?, funcname is expected to be one of the keywords you've specified for the extractor, in order to follow gettext plurality tradition. I agree the magic-tuple-based extractor API in Babel is clunky, though in its defense, it's old enough to be in primary school...

muelli commented 6 years ago

uh. I'm afraid I don't understand what the "gettext plurality tradition" is :-/ The documentation is also extremely sparse of how that's supposed to be working.

akx commented 6 years ago

Sorry for not replying earlier...

I meant that gettext is for translating simple singulars, ngettext for plurals, etc.

muelli commented 6 years ago

FTR: We have extracted some bits into https://github.com/gnome-keysign/babel-glade It extracts messages from desktop, glade and appdata files and produces a translated version. We plan to have separate modules for each of the files, but that's really low priority. It may not be feature (or bug) compatible with the alternatives. In particular, I have yet to find out what the intended behaviour for nested XML elements is. But it seems to work well enough for us for now.

As always: Patches welcome :)