简体   繁体   English

Django 不创建翻译 .po 文件

[英]Django doesn't create translation .po files

I have my translation strings only in templates (stored in the project_dir/Templates), I tried running the $ django-admin.py createmessages -l ru both in the project root directory and in the app directories that use templates with trans.我的翻译字符串仅在模板中(存储在 project_dir/Templates 中),我尝试在项目根目录和使用模板和 trans 的应用程序目录中运行$ django-admin.py createmessages -l ru strings.字符串。 It created folders locale/ru/LC_MESSAGES but the folders were empty.它创建了文件夹 locale/ru/LC_MESSAGES 但文件夹是空的。 I tried to add the django.po files manually (with the syntax mentioned in the l10n docs).我尝试手动添加 django.po 文件(使用l10n文档中提到的语法)。 And ran the createmessages -a and compilemessages commands.并运行 createmessages -a 和 compilemessages 命令。 It created the .mo files, but the translations didn't appear in the browser.它创建了 .mo 文件,但翻译没有出现在浏览器中。

  1. As I created the .po files manually I had no lines starting with #.当我手动创建 .po 文件时,我没有以 # 开头的行。 What should I write there?我应该在那里写什么?
  2. My template files are in different folder than the .py files for apps.我的模板文件与应用程序的 .py 文件位于不同的文件夹中。 Should I put some extra links to them?我应该为他们添加一些额外的链接吗?

did you try :你试过了吗 :

python manage.py makemessages -a 

from project root and app ?从项目根和应用程序?

this should create a .po that you have to edit.这应该会创建一个您必须编辑的 .po。 be sure to remove 'fuzzy' stuff everywhere.一定要删除到处都是“模糊”的东西。

then :然后 :

python manage.py compilemessages

You need to restart the server您需要重新启动服务器

For newer versions of Django (eg 3.2):对于较新版本的 Django(例如 3.2):

  1. in the root directory create folder "locale"在根目录中创建文件夹“locale”

  2. run command django-admin makemessages -l ru运行命令django-admin makemessages -l ru

  3. update your language files (located in the locale folder)更新您的语言文件(位于语言环境文件夹中)

  4. run django-admin compilemessages运行django-admin compilemessages

  5. Configure the LOCALE_PATHS in settings.py, otherwise you won't see the translations:在settings.py中配置LOCALE_PATHS,否则看不到翻译:

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

    LANGUAGE_CODE = 'ru'

To fix empty po files:修复空的 po 文件:

  1. Make sure you did the needed changes for the templates as mentioned in the documentation .确保您对文档中提到的模板进行了必要的更改。 Please make sure that you are checking the correct documentation version for your project.请确保您正在检查项目的正确文档版本。
  2. You can add a locale directory in your templates directory and add its path to the LOCALE_PATHS list.您可以在templates目录中添加locale目录并将其路径添加到LOCALE_PATHS列表。 (Optional, but helpful to make sure that the template directory is included in step 4) (可选,但有助于确保模板目录包含在步骤 4 中)
  3. Go to the project_dir (you should run the next command in a parent directory of the files to be translated)转到 project_dir(您应该在要翻译的文件的父目录中运行下一个命令)
  4. Run the command django-admin makemessages -l ru运行命令django-admin makemessages -l ru

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM