如何使Django支持IETF语言标签(xx-YY格式)? [英] How to make Django support IETF language tag (xx-YY format)?

查看:47
本文介绍了如何使Django支持IETF语言标签(xx-YY格式)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个支持多种语言的Django网站.尝试添加对多种语言的opensearch插件支持.

We have a Django site that supports many languages. Trying to add opensearch plug-ins support for multi-language.

OpenSearch.org规范使用 IETF语言标记(xx-YY格式).默认的Django设置使用完全匹配.

OpenSearch.org spec uses IETF language tag (xx-YY format). Default Django setup uses exact matching.

  • 当前状态,该站点仅支持xx形式的其他任何内容,返回E404.示例:

  • The current state, the site support only xx form anything else return E404. Example:

http://website.domain/en/...

根据用户配置,浏览器将插入语言参数为xx或xx-YY.它需要对两者都起作用

Depending on user configuration the browser insert language parameter as xx or xx-YY. It needs to work for both

  • 如果 xx-YY 不可用,则网站应提供 xx (母语).
  • 如果 xx 不可用,则网站应提供 en (英文)结果.
  • If xx-YY is not available, site should provide xx (mother country language) results.
  • if xx is not available, site should provide en (English) results.

要支持的网址示例:

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

Example URL from Mozilla site: https://support.mozilla.org/en-US/questions/949545

如何使Django支持IETF语言标记(xx-YY格式)?我什至在寻找无需修改django上游代码即可实现此目标的提示.

How to make Django support IETF language tag (xx-YY format)? I'm looking even for hints to implement this without modifying the django upstream code.

更新:

好吧,官方文档明确表示应该退后(例如:en-us to en),但我的案子引发了404错误.

Well, official documentation says clearly it should fall-back (ex: en-us to en) but my case raises 404 error.

来源: https://django.readthedocs.io/en/1.5.x/topics/i18n/translation.html

如果基本语言可用,但指定的子语言不可用,则Django使用基本语言.例如,如果用户指定de-at(奥地利德语),但Django只有de可用,则Django使用de.

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.

...

语言=(('de',_('German')),('en',_('English')),)

LANGUAGES = ( ('de', _('German')), ('en', _('English')), )

此示例将可用于自动选择的语言限制为德语和英语(以及任何副语言,例如de-ch或en-us).

This example restricts languages that are available for automatic selection to German and English (and any sublanguage, like de-ch or en-us).

以下是相关的代码部分:

Here are related code portions:

settings.py

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

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'

推荐答案

据我所知,Django i18n确实支持将 xx-YY 转换为 xx 然后设置为默认值(在我的情况下为 en ),但仅适用于 Accept-Language 用户代理标头.URL语言切换与之不同.

Well as far I can test, Django i18n does supports fall-back xx-YY to xx then to default (en in my case) but only for Accept-Language user agent header. It does not do same for URL language switch.

这是我可以提出的解决方案:

Here is the solution I could come up with:

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)),
)

  • 任何未由i18n模式处理的 xx-YY 都已重定向到 xx
  • 任何未由i18n模式处理的 xx 都重定向为使用 LANGUAGE_CODE 设置的默认语言.
    • Any xx-YY not handled by i18n pattern redirected to xx
    • Any xx not handled by i18n pattern redirected to default language set using LANGUAGE_CODE.
    • 这篇关于如何使Django支持IETF语言标签(xx-YY格式)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