django i18n:确保你有GNU gettext工具

时间:2014-12-01 00:28:20

标签: python django

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

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

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

enter image description here

11 个答案:

答案 0 :(得分:31)

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

  1. 安装Homebrew/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  2. 安装GNU gettext:brew install gettext
  3. 您不应该在步骤3中使用brew link gettext --force,因为它有风险(如果您尝试Brew建议)。更好的解决方法是为您的虚拟环境设置新的PATH variable。因此,在位于虚拟环境文件夹的bin文件夹中的postactivate文件中,键入:

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

    请注意,您必须使用计算机中安装的版本替换0.19.7

    predeactivate文件中,该文件位于postactivate文件的同一文件夹中,请输入:

    export PATH=$TEMP_PATH
    unset TEMP_PATH
    

    现在您可以毫无后顾之忧地使用python manage.py makemessages -l <desired_language>了。 :)

    干杯。

答案 1 :(得分:14)

在Ubuntu中:

$ sudo apt-get install gettext

答案 2 :(得分:11)

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

使用终端

安装带有Homebrew的gettext GNU工具
  1. 安装Homebrew/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

答案 3 :(得分:7)

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

您需要下载两个文件夹:

  • 的gettext-runtime_0.18.1.1-2_win32
  • gettext的工具-dev_0.18.1.1-2_win32

您可以找到它们here

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

  • 您还需要一个名为 libstdc ++ - 6.dll 的文件从here下载并将其放在您的系统目录中。您将在系统目录here找到足够的详细信息。

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

答案 4 :(得分:3)

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

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

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

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

答案 5 :(得分:2)

@max-malyshanswer为我解决了这个问题 - 没有触及系统文件。

复制并运行以下各项:

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"将gettext_path存储在shell变量中 - 根据brew install gettext
  2. 添加版本号
  3. FILE="venv/bin/activate"存储venv shell脚本的路径
  4. echo "" >> $FILE在末尾添加一个空行以确保下一个命令在其自己的行上
  5. echo "export PATH=\$PATH:$GETTEXT_PATH" >> $FILE向venv shell脚本添加命令;此命令将gettext二进制文件的路径添加到全局$PATH变量,以便在OS二进制文件之前使用它们。
  6. source venv/bin/activate运行venv shell脚本,以便正确设置变量。您可以多次运行此命令。

答案 6 :(得分:1)

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

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

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

请务必使用0.19.8.1版本替换gettext

这会将$fish_user_paths设置为通用变量。这是help关于通用变量的说法:

  

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

创建变量

因此,在shell中设置此变量一次(无需在配置文件中执行)即使在注销或重新启动后也会保存它。

答案 7 :(得分:0)

问题在brew ...

的输出中暗示

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

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

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

答案 8 :(得分:0)

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.

If your env folder is in your root folder

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

<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_venv to the name of your virtual environment.

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

processing locale zh_CN

Now you should have a locale folder like this: 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.

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

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

答案 9 :(得分:0)

如果您使用的是Docker,只需在以下命令中运行:

    SocketAcceptor serverAcceptor = (setup, sendingSocket) ->
        Mono.error(new RuntimeException("Setup rejected"));

    RSocketServer.create(serverAcceptor)
        .bind(TcpServerTransport.create("localhost", 7000))
        .subscribe();

    SocketAcceptor clientAcceptor = (setup, sendingSocket) -> {
      logger.debug("Accepted");
      return Mono.just(new RSocket() {});
    };

    RSocket socket = RSocketConnector.create()
        .acceptor(clientAcceptor)
        .connect(TcpClientTransport.create("localhost", 7000)).block();

然后:

apt-get update

答案 10 :(得分:0)

适用于Windows用户。 我正在使用Django 2

  1. 访问此https://mlocati.github.io/articles/gettext-iconv-windows.html

2为您的系统下载静态版本

  1. 下载后执行设置。

  2. 重新启动您的PC,它将正常工作。

仅此而已。谢谢!!!!!!!