2

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.

Ricky Zhang
  • 140
  • 1
  • 9
  • 2
    Possible duplicate of [What is the correct way to include localisation in python packages?](https://stackoverflow.com/questions/22958245/what-is-the-correct-way-to-include-localisation-in-python-packages) – phd Mar 26 '19 at 21:27
  • 1
    https://stackoverflow.com/search?q=%5Bsetuptools%5D+localization+files – phd Mar 26 '19 at 21:27
  • It works. I placed *.mo file under the top level package name so that they get deployed under package folder `lib/python3.7/site-packages/my_top_level_package/locales`. I have verified that it is portable in both Mac OS X, Linux and Windows 10. – Ricky Zhang Apr 03 '19 at 15:15

1 Answers1

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!")) ...

Ricky Zhang
  • 140
  • 1
  • 9