Load GTK-Glade translations in Windows using Python/PyGObject

落爺英雄遲暮 提交于 2019-12-06 11:00:32
Havok

You are close. This is a very complicated subject.

As I wrote in Question 10094335 and in Question 3678174:

To setup the locale to user current locale do not call:

locale.setlocale(locale.LC_ALL, locale.getlocale())

Simply call:

locale.setlocale(locale.LC_ALL, '')

As explained in Python setlocale reference documentation.

This sets the locale for all categories to the user’s default setting (typically specified in the LANG environment variable).

Note that Windows doesn't have the LANG environment variable set up, so, you need to do this before that line:

import sys
import os
import locale

if sys.platform.startswith('win'):
    if os.getenv('LANG') is None:
        lang, enc = locale.getdefaultlocale()
        os.environ['LANG'] = lang

This will also make gettext to work for in-Python translations.

How this work you can check it in the source code here:

https://github.com/python/cpython/blob/master/Modules/_localemodule.c#L90

In particular, the error you're getting:

locale.Error: unsupported locale setting

Is expressed here:

https://github.com/python/cpython/blob/master/Modules/_localemodule.c#L112

Which is just a generic error message that the C call setlocale failed with the given parameters.

The C call setlocale is defined in the locale.h header. In Linux, this is:

In Windows, this is the one used:

In Windows locale.h documentation you can read:

The set of language and country/region strings supported by setlocale are listed in Language Strings and Country/Region Strings.

And that points to:

As you can see, for the 2010 version the setlocale function expects the locale in the format you found out: deu_deu, that differ from the one expected by the Linux version de_DE. Your only option is to use a list of os-dependent locales to setup the locale. Very very sad indeed.

There is another issue here. If you change the version of the toolchain you can see that newer version of the setlocale function now work more closelly to what Linux/POSIX does:

american english en-US

Visual Studio 2010 is the last release to support the old format, starting from version 2012 the new locale format is expected.

As you can imagine, the one you need to use depends on the version of the toolchain for which the CPython interpreter you're using was built to. I don't know which version are you using but according to the official Python Developer's Guide:

Python 3.5 and later use Microsoft Visual Studio 2015. [...] Python 3.3 and 3.4 use Microsoft Visual Studio 2010. [...] Most Python versions prior to 3.3 use Microsoft Visual Studio 2008. [...]

That is all related to the Python locale module. Now, for the gettext module or the gettext related functions in the locale module, this is another C library called libintl.

libintl is what is called the C library that is part of gettext for all this translation magic:

One relevant part of this documentation says:

Note that on GNU systems, you don’t need to link with libintl because the gettext library functions are already contained in GNU libc.

But in Windows, because of the issues explained in Question 10094335 you need to load the libintl library that is being used by PyGObject, that is, the very same that it was linked during build. That is done doing the steps you already wrote.

Which intl.dll do I need to include? (I tried the gnome/libintl-8.dll from this source: http://sourceforge.net/projects/pygobjectwin32/, (pygi-aio-3.14.0_rev19-setup.exe))

So, yes. The one that was used to link against when the pygobject AIO was build.

How can I check if the e.g. locale deu_deu gets the correct /mo/de/LC_MESSAGES/appname.mo/

Configure a few messages and note if they show translated. Just a note, is not a folder "/mo/de/LC_MESSAGES/appname.mo/", appname.mo is a file.

Check my first answer to how to create the translation .po file from the Glade file.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!