如何让Django支持IETF语言标签(xx-YY格式)?
How to make Django support IETF language tag (xx-YY format)?
我们有一个支持多种语言的 Django 站点。尝试添加多语言的opensearch插件支持。
OpenSearch.org spec uses IETF language tag (xx-YY format)。默认 Django 设置使用精确匹配。
目前状态,本站仅支持xx格式,其他returnE404。示例:
http://website.domain/en/...
根据用户配置,浏览器将语言参数插入为 xx 或 xx-YY。它需要对两者都有效
- 如果
xx-YY
不可用,站点应提供 xx
(母国语言)结果。
- 如果
xx
不可用,站点应提供 en
(英语)结果。
要支持的 URL 示例:
http://website.domain/fr-YY/...
fall-back to: http://website.domain/fr/...
http://website.domain/xx/...
fall-back to: http://website.domain/en/...
来自 Mozilla 站点的示例 URL:
https://support.mozilla.org/en-US/questions/949545
如何让Django支持IETF语言标签(xx-YY格式)?我什至在寻找在不修改 django 上游代码的情况下实现它的提示。
更新:
好吧,官方文档明确表示它应该回退(例如:en-us 到 en)但是我的情况引发了 404 错误。
来源:https://django.readthedocs.io/en/1.5.x/topics/i18n/translation.html
If a base language is available but the sublanguage specified is not, Django uses the base language. For example, if a user specifies de-at (Austrian German) but Django only has de available, Django uses de.
...
LANGUAGES = (
('de', _('German')),
('en', _('English')),
)
This example restricts languages that are available for automatic selection to German and English (and any sublanguage, like de-ch or en-us).
相关代码部分如下:
settings.py
# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en'
LANGUAGES = (
( 'ar', "Arabic" ),
( 'en', "English" ),
( 'fr', "French" ),
( 'id', "Indonesian" ),
( 'ja', "Japanese"),
( 'ku', "Kurdish" ),
#( 'ur', "Urdu" ),
( 'ms', "Malay" ),
( 'ml', "Malayalam" ),
#( 'tr', "Turkish" ),
( 'es', "Spanish" ),
( 'pt', "Portuguese"),
#( 'sv', "swedish" )
)
# These are languages not supported by Django core. We have to provide
# their info here so we can use them in our templates. This is mainly
# used in `wui.templatetags.languages`.
EXTRA_LANGUAGES = {
'ku': {
'code': 'ku',
'name': 'Kurdish',
'bidi': True,
'name_local': 'Kurdish'
},
'ms': {
'code': 'ms',
'name': 'Malay',
'bidi': False,
'name_local': 'Malay'
},
}
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
urls.py
from django.conf.urls import patterns, include, url
from django.conf.urls.i18n import i18n_patterns
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns( '',
url( r'^jos2', 'wui.views.jos2' ),
url(r'^r', 'wui.views.one_aya_page'),
url(r'^$', 'wui.views.results'),
# url( r'^admin/', include( admin.site.urls ) ),
)
# These URLs accept the language prefix.
urlpatterns += i18n_patterns('',
url(r'^$', 'wui.views.results'),
url(r'^(?P<unit>\w{3,15})/', 'wui.views.results'),
)
# 404 not found handler
handler404 = 'wui.views.custom_404'
您需要在设置中指定支持的语言:
LANGUAGES = (
('en', _('English'),
('en-gb', _('British English'),
('en-au', _('Australian English'),
('es', _('Spanish'),
('es-ar', _('Argentinian Spanish'),
)
从那里开始,使用 i18n_urlpatterns
。当您创建 PO 文件时,您将需要 运行 python manage.py makemessages -l en_GB
等。查看 here 了解更多信息。
据我测试,Django i18n 确实支持 fall-back xx-YY
到 xx
然后默认(en
在我的例子中)但只支持 Accept-Language
用户代理 header。 URL 语言切换不一样。
这是我能想到的解决方案:
from django.views.generic import RedirectView
from django.conf import settings
...
urlpatterns += patterns('',
url(r'^(?P<lang>[a-z]{2})-[A-Za-z]{2}/(?P<path>.*)$', RedirectView.as_view(url='/%(lang)s/%(path)s',query_string=True)),
url(r'^[a-z]{2}/(?P<path>.*)$', RedirectView.as_view(url='/{}/%(path)s'.format(settings.LANGUAGE_CODE),query_string=True)),
)
- 任何
xx-YY
未被 i18n 模式处理的重定向到 xx
- 任何
xx
未被 i18n 模式处理的重定向到使用 LANGUAGE_CODE
设置的默认语言。
我们有一个支持多种语言的 Django 站点。尝试添加多语言的opensearch插件支持。
OpenSearch.org spec uses IETF language tag (xx-YY format)。默认 Django 设置使用精确匹配。
目前状态,本站仅支持xx格式,其他returnE404。示例:
http://website.domain/en/...
根据用户配置,浏览器将语言参数插入为 xx 或 xx-YY。它需要对两者都有效
- 如果
xx-YY
不可用,站点应提供xx
(母国语言)结果。 - 如果
xx
不可用,站点应提供en
(英语)结果。
要支持的 URL 示例:
http://website.domain/fr-YY/...
fall-back to: http://website.domain/fr/...
http://website.domain/xx/...
fall-back to: http://website.domain/en/...
来自 Mozilla 站点的示例 URL: https://support.mozilla.org/en-US/questions/949545
如何让Django支持IETF语言标签(xx-YY格式)?我什至在寻找在不修改 django 上游代码的情况下实现它的提示。
更新:
好吧,官方文档明确表示它应该回退(例如:en-us 到 en)但是我的情况引发了 404 错误。
来源:https://django.readthedocs.io/en/1.5.x/topics/i18n/translation.html
If a base language is available but the sublanguage specified is not, Django uses the base language. For example, if a user specifies de-at (Austrian German) but Django only has de available, Django uses de.
...
LANGUAGES = ( ('de', _('German')), ('en', _('English')), )
This example restricts languages that are available for automatic selection to German and English (and any sublanguage, like de-ch or en-us).
相关代码部分如下:
settings.py
# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en'
LANGUAGES = (
( 'ar', "Arabic" ),
( 'en', "English" ),
( 'fr', "French" ),
( 'id', "Indonesian" ),
( 'ja', "Japanese"),
( 'ku', "Kurdish" ),
#( 'ur', "Urdu" ),
( 'ms', "Malay" ),
( 'ml', "Malayalam" ),
#( 'tr', "Turkish" ),
( 'es', "Spanish" ),
( 'pt', "Portuguese"),
#( 'sv', "swedish" )
)
# These are languages not supported by Django core. We have to provide
# their info here so we can use them in our templates. This is mainly
# used in `wui.templatetags.languages`.
EXTRA_LANGUAGES = {
'ku': {
'code': 'ku',
'name': 'Kurdish',
'bidi': True,
'name_local': 'Kurdish'
},
'ms': {
'code': 'ms',
'name': 'Malay',
'bidi': False,
'name_local': 'Malay'
},
}
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
urls.py
from django.conf.urls import patterns, include, url
from django.conf.urls.i18n import i18n_patterns
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns( '',
url( r'^jos2', 'wui.views.jos2' ),
url(r'^r', 'wui.views.one_aya_page'),
url(r'^$', 'wui.views.results'),
# url( r'^admin/', include( admin.site.urls ) ),
)
# These URLs accept the language prefix.
urlpatterns += i18n_patterns('',
url(r'^$', 'wui.views.results'),
url(r'^(?P<unit>\w{3,15})/', 'wui.views.results'),
)
# 404 not found handler
handler404 = 'wui.views.custom_404'
您需要在设置中指定支持的语言:
LANGUAGES = (
('en', _('English'),
('en-gb', _('British English'),
('en-au', _('Australian English'),
('es', _('Spanish'),
('es-ar', _('Argentinian Spanish'),
)
从那里开始,使用 i18n_urlpatterns
。当您创建 PO 文件时,您将需要 运行 python manage.py makemessages -l en_GB
等。查看 here 了解更多信息。
据我测试,Django i18n 确实支持 fall-back xx-YY
到 xx
然后默认(en
在我的例子中)但只支持 Accept-Language
用户代理 header。 URL 语言切换不一样。
这是我能想到的解决方案:
from django.views.generic import RedirectView
from django.conf import settings
...
urlpatterns += patterns('',
url(r'^(?P<lang>[a-z]{2})-[A-Za-z]{2}/(?P<path>.*)$', RedirectView.as_view(url='/%(lang)s/%(path)s',query_string=True)),
url(r'^[a-z]{2}/(?P<path>.*)$', RedirectView.as_view(url='/{}/%(path)s'.format(settings.LANGUAGE_CODE),query_string=True)),
)
- 任何
xx-YY
未被 i18n 模式处理的重定向到xx
- 任何
xx
未被 i18n 模式处理的重定向到使用LANGUAGE_CODE
设置的默认语言。