Issues with translating the strings

自古美人都是妖i 提交于 2019-12-07 19:12:20

问题


I am using multiple translation in my project

For that I have updated my settings file as

    LANGUAGE_CODE = 'en-us'

    gettext = lambda s: s


    LANGUAGES = (

        ('es', gettext('Spanish')),
        ('en', gettext('English')),
    )


    LOCALE_PATHS = (
        '/mnt/aviesta/pythondev/django/locale',
    )

    USE_I18N = True
MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.locale.LocaleMiddleware',


TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
    "django.core.context_processors.request",
)

And my template file as:

{% load i18n %}

{% trans "Hello" %}


<p>Already a user <a href="/login/"><b>{% trans "login here" %}</b></a></p>

after that I create a locale folder parallel to my app and than create the specific language folders in it as :django-admin.py makemessages -l es which creates the .po file and then update this .po file as:

#: customer_reg/customer_register.html:14
msgid "Hello"
msgstr "¡Hola"

#: customer_reg/customer_register.html:17
msgid "login here"
msgstr "ingresa aquí"

And finally i compiled my msg django-admin.py compilemessages

BUt my strings "hello" and "login here" is remain in English, they are not translated.I don't know why it happens ??


回答1:


Everything looks great with your code. The only thing I've never seen is the LOCALE_PATHS in settings.py

Mabye this answer, I did some time ago, could help you: Issues with multiple languages

Edit // Answer to comment

from django.utils import translation
translation.activate('es')


来源:https://stackoverflow.com/questions/12907870/issues-with-translating-the-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!