本文介绍了如何配置我的Python 2.7在我的OSX Mavericks系统上使用正确的Babel安装?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我。

我有一个OSX Mavericks机器,我使用MacPorts安装Python 2.7和Babel 1.3。
问题是,似乎我在该机器上至少有两个版本的babel,除了我使用MacPort安装的一个,另一个没有安装CLDR语言环境数据文件,所以当我执行以下命令在我的应用程序目录中:

I have an OSX Mavericks machine where I installed Python 2.7 and Babel 1.3 using MacPorts.The problem is that it seems I have at least two versions of babel on that machine and except for the one I installed using MacPort, the other does not have the CLDR locale data files installed, so when I execute the following command in my application directory :

pybabel extract -o ./locale/messages.pot ./

我收到此错误讯息:

[...]
writing PO template file to ./locale/messages.pot
Traceback (most recent call last):
  File "/usr/local/bin/pybabel", line 8, in <module>
    load_entry_point('Babel==2.0-dev-20131218', 'console_scripts', 'pybabel')()
  File "/Library/Python/2.7/site-packages/Babel-2.0_dev_20131218-py2.7.egg/babel/messages/frontend.py", line 1151, in main
    return CommandLineInterface().run(sys.argv)
  File "/Library/Python/2.7/site-packages/Babel-2.0_dev_20131218-py2.7.egg/babel/messages/frontend.py", line 665, in run
    return getattr(self, cmdname)(args[1:])
  File "/Library/Python/2.7/site-packages/Babel-2.0_dev_20131218-py2.7.egg/babel/messages/frontend.py", line 947, in extract
    sort_by_file=options.sort_by_file)
  File "/Library/Python/2.7/site-packages/Babel-2.0_dev_20131218-py2.7.egg/babel/messages/pofile.py", line 427, in write_po
    messages = list(catalog)
  File "/Library/Python/2.7/site-packages/Babel-2.0_dev_20131218-py2.7.egg/babel/messages/catalog.py", line 552, in __iter__
    for name, value in self.mime_headers:
  File "/Library/Python/2.7/site-packages/Babel-2.0_dev_20131218-py2.7.egg/babel/messages/catalog.py", line 337, in _get_mime_headers
    locale='en')))
  File "/Library/Python/2.7/site-packages/Babel-2.0_dev_20131218-py2.7.egg/babel/dates.py", line 609, in format_datetime
    locale = Locale.parse(locale)
  File "/Library/Python/2.7/site-packages/Babel-2.0_dev_20131218-py2.7.egg/babel/core.py", line 284, in parse
    language = get_global('language_aliases').get(language, language)
  File "/Library/Python/2.7/site-packages/Babel-2.0_dev_20131218-py2.7.egg/babel/core.py", line 53, in get_global
    _raise_no_data_error()
  File "/Library/Python/2.7/site-packages/Babel-2.0_dev_20131218-py2.7.egg/babel/core.py", line 25, in _raise_no_data_error
  raise RuntimeError('The babel data files are not available. '
RuntimeError: The babel data files are not available. This usually happens because you are using a source checkout from Babel and you did not build the data files.  Just make sure to run "python setup.py import_cldr" before installing the library.

我在我的机器上的这些位置安装了Babel:

I found Babel installed at these locations on my machine :

> cd /Library/Python/2.7/site-packages/Babel-2.0_dev_20131218-py2.7.egg/
> ls
EGG-INFO    babel
> cd babel
> ls
__init__.py     core.py     localedata.py   numbers.py  support.py
__init__.pyc    core.pyc    localedata.pyc  numbers.pyc support.pyc
_compat.py      dates.py    localtime       plural.py   util.py
_compat.pyc     dates.pyc   messages        plural.pyc  util.pyc

> cd /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/babel
> ls
__init__.py     core.py     localedata      numbers.py  support.py
__init__.pyc    core.pyc    localedata.py   numbers.pyc support.pyc
_compat.py      dates.py    localedata.pyc  plural.py   util.py
_compat.pyc     dates.pyc   localtime       plural.pyc  util.pyc
                global.dat  messages

这些是我的环境中定义的各种路径:

These are the various paths defined in my environment :

> echo $PATH
/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/Users/XXXXX/.rvm/bin
> echo $PYTHONPATH

> echo $DYLD_FALLBACK_LIBRARY_PATH

> echo $DYLD_LIBRARY_PATH

> echo $DYLD_FALLBACK_FRAMEWORK_PATH

> echo $DYLD_FRAMEWORK_PATH

>

pybabel是使用以下命令创建的别名:

pybabel is an alias i created using the following command :

> alias pybabel='pybabel-2.7'



如何使babel在/ opt / local / Library / Frameworks / Python.framework / Versions / 2.7 / lib / python2.7 / site-packages / babel?

How can I make babel see the Babel version in /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/babel?

推荐答案

我尝试设置PYTHONPATH但出错了...

I tried setting the PYTHONPATH but made a mistake ...

我在egg存档中创建了一个符号链接pybabel在MacPorts版本中使用localedata /似乎有效:

I created a symbolic link in the egg archive pybabel was using to the localedata/ in the MacPorts version and it seems to have worked :

> cd /Library/Python/2.7/site-packages/Babel-2.0_dev_20131218-py2.7.egg/babel
> ln -s /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/babel/localedata localedata
ln: localedata: Permission denied
> sudo ln -s /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/babel/localedata localedata
Password:
>

如果有人有更好的建议或建议,请留下评论或另一个答案。谢谢。

If someone has better suggestions or advices, please leave a comment or another answer. Thank you.

[follow-up 2014-01-09] ------------------- -------------------------------------------------- -----------------------------------------

我仍然无法翻译{%trans%}标签,我发现,你需要明确地使用-F选项在pybabel像这样(即使babel.cfg文件是在当前目录!):

I still could not get the "{% trans %}" tags translated, I found here that you need to explicitly use the -F option on pybabel like that (even if the babel.cfg file is in the current directory!):

> pybabel -v extract -F babel.cfg -o ./locale/messages.pot ./

帮助这里是我的babel.cfg文件的内容:

And to help here is the content of my babel.cfg file :

[jinja2: **/templates/**.html]
encoding = utf-8
[python: python/*.py]
[extractors] 
jinja2 = jinja2.ext:babel_extract

但是我有ImportError:No module named ...错误消息,因为我没有正确配置jinja2和markupsafe。所以,我添加了两个符号链接:

But I got "ImportError: No module named ..." error messages because I did not have jinja2 and markupsafe properly configured. So, I added two symlinks :

> sudo ln -s /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/jinja2 /Library/Python/2.7/site-packages/jinja2
> sudo ln -s /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/markupsafe /Library/Python/2.7/site-packages/markupsafe

它终于工作了!我得到了我的messages.pot文件中的{%trans%}标签的内容!

And it finally worked! I got the content of the "{% trans %}" tags in my messages.pot file!

然而,我不知道这是否是他最好的解决方案...

Yet, I wonder if it is he best solution ...

这篇关于如何配置我的Python 2.7在我的OSX Mavericks系统上使用正确的Babel安装?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 14:43