【问题标题】:when i add new language to django, there was no error but dosnt show my translations. why?当我向 django 添加新语言时,没有错误但没有显示我的交易。为什么?
【发布时间】:2016-02-02 08:45:33
【问题描述】:

我的设置文件是:

我为 kk-ar、kk-kz、kk-latn 语言创建消息文件以进行翻译。以及不同书写系统中相同语言的这些语言代码。但这不支持 django 设置,所以我将它添加到设置中,但是当我运行服务器时,django 不显示我的翻译。

这是为什么?

由 'django-admin startproject' 使用 Django 1.8.5 和 Python 生成 3.4

gettext = lambda s: s

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

DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'task.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, "templates")],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'django.template.context_processors.i18n',
            ],
        },
    },
]

WSGI_APPLICATION = 'task.wsgi.application'


# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/


LANGUAGES = (
    ('kk-kz', gettext('Kazakh')),
    ('kk-latn', gettext('Kazakh Latin')),
    ('kk-ar', gettext('Kazakh Arab')),
)

EXTRA_LANG_INFO = {
    'kk-ar': {
        'bidi': True,  # right-to-left
        'code': 'kk-ar',
        'name': 'Kazakh Arab',
        # unicode codepoints here
        'name_local': u'\u0642\u0627\u0632\u0627\u0642\u0634\u0627',
    },
    'kk-latn': {
        'bidi': False,  # right-to-left
        'code': 'kk-latn',
        'name': 'Kazakh Latin',
        # unicode codepoints here
        'name_local': u'Qazaq',
    },
}

# Add custom languages not provided by Django
laninfo = django.conf.locale.LANG_INFO
laninfo.update(EXTRA_LANG_INFO)
django.conf.locale.LANG_INFO = laninfo

# Languages using BiDi (right-to-left) layout
LANGUAGES_BIDI = global_settings.LANGUAGES_BIDI + ("kk-ar",)

LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale'),
)

LANGUAGE_CODE = 'kk-ar'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = False

USE_TZ = True

我的 url.py 是这样的: 从 django.conf.urls 导入包含,url

from django.contrib import admin
from django.conf.urls.i18n import i18n_patterns
from .views import home, home_files

urlpatterns = [
    url(r'^(?P<filename>(robots.txt)|(humans.txt))$',
        home_files, name='home_files'),
]
urlpatterns += i18n_patterns(
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', home, name='home'),    
    url(r'^i18n/', include('django.conf.urls.i18n')),
)

【问题讨论】:

  • 你编译文件了吗?你把它们放在哪里了?

标签: python django


【解决方案1】:

在 Django 中进行翻译工作的三个步骤:

  • 将字符串标记为可翻译
  • 创建 PO 文件
  • 将 PO 文件编译为 MO 文件

这里的本地化指南:https://docs.djangoproject.com/en/1.8/topics/i18n/translation/#how-to-create-language-files 有一个很好的分步指南。

【讨论】:

  • 我已经制作了 trans 字符串,创建 po 文件,并将其编译为 mo,但仍然不显示我的翻译,但我添加了一些 django 支持的语言它可以工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-13
  • 2011-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-11
  • 2021-03-30
相关资源
最近更新 更多