简体   繁体   English

更改 django 中特定语言代码的一些默认单词

[英]Change some default words for a specific language code in django

I'm trying to change some words that are poorly translated into Persian.我正在尝试更改一些很难翻译成波斯语的单词。 I've searched this issue but I couldn't find any article about it.我已经搜索了这个问题,但我找不到任何关于它的文章。 I've only changed the LANGUAGE_CODE, and I don't want a website for both languages我只更改了 LANGUAGE_CODE,我不想要两种语言的网站

You can create a locale directory in your project or app.您可以在项目或应用程序中创建语言环境目录。

Then add the directory to LOCALE_PATHS ( Django Docs ) in settings.py :然后将目录添加到settings.py中的LOCALE_PATHS ( Django Docs ):

import os
  
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
PROJECT_APP_PATH = os.path.dirname(os.path.abspath(__file__))
PROJECT_APP = os.path.basename(PROJECT_APP_PATH)
BASE_DIR = os.path.dirname(PROJECT_APP_PATH)

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

Then run command makemessages :然后运行命令makemessages

python manage.py makemessages -l fa

It creates (or updates) a message file in the directory locale/fa/LC_MESSAGES .它在locale/fa/LC_MESSAGES目录中创建(或更新)一个消息文件。 In the de example, the file will be locale/fa/LC_MESSAGES/django.po .在 de 示例中,该文件将是locale/fa/LC_MESSAGES/django.po

Now you can edit the translations for whichever strings you like.现在您可以编辑您喜欢的任何字符串的翻译。

For example: open locale/fa/LC_MESSAGES/django.po and add desired strings :例如:打开locale/fa/LC_MESSAGES/django.po并添加所需的字符串

msgid "No fields changed."
msgstr "فیلدی تغییر نیافته است." # Override it to desired translation

msgid "None"
msgstr "هیچ" # Override it to desired translation

After you create your message file – and each time you make changes to it – you'll need to compile it into a more efficient form, for use by gettext .创建消息文件后——并且每次对它进行更改——你需要将它编译成更有效的形式,以供gettext使用。 Do this with the utility:使用实用程序执行此操作:

python manage.py compilemessages

This command runs over all available .po files and creates .mo files, which are binary files optimized for use by gettext .此命令运行所有可用的.po文件并创建.mo文件,这些文件是为gettext使用而优化的二进制文件。

For more info look at Localization: how to create language files有关更多信息,请查看本地化:如何创建语言文件

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

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