Python django i18n:确保你有 GNU gettext 工具

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27220052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:32:11  来源:igfitidea点击:

django i18n: Make sure you have GNU gettext tools

pythondjango

提问by user2492364

I try django-admin.py makemessages -l zh_CNbut has error :

我尝试django-admin.py makemessages -l zh_CN但有错误:

CommandError: Can't find msguniq. Make sure you have GNU gettext tools 0.15 or newer installed.

after I use brew install gettext,it still get wrong.
Do I need to do something? here is my terminal screenshot
Please guide me thank you.

在我使用 brew install gettext 后,它仍然出错。
我需要做些什么吗?这是我的终端截图
请指导我谢谢。

enter image description here

在此处输入图片说明

回答by Anentropic

the problem is hinted in the output from brew...

问题在brew...的输出中有所暗示

it has installed the GNU gettext but hasn't linked it into your bin directory because OSX already provides a different version of gettext...

它已经安装了 GNU gettext 但没有将它链接到你的 bin 目录中,因为 OSX 已经提供了不同版本的 gettext ...

so Django doesn't know to run the version you installed from brew.

所以 Django 不知道运行你从 brew 安装的版本。

apparently brew is too cautious here though and you should just link it https://stackoverflow.com/a/9787791/202168

显然 brew 在这里太谨慎了,你应该链接它https://stackoverflow.com/a/9787791/202168

回答by Louis Barranqueiro

This procedure worked for me (OSX 10.11.2 - python v3.5 and Django 1.8)It should work with your configuration.

此过程对我有用(OSX 10.11.2 - python v3.5 和 Django 1.8)它应该适用于您的配置。

Install gettext GNU tools with Homebrew using Terminal

使用终端安装带有 Homebrew 的 gettext GNU 工具

  1. Install Homebrew: /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  2. Install GNU gettext : brew install gettext
  3. Create symlink : brew link gettext --force
  1. 安装自制软件/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  2. 安装 GNU gettext : brew install gettext
  3. 创建符号链接: brew link gettext --force

回答by reinaldoluckman

For Mac users, after installing Homebrew and gettext as @Louis Barranqueiro says (steps 1 and 2):

对于 Mac 用户,在安装 Homebrew 和 gettext 之后,@Louis Barranqueiro 说(步骤 1 和 2):

  1. Install Homebrew: /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  2. Install GNU gettext : brew install gettext
  1. 安装自制软件/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  2. 安装 GNU gettext : brew install gettext

You shouldn't use brew link gettext --forcein step 3, because it is risky (as Brew advises if you try). A better workaround is to set a new PATH variablefor your virtual environment. So, in the postactivatefile, which is located in the bin folder of your virtual environment folder, type:

您不应该brew link gettext --force在第 3 步中使用,因为它有风险(如果您尝试,Brew 会建议您这样做)。更好的解决方法是PATH variable为您的虚拟环境设置一个新的。因此,在postactivate位于虚拟环境文件夹的 bin 文件夹中的文件中,键入:

export TEMP_PATH=$PATH
export PATH=$PATH:/usr/local/Cellar/gettext/0.19.7/bin

Note that you have to replace 0.19.7by the version that is installed in your machine.

请注意,您必须替换0.19.7为您机器中安装的版本。

And in your predeactivatefile, which is located in the same folder of postactivatefile, type:

predeactivate位于同一文件postactivate文件夹中的文件中,键入:

export PATH=$TEMP_PATH
unset TEMP_PATH

Now you can use the python manage.py makemessages -l <desired_language>without worries. :)

现在您可以python manage.py makemessages -l <desired_language>放心使用。:)

Cheers.

干杯。

回答by Joe Cheng

In Ubuntu:

在 Ubuntu 中:

$ sudo apt-get install gettext

回答by Gabriel

If you use fish shell, another way around is to add this path to $fish_user_paths.This variable is prepended to $PATH, so you don't have to set it in all your projects.

如果您使用fish shell,另一种方法是将此路径添加到$fish_user_paths.This 变量之前添加到$PATH,因此您不必在所有项目中设置它。

You can do it with the following command line :

您可以使用以下命令行执行此操作:

set -U fish_user_paths /usr/local/Cellar/gettext/0.19.8.1/bin $fish_user_paths

Remember to replace 0.19.8.1with your gettextversion.

记得0.19.8.1用你的gettext版本替换。

This sets $fish_user_pathsas a Universal Variable. Here's what helpsays about Universal Variables :

这设置$fish_user_paths为通用变量。以下是help关于通用变量的说明:

A universal variable is a variable whose value is shared across all instances of fish, now and in the future – even after a reboot. You can make a variable universal with set -U

通用变量是一个变量,其值在现在和将来的所有 fish 实例中共享 - 即使在重新启动后也是如此。您可以使变量通用set -U

So setting this variable in your shell once (no need to do it in a config file) will save it even after logging out or rebooting.

所以在你的 shell 中设置一次这个变量(不需要在配置文件中设置)即使在注销或重新启动后也会保存它。

回答by Far

This solution worked for me ( win. 7, 8 and 10 )

这个解决方案对我有用( win. 7, 8 和 10 )

You need to download two folders:

您需要下载两个文件夹:

  • gettext-runtime_0.18.1.1-2_win32
  • gettext-tools-dev_0.18.1.1-2_win32
  • gettext-runtime_0.18.1.1-2_win32
  • gettext-tools-dev_0.18.1.1-2_win32

You can find them here.

您可以在此处找到它们。

  • After you download them, unzip them and add the directory of the bin file of the both folders to the system variables PATHof your pc.

  • You will also need a file named libstdc++-6.dlldownload it from hereand place it in your system directory. You will find adequate details on system directory here.

  • 下载后解压,将两个文件夹的bin文件目录添加到PATH电脑的系统变量中。

  • 您还需要一个名为libstdc++-6.dll 的文件,从这里下载并将它放在您的系统目录中。您可以在此处找到有关系统目录的足够详细信息。

