Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django i18n setlang不更改会话数据Django_语言_Python_Django_Internationalization - Fatal编程技术网

Python Django i18n setlang不更改会话数据Django_语言

Python Django i18n setlang不更改会话数据Django_语言,python,django,internationalization,Python,Django,Internationalization,我不知道我做错了什么,但即使我向/i18n/setlang提交新语言,我的会话数据也不会改变。我要用代码“tl”翻译成菲律宾语,但不知怎么的,它似乎不起作用。请帮忙。以下是一些代码: # Django settings for ppdjango project. import os DEBUG = True TEMPLATE_DEBUG = DEBUG ADMINS = ( # ('Your Name', 'your_email@domain.com'), ) MANAGERS =

我不知道我做错了什么,但即使我向/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 = ''
编辑我的语言运行良好,因为如果我将“tl”作为设置中的唯一语言,页面将被翻译为塔加洛语

编辑包括整个设置文件

尝试添加
语言代码='en-us'

这不是缓存问题吗?
尝试删除缓存以进行测试。

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

Django通过 遵循此算法:

  • 首先,它在应用程序目录中查找区域设置目录 正在调用的视图。如果是 查找所选文档的翻译 语言,翻译将是 安装
  • 接下来,它在项目目录中查找区域设置目录。如果 它找到了一个翻译,即 翻译将被安装
  • 最后,它检查Django在中提供的基本翻译 django/conf/locale
菲律宾语不是django支持的语言之一,因此您需要自己添加它。这是我从djangobook.com上得到的一些东西

LocaleMiddle软件只能选择 有特定语言的语言 Django提供基本翻译。如果 您希望为提供翻译 您的应用程序尚未启动 在Django的翻译集中 源代码树,您将希望在 最基本的翻译 语言。例如,Django使用 要翻译的技术消息ID 日期格式和时间格式-所以你 至少需要这些翻译 使系统正常工作

print request.LANGUAGE_CODE
一个好的起点是复制 英语.po文件,并在 至少是技术信息——也许吧 验证消息也是如此

技术消息ID易于识别 辨识;它们都是大写的。 您不能将消息ID翻译为 对于其他消息,您提供 在所提供的计算机上更正本地变量 英语价值观。例如,与 日期时间格式(或日期格式或 时间(U格式),这将是格式 要在应用程序中使用的字符串 语言。格式与 用户现在使用的格式字符串 模板标签


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

我会检查您的应用程序中是否使用了django.utils.translation的停用或激活方法,因为我曾经因此遇到过麻烦


我还建议您在尝试更改语言时,与pdb一起查看django.views.i18n set_language view中发生的情况。

我对此进行了一些调查,以下是一些需要检查的内容:

1-在您的/friend/invite/associated视图中添加此行,以确保set_lang视图正常工作

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

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


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

如果我理解正确,将LANGUAGE_CODE设置为tl可以让您以菲律宾语加载站点。如果不是,您可能在语言环境设置方面存在一些配置问题

为了帮助定位问题,我建议执行以下操作:

更改语言时,使语言设置窗体指向自身。通过这种方式,我们可以在点击之间进行非常重要的比较。这里我假设语言设置表单驻留在书签url中

<input name="next" type="hidden" value="/bookmarks/" />
您还可以将另一种语言(如fr)添加到语言设置中,以查看django提供的语言是否有效


直接加载语言设置页面将允许您更改语言,然后比较更改的标题(如果需要,您可以使用另一个标记)。使用此方法和您提供的信息,语言开关在我的设置中起作用。

可能您正在使用ugettext,而您应该使用ugettext\u。请参阅。

TEMPLATE_CONTEXT_PROCESSORS=(“django.core.CONTEXT_PROCESSORS.i18n”),您是否已将此添加到settings.pyyes中。我想我必须更新它以包含我的整个设置文件问题甚至在我添加缓存中间件之前就出现了:)谢谢,但是我已经有了用python manage.py makemessages创建的可翻译字符串的菲律宾翻译。它位于一个名为locale的文件夹中,代码为“tl”,我想提示一下,中间件的顺序可能很重要。我需要将SessionMiddle放在LocaleMiddle之前,这听起来很有道理:“django.contrib.sessions.middleware.SessionMiddleware”、“django.middleware.locale.L”
<input name="next" type="hidden" value="/bookmarks/" />
<title>{% trans "Delete" %}</title>
MIDDLEWARE_CLASSES = (
    'django.middleware.cache.UpdateCacheMiddleware',
+    'django.middleware.locale.LocaleMiddleware', # you need this to activate language (code)
    'django.middleware.common.CommonMiddleware',
    ...
)