3

I followed Python i18n and l10n process to externalize and translate text message in Python app. But when I package Python code into a wheel package, I can't find any guidance in setuptools docs.

Because the localized messages files can be treated as data files. Perhaps, I could use package_data parms to include those files. However, it doesn't seem to be right way to do this. Because the deployed localized messages files should be either in system default locale location /usr/share/locale or user-specific location. In either way, I find it difficult to connect pkg_resources package to gettext package without messing with real physical path hacking.

3

1 Answer 1

4

Here is what I did. I verified that the wheel package deployed and loaded localized *.mo message catalog file properly in Linux, Mac OSX and Windows 10.

  1. Move your locales folder under your top level Python package folder. For example, let say your package name pkg1 and you have my_msg.mo catalog file for French locale. Move your *.mo file to pkg1/locales/fr/LC_MESSAGES/my_msg.mo

  2. In your setup.py, add: ... package_data={'pkg1': ['pkg1/locales//LC_MESSAGES/.mo']}, include_package_data=True, ...

  3. In your Python script, use the following way to load without hard code any physical path: ... locale_path = pkg_resources.resource_filename('pkg1', 'locales') my_msg = gettext.translation(domain='my_msg', localedir=locale_path, fallback=True) _T = my_msg.gettext print(_T("hello world!")) ...

Sign up to request clarification or add additional context in comments.

3 Comments

Under point 2 better use package_data={'pkg1': ['locales/*/LC_MESSAGES/*.mo']}, ... otherwise the .mo file was not found in my case. Furthermore additonal using of Manifest.in is not bad.
you're my hero! you've no clue how long I've been looking for this..
It took me exactly 8 days to figure this out. Cheers, mate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.