bro*_*njc 51 python django homebrew gettext
我正在尝试翻译一个字符串.
{% load i18n %}
{% trans "Well, Hello there, how are you?" %}
Run Code Online (Sandbox Code Playgroud)
至...
Hola amigo, ¿que tal?
Run Code Online (Sandbox Code Playgroud)
我的settings.py文件包含:
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'translations'),
)
Run Code Online (Sandbox Code Playgroud)
我得到了这个:
(env)glitch:translations nathann$ django-admin.py compilemessages
CommandError: Can't find msgfmt. Make sure you have GNU gettext tools 0.15 or newer installed.
Run Code Online (Sandbox Code Playgroud)
我也不明白这个错误信息.
(env)glitch:ipals nathann$ django-admin.py makemessages -l es
CommandError:
This script should be run from the Django Git tree or your project or
app tree. If you did indeed run it from the Git checkout or your project
or application, maybe you are just missing the conf / locale(in the
django tree) or locale(for project and application) directory? It is not
created automatically, you have to create it by hand if you want to
enable i18n for your project or application.
Run Code Online (Sandbox Code Playgroud)
文档:https://docs.djangoproject.com/en/1.6/ref/django-admin/#django-admin-makemessages
对于奖金upvotes,一个相关的问题:gettext在我安装时没有链接...对这个有任何帮助吗?我应该强迫它吗?
glitch:translations nathann$ brew link gettext
Warning: gettext is keg-only and must be linked with --force
Note that doing so can interfere with building software.
Run Code Online (Sandbox Code Playgroud)
谢谢!
更新:
我已经将翻译名称更改为语言环境并相应地更新了我的settings.py. 然后我再次运行它仍然抱怨gettext:
(env)glitch:ipals nathann$ mv translations/ locale
(env)glitch:ipals nathann$ django-admin.py makemessages -l es
CommandError: Can't find xgettext. Make sure you have GNU gettext tools 0.15 or newer installed.
Run Code Online (Sandbox Code Playgroud)
我也发现了这个:
看完之后:
(env)glitch:ipals nathann$ brew install gettext
Warning: gettext-0.18.3.2 already installed
(env)glitch:ipals nathann$ brew link gettext
Warning: gettext is keg-only and must be linked with --force
Note that doing so can interfere with building software.
Run Code Online (Sandbox Code Playgroud)
bro*_*njc 72
确认我在设置中有这个:
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locale'),
)
print(LOCALE_PATHS)
Run Code Online (Sandbox Code Playgroud)
我仔细检查了我的locale
目录在正确的位置,其名称拼写正确.
我最终链接了gettext(在超级用户上询问之后):
brew link gettext --force
manage.py compilemessages
django-admin.py makemessages -l es
Run Code Online (Sandbox Code Playgroud)
和BAM.我有我的po文件.
但医生说:
Warning: Some keg-only formula are linked into the Cellar.
Linking a keg-only formula, such as gettext, into the cellar with
`brew link <formula>` will cause other formulae to detect them during
the `./configure` step. This may cause problems when compiling those
other formulae.
Binaries provided by keg-only formulae may override system binaries
with other strange results.
You may wish to `brew unlink` these brews:
gettext
Run Code Online (Sandbox Code Playgroud)
Vij*_*pal 56
请在Ubuntu中试试
sudo apt-get install gettext
并在OSX中使用brew install gettext
还要确保在settings.py文件中设置本地路径.
这是针对翻译问题或者在Django中第一次创建多语言站点的解决方案.这是我这样做的方式,自从Django 1.4以来我一直在做,下面是在1.7.1中测试的:
在settings.py中......
添加到MIDDLEWEAR_CLASSES,locale,它可以根据请求选择语言:
'django.middleware.locale.LocaleMiddleware',
Run Code Online (Sandbox Code Playgroud)
添加LOCALE_PATHS,这是您的翻译文件的存储位置,也启用i18N:
USE_I18N = True
LOCALE_PATHS = (
os.path.join(PROJECT_PATH, 'locale/'),
)
Run Code Online (Sandbox Code Playgroud)
设置您将要将网站翻译为的语言:
ugettext = lambda s: s
LANGUAGES = (
('en', ugettext('English')),
('fr', ugettext('French')),
('pl', ugettext('Polish')),
)
Run Code Online (Sandbox Code Playgroud)
添加i18n模板上下文处理器,请求现在将包括LANGUAGES和LANGUAGE_CODE:
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.debug',
'django.core.context_processors.i18n', # this one
'django.core.context_processors.request',
'django.core.context_processors.static',
'django.contrib.messages.context_processors.messages',
)
Run Code Online (Sandbox Code Playgroud)
在urls.py中嵌套:
在url_patterns中,添加以下内容,它将启用设置语言重定向视图:
url(r'^i18n/', include('django.conf.urls.i18n')),
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅翻译中的其他内容.
添加以下导入,并使用i18n_patterns封装要翻译的URL.这是我的样子:
from django.conf.urls.i18n import i18n_patterns
from django.utils.translation import ugettext_lazy as _
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^i18n/', include('django.conf.urls.i18n')),
)
urlpatterns += i18n_patterns('',
(_(r'^dual-lang/'), include('duallang.urls')),
(r'^', include('home.urls')),
)
Run Code Online (Sandbox Code Playgroud)
注意:您也可以将管理员网址放入i18n_patterns.
现在你在任何地方使用文本并想要转换它,导入lazytext并用它包装每个字符串_('text'),你甚至可以转到你的其他urls.py文件并进行url翻译,如下所示:
url(_(r'^dual_language/$'), landing, name='duallang_landing'),
Run Code Online (Sandbox Code Playgroud)
您可以将要翻译的文本换行到其他文件中,例如models.py,views.py等.这是一个示例模型字段,其中包含label和help_text的翻译:
name = models.CharField(_('name'), max_length=255, unique=True, help_text=_("Name of the FAQ Topic"))
Run Code Online (Sandbox Code Playgroud)
Django翻译文档非常适合这个!
在你的HTML模板中......
现在,您可以进入模板并加载i18n模板标签,并对要翻译的静态内容使用trans和transblock.这是一个例子:
{% load i18n %}
{% trans "This is a translation" %}<br><br>
{% blocktrans with book_t='book title'|title author_t='an author'|title %}
This is {{ book_t }} by {{ author_t }}. Block trans is powerful!
{% endblocktrans %}
Run Code Online (Sandbox Code Playgroud)
现在为每个语言环境运行makemessages:
./manage.py makemessages -l pl
Run Code Online (Sandbox Code Playgroud)
现在剩下的就是进入你的/ locales文件夹,并编辑每个.po文件.填写每个msgstr的数据.这是一个这样的例子:
msgid "English"
msgstr "Angielski"
Run Code Online (Sandbox Code Playgroud)
最后编译消息:
./manage.py compilemessages
Run Code Online (Sandbox Code Playgroud)
翻译需要学习更多内容,国际化与此主题密切相关,因此请查看相关文档.我还建议查看一些Django可用的国际化软件包,如django-rosetta和django-linguo.它们有助于翻译模型内容,django-rosetta不会在您的数据库中为此创建新条目,而django-linguo会这样做.
如果你遵循这个,你应该有一个良好的开端.我相信这是让您的网站以多种语言运行的最标准化方式.干杯!
对于Mac来说,用户brew link gettext --force
可能会有风险,正如Brew建议的那样.更好的解决方法是PATH variable
为您的虚拟环境设置一个新的.因此,在postactivate
位于虚拟环境文件夹的bin文件夹中的文件中,键入:
export TEMP_PATH=$PATH
export PATH=$PATH:/usr/local/Cellar/gettext/0.19.7/bin
Run Code Online (Sandbox Code Playgroud)
请注意,您必须更换0.19.7
计算机中安装的版本.
并在您的predeactivate
文件中,它位于文件的同一文件夹中postactivate
,键入:
export PATH=$TEMP_PATH
unset TEMP_PATH
Run Code Online (Sandbox Code Playgroud)
现在你可以python manage.py makemessages -l <desired_language>
毫无后顾之忧地使用.:)
干杯.
对于macOS
:
brew install gettext
export PATH="/usr/local/opt/gettext/bin:$PATH"
归档时间: |
|
查看次数: |
42176 次 |
最近记录: |