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

Enable gettext translation for glade dialogs. #75

Merged
merged 4 commits into from
Mar 5, 2015

Conversation

kurisuke
Copy link
Collaborator

@kurisuke kurisuke commented Mar 1, 2015

Glade dialogs created by Gtk.Builder use the C library gettext
interface. To enable localization for this interface, use setlocale /
bindtextdomain from the python locale module.

For a detailed explanation see: https://stackoverflow.com/questions/10094335/how-to-bind-a-text-domain-to-a-local-folder-for-gettext-under-gtk3

Glade dialogs created by Gtk.Builder use the C library gettext
interface. To enable localization for this interface, use setlocale /
bindtextdomain from the python locale module.
@multani
Copy link
Owner

multani commented Mar 1, 2015

I actually removed the call to locale.bindtextdomain in 2c533b3 because of the problem reported in #67.

The comment I was looking into in Python's documentation was the following:

Python applications should normally find no need to invoke these functions, and should use gettext instead. A known exception to this rule are applications that link with additional C libraries which internally invoke gettext() or dcgettext(). For these applications, it may be necessary to bind the text domain, so that the libraries can properly locate their message catalogs.

It looks like I was wrong and I should have used locale.bindtextdomain for Gtk to get the correct locales. Still, it seems it doesn't work on OS X and I can't test this as I don't have access to this plateform. I'll see with the guys from #67, or if you have an idea on this...?

@multani multani mentioned this pull request Mar 1, 2015
@kurisuke
Copy link
Collaborator Author

kurisuke commented Mar 1, 2015

Thanks for pointing me to the issue, I totally overlooked this.
Unfortunately, I don't have access to OSX either.

Regarding your quote from the Python docs:
It seems the bindtextdomain() function ist actually not provided by the OSX libc, which seems to be perfectly acceptable behaviour, as it is specific to GNU gettext.. I will update this push request to check whether the function is actually available in the locale module (hasattr).

Peter Helbing added 2 commits March 1, 2015 16:01
locale.bindtextdomain() is an interface to the eponymous libc
function, which is GNU gettext specific and may not be provided on
some systems (e.g. OSX). See:
https://docs.python.org/3/library/locale.html#access-to-message-catalogs
@multani
Copy link
Owner

multani commented Mar 3, 2015

Thanks, I appreciate the comments you wrote!

So, I finally had a tried and I'm not convinced:

  • if I use the code in master (so without your fix), all the UI seemed to be translated, ie. LANG=fr_FR.UTF-8 ./run-sonata gets me a French Sonata. However, this only works if the catalog file has been correctly compiled for this lang, ie. only if sonata/share/locale/fr/LC_MESSAGES/sonata.mo exists. These files get generated when you execute setup.py:
$ python3 setup.py             
Generating sonata/share/locale/ar/LC_MESSAGES/sonata.mo
Generating sonata/share/locale/be/LC_MESSAGES/sonata.mo
Generating sonata/share/locale/ca/LC_MESSAGES/sonata.mo
Generating sonata/share/locale/cs/LC_MESSAGES/sonata.mo
Generating sonata/share/locale/da/LC_MESSAGES/sonata.mo
Generating sonata/share/locale/de/LC_MESSAGES/sonata.mo
Generating sonata/share/locale/el_GR/LC_MESSAGES/sonata.mo
Generating sonata/share/locale/es/LC_MESSAGES/sonata.mo
Generating sonata/share/locale/et/LC_MESSAGES/sonata.mo
Generating sonata/share/locale/fi/LC_MESSAGES/sonata.mo
Generating sonata/share/locale/fr/LC_MESSAGES/sonata.mo
Generating sonata/share/locale/it/LC_MESSAGES/sonata.mo
Generating sonata/share/locale/ja/LC_MESSAGES/sonata.mo
Generating sonata/share/locale/ko/LC_MESSAGES/sonata.mo
Generating sonata/share/locale/nl/LC_MESSAGES/sonata.mo
Generating sonata/share/locale/pl/LC_MESSAGES/sonata.mo
Generating sonata/share/locale/pt_BR/LC_MESSAGES/sonata.mo
Generating sonata/share/locale/ru/LC_MESSAGES/sonata.mo
Generating sonata/share/locale/sk/LC_MESSAGES/sonata.mo
Generating sonata/share/locale/sl/LC_MESSAGES/sonata.mo
Generating sonata/share/locale/sv/LC_MESSAGES/sonata.mo
Generating sonata/share/locale/tr/LC_MESSAGES/sonata.mo
Generating sonata/share/locale/uk/LC_MESSAGES/sonata.mo
Generating sonata/share/locale/zh_CN/LC_MESSAGES/sonata.mo
Generating sonata/share/locale/zh_TW/LC_MESSAGES/sonata.mo
usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
   or: setup.py --help [cmd1 cmd2 ...]
   or: setup.py --help-commands
   or: setup.py cmd --help

