pygettext.py and msgfmt.py on Mac OS X
Asked Answered
B

5

9

I want to translate strings in my Python app on my Mac OS X 10.7. I can import the gettext module but i can't find the tools pygettext.py and msgfmt.py, which according to the Python docs should be somewhere in my Python installation.

Is the pre-installed Python version on Mac OS X 10.7 missing these tools and if yes, how can I get them?

Thanks.

Bowsprit answered 12/7, 2012 at 14:47 Comment(0)
C
8

Both of those scripts reside in the miscellaneous Tools directory of the Python source. This directory is often not included in a binary installation of Python such as the ones supplied by Apple in OS X. However, it is easy to download them separately from a Python source release; see the most recent releases here. For the current Python 2.7.3 release, you could do the following:

$ curl -O http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz
$ tar -xf Python-2.7.3.tgz 
$ cd Python-2.7.3
$ cd Tools/i18n/
$ ls
makelocalealias.py  msgfmt.py*          pygettext.py*

Beginning with Python 3.2, the Tools directory is installed by python.org OS X binary installers. You'll find it inside the Python framework at:

/Library/Frameworks/Python.framework/Versions/3.2/share/doc/python3.2/examples/Tools
Cessation answered 12/7, 2012 at 17:22 Comment(3)
Thanks, I downloaded the Tools separately.Bowsprit
on Mac 10.8.5 curl did not work for me, but wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz did the trickWaggon
no reason curl shouldn't work, @jrgray. You might need -L in case the URL does a redirect to the actual location of the file you're dowloading.Washko
A
14

With python 3 and MacOS >= 10.9, installing gettext via homebrew won't link the libraries and this will produce the error Can't find msgfmt.

This helps:

$ brew install gettext  # if not already done
$ brew link gettext --force

Thanks to https://mcmap.net/q/206239/-django-1-3-1-compilemessages-error-sh-msgfmt-command-not-found

Ase answered 28/9, 2015 at 11:23 Comment(0)
C
8

Both of those scripts reside in the miscellaneous Tools directory of the Python source. This directory is often not included in a binary installation of Python such as the ones supplied by Apple in OS X. However, it is easy to download them separately from a Python source release; see the most recent releases here. For the current Python 2.7.3 release, you could do the following:

$ curl -O http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz
$ tar -xf Python-2.7.3.tgz 
$ cd Python-2.7.3
$ cd Tools/i18n/
$ ls
makelocalealias.py  msgfmt.py*          pygettext.py*

Beginning with Python 3.2, the Tools directory is installed by python.org OS X binary installers. You'll find it inside the Python framework at:

/Library/Frameworks/Python.framework/Versions/3.2/share/doc/python3.2/examples/Tools
Cessation answered 12/7, 2012 at 17:22 Comment(3)
Thanks, I downloaded the Tools separately.Bowsprit
on Mac 10.8.5 curl did not work for me, but wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz did the trickWaggon
no reason curl shouldn't work, @jrgray. You might need -L in case the URL does a redirect to the actual location of the file you're dowloading.Washko
N
1

create a symbolic link

sudo ln -s /Library/Frameworks/Python.framework/Versions/3.7/share/doc/python3.7/examples/Tools/i18n/pygettext.py /usr/local/bin/pygettext
Notecase answered 29/3, 2019 at 20:36 Comment(0)
P
0

If you have Python correctly installed in Linux you should be able to access the binary versions from the command line. So, to make the .pot file, you can type:

    $> pygettext [options] source_file_list

I found I had two versions of pygettext, one for 2.7, and another for 3.2, but they seem to work pretty much the same. To create the .mo files from the .po files:

    $> msgfmt [options] file.po

but you need to make sure you do not accidentally use the gnu version. The Python version of msgfmt is much simpler. Both commands have help, so you can check the options (and versions) with:

    $> pygettext --help

and

    $> msgfmt --help

If in doubt, downloading the Python source as suggested above is the belt-and-braces approach.

I am still finding my way round i18n myself, so if someone knows better, please add your comments :)

Papilloma answered 30/7, 2012 at 3:18 Comment(0)
U
0

By using find . -name pygettext.py/msgfmt.py, I can't find these two files on macOS, no mater in system python installation (/System/Library/Frameworks/Python.framework/Versions) or in Anaconda Python distribution.

But we can always directly download these two files from the Python source code. At this time, they located in: https://github.com/python/cpython/tree/master/Tools/i18n


Besides, pygettext.py and msgfmt.py are the mimics of GNU gettext toolset: xgettext and msgfmt. You can also check whether or not these GNU toolset have already been installed. (type -a xgettext/msgfmt)

If not, you can install then by using either MacPort's port command or Anaconda's conda command.

port install gettext
conda install gettext
Upolu answered 7/6, 2019 at 4:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.