一个django应用程序,允许根据域来切换语言
django-domain-language的Python项目详细描述
django域语言
一个django应用程序,允许根据django服务器访问的域来切换语言。
您可能正在运行一个名为mysite.com
的站点,并希望在meineseite.de
下运行该站点的德语版本。
此应用程序可帮助您完成此操作。您可以用什么语言配置应该为哪个域服务。你也可以用它
配置特定于语言的子域,如de.mysite.com和fr.mysite.com。
安装
使用安装软件包
python setup.py install
将domain_lang
添加到已安装的应用程序中
INSTALLED_APPS=[...'domain_lang',]
在LocaleMiddleware
MIDDLEWARE=[...'django.middleware.locale.LocaleMiddleware','domain_lang.middleware.DomainLanguageMiddleware',...]
如果要访问模板中配置的域,请将domain_lang.context_processors.domain_language
添加到上下文处理器。
TEMPLATES = [
{
...
'OPTIONS': {
'context_processors': [
...
'domain_lang.context_processors.domain_language',
],
},
},
]
最后运行迁移以创建数据库表。
python manage.py migrate
用法
安装并运行django服务器之后,您将在管理员中找到domainlanguageappings。
您可以在那里配置域及其相应的语言。然后,中间件将始终基于reuqest的域设置acitve语言。
如果没有为活动域配置映射,则由LocaleMiddleware
设置的语言将就位。
饼干
您必须分别登录每个域。如果使用子域进行langauge选择(en.example.com,de.example.com,…)
您可以在SESSION_COOKIE_DOMAIN
前面加上一个点,这将允许您为所有子域登录一次
SESSION_COOKIE_DOMAIN = '.example.com'
模板上下文
如果已将domain_lang.context_processors.domain_language
添加到上下文处理器,则将提供所有映射
在名为language_mappings
的变量中您可以使用此选项显示指向网站其他版本的链接。
模板标记
此应用程序提供一个模板标记,允许您翻译域。它还将根据配置的映射转换给定url的域部分
{%loaddomain_lang%}{%translate_url'mysite.com/contact/''de'%} # Might be translated to meineseite.de/kontakt/
许可证
django-domain-language is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.