在Django i18n中,语言环境路径不起作用

时间:2022-01-11 22:22:25

I want to make a django.po in the root of the project( contains all model and templates ) in my settings.py file.

我想在我的settings.py文件中在项目的根目录中创建一个django.po(包含所有模型和模板)。

LOCAL_PATHS = ('/path/to/project/locale/', )

LOCAL_PATHS =('/ path / to / project / locale /',)

but it doesn't work. . After makemessages and compilemessages,the po/mo file is generated successfully,but nothing happens when I change the language settings(The modeltranslation works well). So I think maybe the locale directory cannot be recognized in the project root . Here is my project structure:

但它不起作用。 。在makemessages和compilemessages之后,po / mo文件成功生成,但是当我更改语言设置时没有任何反应(模型转换效果很好)。所以我认为可能无法在项目根目录中识别语言环境目录。这是我的项目结构:

project
  -app/
  -app/
  -project/
      -settings.py
      -urls.py
  -templates/
  -static/
  -locale/

then i put the locale directory under an app directory and use the makemessages/compilemessages tool, it works. But it only contains the translation which marked in this app, which means I can't make the translations that marked in templates or other apps.

然后我将locale目录放在app目录下并使用makemessages / compilemessages工具,它可以工作。但它只包含在此应用程序中标记的翻译,这意味着我无法制作在模板或其他应用程序中标记的翻译。

Is there any better solution for this situation?

对于这种情况有没有更好的解决方案?

1 个解决方案

#1


3  

Here how I implemented:

这是我实施的方式:

I kept locale folder like this in my project:

我在我的项目中保留了这样的locale文件夹:

-Project  #*main Project directory
  -apps
  -apps
  -templates
  -project
     -settings.py
  -locale              #outside project folder but within main Project
     -ru_RU
       -LC_MESSAGES
          -django.po

Now I gave the locale path in settings.py like:

现在我在settings.py中给出了locale路径,如:

PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))

 LOCALE_PATHS = (
    os.path.join(PROJECT_PATH, '../locale'),

)

Language choices:

LANGUAGES = (
    ('en-us', 'English'),
    ('ru_RU', 'Russian'),
)


LANGUAGE_CODE = 'en-us' 'ru_RU' 

Finally added the middleware:

最后添加了中间件:

django.middleware.locale.LocaleMiddleware

Hopefully it will help. I wrote about it in details at my blog: http://ruddra.com/2015/09/17/django-translation-using-po-file/ .

希望它会有所帮助。我在博客上详细介绍了它:http://ruddra.com/2015/09/17/django-translation-using-po-file/。

#1


3  

Here how I implemented:

这是我实施的方式:

I kept locale folder like this in my project:

我在我的项目中保留了这样的locale文件夹:

-Project  #*main Project directory
  -apps
  -apps
  -templates
  -project
     -settings.py
  -locale              #outside project folder but within main Project
     -ru_RU
       -LC_MESSAGES
          -django.po

Now I gave the locale path in settings.py like:

现在我在settings.py中给出了locale路径,如:

PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))

 LOCALE_PATHS = (
    os.path.join(PROJECT_PATH, '../locale'),

)

Language choices:

LANGUAGES = (
    ('en-us', 'English'),
    ('ru_RU', 'Russian'),
)


LANGUAGE_CODE = 'en-us' 'ru_RU' 

Finally added the middleware:

最后添加了中间件:

django.middleware.locale.LocaleMiddleware

Hopefully it will help. I wrote about it in details at my blog: http://ruddra.com/2015/09/17/django-translation-using-po-file/ .

希望它会有所帮助。我在博客上详细介绍了它:http://ruddra.com/2015/09/17/django-translation-using-po-file/。