error: no commands supplied

although I recognized it's probably less than ideal and not really documented. Could you have been running in this case where the correct locale file doesn't exist?

  • also, running Sonata with your patch and an unsupported locale now crashes:
$ LANG=foo ./run-sonata                      
Traceback (most recent call last):
  File "./run-sonata", line 23, in <module>
    sonata.launcher.run()
  File "/home/jon/projects/sonata/sonata/sonata/launcher.py", line 126, in run
    locale.setlocale(locale.LC_ALL, '')
  File "/usr/lib/python3.4/locale.py", line 592, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting

I don't think it should :)

@kurisuke
Copy link
Collaborator Author

kurisuke commented Mar 3, 2015

I didn't test for unsupported locales! Good idea to check for it :)

Actually, it seems to be even more complicated:

  • Glade strings are only translated if the locale is supported by the C library. You can check supported locales by locale -a.
  • Python strings (handled by the gettext module) will always be translated if the .mo file is installed with sonata, even if the language is not supported by the C library.

So if a locale is not supported by the C library, but the catalog file is installed with sonata, we will have a mix of languages in the UI.
E.g. on my system I don't support fr_FR.UTF-8 with libc, but if I start sonata with fr_FR.UTF-8 ./run-sonata, I will get all UI in french except the Glade dialogs, which are untranslated.

I see two possible ways to handle this:

  1. Do nothing. Any translated strings are better than no translated strings. try for an exception and pass in the except block
  2. When the locale.setlocale() call fails, call gettext.install() to provide the _() function, but don't point it to the translation files: localedir=None. So all strings in the application will be left untranslated.

What do you think?

@multani
Copy link
Owner

multani commented Mar 4, 2015

On Tue, Mar 03, 2015 at 01:37:13PM -0800, Peter Helbing wrote:

I didn't test for unsupported locales! Good idea to check for it :)

Actually, it seems to be even more complicated:

  • Glade strings are only translated if the locale is supported by the C library. You can check supported locales by locale -a.
  • Python strings (handled by the gettext module) will always be translated if the .mo file is installed with sonata, even if the language is not supported by the C library.

So if a locale is not supported by the C library, but the catalog file is installed with sonata, we will have a mix of languages in the UI.
E.g. on my system I don't support fr_FR.UTF-8 with libc, but if I start sonata with fr_FR.UTF-8 ./run-sonata, I will get all UI in french except the Glade dialogs, which are untranslated.

Yeah, you are right, good catch!

I see two possible ways to handle this:

  1. Do nothing. Any translated strings are better than no translated strings. try for an exception and pass in the except block
  2. When the locale.setlocale() call fails, call gettext.install() to provide the _() function, but don't point it to the translation files: localedir=None. So all strings in the application will be left untranslated.

I would say the second on looks better to me - at least it makes things
coherent. I tried running the first one with unsupported locales on my
system, and it definitively looks weird with half the UI translated.

If you decide to go this way, can you also log the problem so at least
an user running Sonata from the command line knows that something went
wrong?

Provide uniform translation for both GObject / Glade strings
(initialized by module locale) and Python gettext strings to
prevent a mix of languages in UI strings.

If a locale is provided with sonata but not generated for the C
library, Python strings will be translated, but strings in Glade
dialogs will not be translated. So if the initial locale.setlocale
call fails, don't point Python gettext to the .mo files -- this way
all strings will be left untranslated.
@kurisuke kurisuke force-pushed the fix-untranslated-glade-dialogs branch from 6fdcce4 to 628ffaf Compare March 5, 2015 16:41
@kurisuke
Copy link
Collaborator Author

kurisuke commented Mar 5, 2015

I agree with you :)
I updated the pull request accordingly (added a new commit) and also added an error print.

@multani multani merged commit 628ffaf into multani:master Mar 5, 2015
@multani
Copy link
Owner

multani commented Mar 5, 2015

