django - how to make translation work?

前端 未结 9 1086
不知归路
不知归路 2020-12-13 04:00

I\'m trying to render a template in a different language using i18n. I did everything I could read about, from setting the language code, creating and compiling translation

相关标签:
9条回答
  • 2020-12-13 04:53

    Yes you do need to make message files as celopes suggests and then compile them

    python manage.py compilemessages
    

    But you will still have a problem.

    Disable LocaleMiddleware for a bit, i.e. remove this

    django.middleware.locale.LocaleMiddleware
    

    from your middleware list. Don't use it if you do not need to switch the language at run time, but if you do need it, then there is a solution. I had the same problem before and someone explained this to me.

    Also I had this weird issue before. Makemessages command would choke on strings wrapped with backslash in .py files.

    0 讨论(0)
  • 2020-12-13 04:55

    I may be wrong - as the only time I used translation stuff was on a test project many moons ago - but I think you don't want this:

    $ django-admin.py makemessages -l he-il -e html

    But rather this:

    $ django-admin.py makemessages -l he_il -e html

    Notice the underscore in he_il.

    I was having issues with pt-BR too, until I made the messages file with pt_br instead. Then things started working...

    Yeah, it is not obvious and I couldn't find documentation about it anywhere.

    Hope that helps.

    0 讨论(0)
  • 2020-12-13 04:57

    I had the same issues, it seems that your locale path have to end with a slash :

    LOCALE_PATHS = (
        '/dir/to/my/locale/', 
    )
    
    0 讨论(0)
提交回复
热议问题