And that's it. Hope it is useful for you.

就是这样。希望对你有用。

回答by Goulven

@max-malysh's answersolved it for me —without touching system files.

@max-malysh回答为我解决了这个问题——不涉及系统文件。

Copy and run each of the following:

复制并运行以下各项:

brew install gettext
GETTEXT_PATH="/usr/local/Cellar/gettext/0.19.8.1/bin"
FILE="venv/bin/activate"
echo "" >> $FILE
echo "export PATH=$PATH:$GETTEXT_PATH" >> $FILE
source venv/bin/activate
  1. GETTEXT_PATH="/usr/local/Cellar/gettext/0.19.8.1/bin"stores gettext_path in a shell variable —adapt the version number according to what brew install gettext
  2. FILE="venv/bin/activate"stores the path to the venv shell script
  3. echo "" >> $FILEadds an empty line at the end of the to make sure the next command is on its own line
  4. echo "export PATH=\$PATH:$GETTEXT_PATH" >> $FILEadds a command to the venv shell script; this command adds the path to gettext binaries to the global $PATHvariable, so that they are used before OS binaries.
  5. source venv/bin/activateruns the venv shell script so that variables are properly set. You can run this command more than once.
  1. GETTEXT_PATH="/usr/local/Cellar/gettext/0.19.8.1/bin"将 gettext_path 存储在一个 shell 变量中——根据什么调整版本号 brew install gettext
  2. FILE="venv/bin/activate"存储 venv shell 脚本的路径
  3. echo "" >> $FILE在末尾添加一个空行以确保下一个命令在它自己的行上
  4. echo "export PATH=\$PATH:$GETTEXT_PATH" >> $FILE向 venv shell 脚本添加命令;此命令将 gettext 二进制文件的路径添加到全局$PATH变量中,以便在操作系统二进制文件之前使用它们。
  5. source venv/bin/activate运行 venv shell 脚本,以便正确设置变量。您可以多次运行此命令。

回答by Elias Glyptis

Hi first of all make sure that your virtual environment is not in your root folder. I think it's better practice to keep your virtual environment outside of the root folder. Obviously make sure your environment is activated. Of course make sure you have gettext installed as well.

嗨,首先确保您的虚拟环境不在您的根文件夹中。我认为将虚拟环境保留在根文件夹之外是更好的做法。显然,请确保您的环境已激活。当然,请确保您也安装了 gettext。

If your env folder is in your root folder

如果您的 env 文件夹在您的根文件夹中

To test this just make sure you add {% load i18n %} in all your templates, choose a template and do something like this:

要对此进行测试,请确保在所有模板中添加 {% load i18n %},选择一个模板并执行以下操作:

<h1>{% trans 'My Test to be translated' %}</h1>

Now run this command

现在运行这个命令

django-admin makemessages -l 'zh_CN' -i your_venv

(Make sure you replace your_venvto the name of your virtual environment.

(确保替换your_venv为虚拟环境的名称。

After you run the above command, you should get this in your terminal.

运行上述命令后,您应该在终端中获得它。

processing locale zh_CN

处理语言环境zh_CN

Now you should have a locale folder like this: locale/cn/LC_MESSAGES/django.po

现在你应该有一个像这样的语言环境文件夹: locale/cn/LC_MESSAGES/django.po

Now you will need to compile the messages. Run this command

现在您需要编译消息。运行这个命令

django-admin compilemessages

In your locale folder, now you should get you should see django.mo file as well, but you will notice the difference in django.po file. Just add your translation there, and you can test again by setting your en language to LANGUAGE_CODE = 'zh_CN' then just refresh and test the h1 string will be translated to Chinese.

在您的语言环境文件夹中,现在您应该也应该看到 django.mo 文件,但您会注意到 django.po 文件中的差异。只需在那里添加您的翻译,您就可以通过将您的 en 语言设置为 LANGUAGE_CODE = 'zh_CN' 再次测试,然后只需刷新并测试 h1 字符串将被翻译成中文。

In order for the above to work in your settings.py ensure you have this here, for now most important is the LOCALE_PATHS, but please check if this ('zh_CN', _('Chinese')),is correct

为了使上述内容在你的 settings.py 中工作,确保你在这里有这个,现在最重要的是 LOCALE_PATHS,但请检查这个('zh_CN', _('Chinese')),是否正确

LANGUAGES = [
   ('zh_CN', _('Chinese')),
   ('en', _('English')),
]

LANGUAGE_CODE = 'en-us'


TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

LOCALE_PATHS = [
    os.path.join(BASE_DIR, 'locale'),
]

In this reply, the most important part is to realize where your virtual environment is located. Reason why you get all these errors.

在这个回复中,最重要的部分是了解您的虚拟环境位于何处。出现所有这些错误的原因。

Please make sure you refer to this video here, it's a great tutorial. https://www.youtube.com/watch?v=xI97sLMd1rM

请确保您在此处参考此视频,这是一个很棒的教程。 https://www.youtube.com/watch?v=xI97sLMd1rM

回答by Yuksel CELIK

Just below solution solved my problem. I am using Windows 10 64bit

下面的解决方案解决了我的问题。我使用的是 Windows 10 64 位

1- Go to this link : https://mlocati.github.io/articles/gettext-iconv-windows.html

1- 转到此链接:https: //mlocati.github.io/articles/gettext-iconv-windows.html

2- Download 32 or 64 bit sharedand staticwindows installation files

2- 下载 32 位或 64 位共享静态Windows 安装文件

3-Install both of files
4-Restart your computer

3-安装两个文件
4-重新启动计算机