I did some updates on your last patch (I moved out of the exception handler the code which wasn't related to the exception, and I changed the usage of print() with logger.error()), but thanks for investing most of it!

@kurisuke kurisuke deleted the fix-untranslated-glade-dialogs branch March 5, 2015 19:54
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this pull request Sep 14, 2019
1.7b1 (2016-01-31)
------------------

New features
''''''''''''

    * redirect to the localized Wikipedia according to the user's local,
      instead of redirecting to the English one.
      multani/sonata#61
    * add "Shuffle by album"
      multani/sonata#59
    * repeat single song (Corentin Néau)
      multani/sonata#95


Bug fixes
'''''''''

    * fix crashs on Gtk.TextView when updating the lyrics of a song
    * Fix streams parsing:
      multani/sonata#41
      multani/sonata#38
    * fix withdrawn mode by fixing incorrect usage of threads and Gtk
      eco-system
    * choosing a local artwork wasn't working anymore
    * fix artwork caching
    * keep the selected tab when hiding/showing Sonata using the tray icon
    * correctly handle tracks without a definitely reasonable track or disc
      value (Peter Helbing)
      multani/sonata#74
    * fix translation of of Glade dialogs (Peter Helbing)
      multani/sonata#75
    * fix opening the About dialog (based on Rocus van Oosten report).
    * fix refreshing profiles upon connections add/delete (Corentin Néau)
      multani/sonata#92
    * fix update of cover and text in fullscreen mode (Corentin Néau)
      multani/sonata#93

Internal changes
''''''''''''''''

    * slightly better performance when updating the library
    * threading improvements:

      - use GIO instead of a thread to download drag-and-droped covers
      - remove the MPD from a dedicated thread. It works as good (if not
        better) as before since the threaded code was poorly written anyway and
        rendered the additionnal basically useless. This move is not definitive
        but something better will probably come up some day.

    * Looking for covers isn't as filesystem expensive as before, but it
      doesn't check for multiple possible cases in the cover name anymore.


1.7a2 (2013-11-26)
------------------

New features
''''''''''''

    * Download bigger covers from Last.fm
    * Centering the current song using Ctrl+i now also selects the current song
    * Use Transifex as a translation plateform. See
      https://www.transifex.com/projects/p/sonata/
    * Update translations:

        - Russian (thanks Stas!)
        - Ukrainian (thanks Stas!)
        - French (thanks Jon!)

Bug fixes
'''''''''

    * Fix search in library (using a specific filter or the 'everything' filter)
    * Fix scrobbling to Last.fm
    * Scrolling with the mouse wheel on the tray icon correctly changes the volume
    * Some lyrics from LyricsWiki redirects to another page. The plugin now
      follows those redirect to find the actual lyrics.
    * Fix multiple issues related to drag-and-drop of songs in the current playlist
    * Fix error while using Sonata's command line interface
    * Fix filtering songs in the current playlist
    * Fix DND of custom artwork images in the main window
    * Fix breadcrumb icon in the library while browsing an album while in Artist view
    * Fix saving the visibility flag of tabs: multani/sonata#45
    * Fix hiding/showing the main window even if it's not the active widget:
      multani/sonata#43
    * Deleting a track doesn't toggle the filter bar anymore in the current playlist.
    * Better support for MPD 0.18

Internal changes
''''''''''''''''

    * Simplified the `current` module. It should also now use less memory than before.
    * Provide a new GObject signal to notify on artwork change and artwork reset.


1.7a1 (2013-02-08)
------------------

New features
''''''''''''

    * Fetch covers from Last.fm instead of Rhapsody.com
    * Custom plugins are now stored along the configuration file in ~/.config/sonata/plugins/
    * Switch to Python 3 and Gtk 3 (Jörg Thalheim & Adam Dane)
    * More items in the tray menu (Kirill Lashuk)
    * Better fullscreen support (Kirill Lashuk)
    * Toggle fullscreen from the command line (Daniel)
    * Support MPD's "consume" mode (Anton Lashkov)
    * Use more default icons in the context menus (Anton Lashkov)
    * Load only the most recent of plugins with the same names

Bug fixes
'''''''''

    * Fix population of the "Save to playlist" menu (Zhihao Yuan)
    * Prevent multiple entries and improve art search with multi-CD albums (Kirill Lashuk)
    * Fixes weird show up if Sonata is not on the current workspace (Kirill Lashuk)
    * Scrobble after seeking to the beginning (Kirill Lashuk)
    * The lyricswiki plugin should now work again (Jonathan Ballet)
    * Fix UI status after reconnection to MPD (Jonathan Ballet)
    * Fix crash when searching the library (Kirill Lashuk)

Internal changes
''''''''''''''''

    * Simpler API for cover fetching plugins
    * Lot of code cleanup and internal changes
    * Removed Sugar UI support
    * Use Glade files to describe the interface and GtkBuilder to build the interface
    * More systematic configuration file management
    * High-level access to MPD's command results
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this pull request Sep 15, 2019
1.7b1 (2016-01-31)
------------------

New features
''''''''''''

    * redirect to the localized Wikipedia according to the user's local,
      instead of redirecting to the English one.
      multani/sonata#61
    * add "Shuffle by album"
      multani/sonata#59
    * repeat single song (Corentin Néau)
      multani/sonata#95


Bug fixes
'''''''''

    * fix crashs on Gtk.TextView when updating the lyrics of a song
    * Fix streams parsing:
      multani/sonata#41
      multani/sonata#38
    * fix withdrawn mode by fixing incorrect usage of threads and Gtk
      eco-system
    * choosing a local artwork wasn't working anymore
    * fix artwork caching
    * keep the selected tab when hiding/showing Sonata using the tray icon
    * correctly handle tracks without a definitely reasonable track or disc
      value (Peter Helbing)
      multani/sonata#74
    * fix translation of of Glade dialogs (Peter Helbing)
      multani/sonata#75
    * fix opening the About dialog (based on Rocus van Oosten report).
    * fix refreshing profiles upon connections add/delete (Corentin Néau)
      multani/sonata#92
    * fix update of cover and text in fullscreen mode (Corentin Néau)
      multani/sonata#93

Internal changes
''''''''''''''''

    * slightly better performance when updating the library
    * threading improvements:

      - use GIO instead of a thread to download drag-and-droped covers
      - remove the MPD from a dedicated thread. It works as good (if not
        better) as before since the threaded code was poorly written anyway and
        rendered the additionnal basically useless. This move is not definitive
        but something better will probably come up some day.

    * Looking for covers isn't as filesystem expensive as before, but it
      doesn't check for multiple possible cases in the cover name anymore.


1.7a2 (2013-11-26)
------------------

New features
''''''''''''

    * Download bigger covers from Last.fm
    * Centering the current song using Ctrl+i now also selects the current song
    * Use Transifex as a translation plateform. See
      https://www.transifex.com/projects/p/sonata/
    * Update translations:

        - Russian (thanks Stas!)
        - Ukrainian (thanks Stas!)
        - French (thanks Jon!)

Bug fixes
'''''''''

    * Fix search in library (using a specific filter or the 'everything' filter)
    * Fix scrobbling to Last.fm
    * Scrolling with the mouse wheel on the tray icon correctly changes the volume
    * Some lyrics from LyricsWiki redirects to another page. The plugin now
      follows those redirect to find the actual lyrics.
    * Fix multiple issues related to drag-and-drop of songs in the current playlist
    * Fix error while using Sonata's command line interface
    * Fix filtering songs in the current playlist
    * Fix DND of custom artwork images in the main window
    * Fix breadcrumb icon in the library while browsing an album while in Artist view
    * Fix saving the visibility flag of tabs: multani/sonata#45
    * Fix hiding/showing the main window even if it's not the active widget:
      multani/sonata#43
    * Deleting a track doesn't toggle the filter bar anymore in the current playlist.
    * Better support for MPD 0.18

Internal changes
''''''''''''''''

    * Simplified the `current` module. It should also now use less memory than before.
    * Provide a new GObject signal to notify on artwork change and artwork reset.


1.7a1 (2013-02-08)
------------------

New features
''''''''''''

    * Fetch covers from Last.fm instead of Rhapsody.com
    * Custom plugins are now stored along the configuration file in ~/.config/sonata/plugins/
    * Switch to Python 3 and Gtk 3 (Jörg Thalheim & Adam Dane)
    * More items in the tray menu (Kirill Lashuk)
    * Better fullscreen support (Kirill Lashuk)
    * Toggle fullscreen from the command line (Daniel)
    * Support MPD's "consume" mode (Anton Lashkov)
    * Use more default icons in the context menus (Anton Lashkov)
    * Load only the most recent of plugins with the same names

Bug fixes
'''''''''

    * Fix population of the "Save to playlist" menu (Zhihao Yuan)
    * Prevent multiple entries and improve art search with multi-CD albums (Kirill Lashuk)
    * Fixes weird show up if Sonata is not on the current workspace (Kirill Lashuk)
    * Scrobble after seeking to the beginning (Kirill Lashuk)
    * The lyricswiki plugin should now work again (Jonathan Ballet)
    * Fix UI status after reconnection to MPD (Jonathan Ballet)
    * Fix crash when searching the library (Kirill Lashuk)

Internal changes
''''''''''''''''

    * Simpler API for cover fetching plugins
    * Lot of code cleanup and internal changes
    * Removed Sugar UI support
    * Use Glade files to describe the interface and GtkBuilder to build the interface
    * More systematic configuration file management
    * High-level access to MPD's command results
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