Django i18n 设置语言未更改会话数据 django_language

2024-05-20 08:36:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我不知道我做错了什么,但是即使我向/i18n/setlang提交了一种新语言,会话数据也不会改变。我要用代码“tl”翻译成菲律宾人的语言,但不知怎么的,它似乎不起作用。请帮忙。下面是一些代码:

# Django settings for ppdjango project.
import os

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
    # ('Your Name', 'your_email@domain.com'),
)

MANAGERS = ADMINS

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3', 
        'NAME': 'bookmarksdb',                      
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# On Unix systems, a value of None will cause Django to use the same
# timezone as the operating system.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'America/Chicago'

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
# LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'en-us'

LANGUAGES = (
    ('tl', 'Filipino'),
    ('en', 'English'),
)

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale
USE_L10N = True

# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = ''

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = ''

# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/media/'

# Make this unique, and don't share it with anybody.
SECRET_KEY = '35qqpsggj&v0^!rdabnr7daj(#gu2252hj4&8qw1k6gb@5r)qa'

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.cache.UpdateCacheMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.cache.FetchFromCacheMiddleware'
)

ROOT_URLCONF = 'ppdjango.urls'

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(os.path.dirname(__file__), 'templates')
)

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

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.comments',
    'bookmarks',

    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
)

LOGIN_URL = '/login/'
CACHE_BACKEND = 'db://cache_table'
CACHE_MIDDLEWARE_SECONDS = 60 * 5

#email
SITE_HOST = '127.0.0.1:8000'
DEFAULT_FROM_EMAIL = 'Django Bookmarks <django.bookmarks@example.com>'
EMAIL_HOST = 'mail.ygamretuta.com'
EMAIL_PORT = ''
EMAIL_HOST_USER = 'dev@ygamretuta.com'
EMAIL_HOST_PASSWORD = ''

我的语言设置窗体: {

{% load i18n %}

<!DOCTYPE html>
<html>
  <head>
    <title>
    Django Bookmarks | {% block title %}{% endblock %}
    </title>
    <link rel="stylesheet" type="text/css" href="/site_media/style.css">
    <script type="text/javascript" src="/site_media/jquery.js"></script>
    {% block external %}
      <script type="text/javascript" src="/site_media/search.js"></script>
    {% endblock %}
  </head>

  <body>
  {% block content %}{% endblock %}

    <div id="footer">
      <form action="/i18n/setlang/" method="post">
        {% csrf_token %}
        <input name="next" type="hidden" value="/friend/invite/" />
        <select name="language"> 
          {% for lang in LANGUAGES %}
            <option value="{{ lang.0 }}">{{ lang.1 }}</option>
          {% endfor %}
        </select>
        <input type="submit" value="Switch Language"/>
      </form>
    </div>
  </body>
</html>

我的url设置:

# the rest is above
(r'^i18n/', include('django.conf.urls.i18n')),

编辑我的语言运行良好,因为如果我将“tl”作为设置中的唯一语言,则该页将被翻译为Tagalog

编辑包括整个设置文件


Tags: thetodjangocom语言hostforwith
3条回答

我已经调查过一点了,这里有一些事情要检查:

1-在/friend/invite/associated视图中添加这一行,以确保set_lang视图正常工作。

print request.LANGUAGE_CODE

如果它不输出“tl”,可能是因为您的语言缺少django语言环境文件,因为它们在您的设置文件中。这些文件应该位于django/conf/locale/tl下。其中有多个文件,这些文件不会由makemessages命令生成。

2-如果它确实输出了“tl”,那就意味着您在django代码树中有一个tl文件夹。然后我会先尝试使用支持的语言。当我试图让你的例子起作用时,我也没能得到塔加洛。通过切换到另一种受支持的语言,在我的例子中是法语,我已经能够解决我的问题。

3-将模板设置为render/friend/invite/view会有帮助,这样我们就可以看到您正在翻译的内容。在我的测试中,我使用了django字符串来避免生成自己的消息。我照文件上说的做了,并从英语中抄来用在塔加洛语中。然后我修改了其中一根弦。问题是,django使用从文本生成的二进制文件,因此即使在tagalog下,仅修改文本版本仍会以英语显示django字符串。我发现,当我不使用英语作为基础时,我使用法语版本放在tagalog下。

您是否为菲律宾人创建了翻译并将其放在项目中的正确位置?如果是,你把它放哪儿了?

Django looks for translations by following this algorithm:

  • First, it looks for a locale directory in the application directory of the view that’s being called. If it finds a translation for the selected language, the translation will be installed.
  • Next, it looks for a locale directory in the project directory. If it finds a translation, the translation will be installed.
  • Finally, it checks the Django-provided base translation in django/conf/locale.

菲律宾语不是django支持的语言之一,所以您需要自己添加它。这是我从djangook.com上得到的

http://www.djangobook.com/en/2.0/chapter19/

The LocaleMiddleware can only select languages for which there is a Django-provided base translation. If you want to provide translations for your application that aren’t already in the set of translations in Django’s source tree, you’ll want to provide at least basic translations for that language. For example, Django uses technical message IDs to translate date formats and time formats — so you will need at least those translations for the system to work correctly.

A good starting point is to copy the English .po file and to translate at least the technical messages — maybe the validation messages, too.

Technical message IDs are easily recognized; they’re all upper case. You don’t translate the message ID as with other messages, you provide the correct local variant on the provided English value. For example, with DATETIME_FORMAT (or DATE_FORMAT or TIME_FORMAT), this would be the format string that you want to use in your language. The format is identical to the format strings used by the now template tag.

我不确定这是否能解决你的问题,但希望它能帮助你达到目的。

MIDDLEWARE_CLASSES = (
    'django.middleware.cache.UpdateCacheMiddleware',
+    'django.middleware.locale.LocaleMiddleware', # you need this to activate language (code)
    'django.middleware.common.CommonMiddleware',
    ...
)

相关问题 更多 >