Django:默认语言i18n [英] Django: default language i18n
问题描述
我有一个用荷兰语编写的网站.现在,我必须为该网站提供第二种语言,即法语.
I have a website that is written in dutch. Now I have to provide a second language for that website which is french.
因此,我用gettext函数包围了所有需要翻译的文本,创建了po文件并将其编译为mo文件.我还创建了一个视图,该视图将django_language会话设置为适当的语言代码.所以现在法语版本可以使用了,但是我不能切换回荷兰语版本.
So I surrounded all text that needs to be translated with the gettext function, created the po files and compiled those to mo files. I also created a view that sets the django_language session to the appropriate language code. So now the french version is working but I can't switch back to the dutch version.
所以我想知道是否还需要为荷兰语版本创建po/mo文件?gettext过去的文本已经在荷兰语中了.有没有办法说使用默认文本"?
So I was wondering do I need to create a po/mo file for the dutch version also? The text that's being past to gettext is already in dutch. Is there a way to say use the 'default text'?
这是我用来将语言代码添加到会话中的视图:
This is the view I use to add the language code to my session:
class LanguagePickerView(RedirectView):
url = '/'
def get(self,request,*args, **kwargs):
request.session['django_language'] = self.kwargs.get('language')
return super(LanguagePickerView, self).get(request, args, kwargs)
在我的模板中,我使用以下网址:
In my templates I use the following urls:
<a href='{% url web-language 'nl-nl' %}'>NL</a>
<a href='{% url web-language 'fr' %}'>fr</a>
推荐答案
默认语言字符串未存储在po/mo文件中,它们直接存储在代码和模板中-似乎您具有此权限.
Default language strings are not stored in po/mo files, they go directly in code and templates - seems that you have this right.
您可以通过将会话变量 django_language
设置回荷兰语来切换回它.
You can switch back to it, by setting the session variable django_language
back to dutch.
确保已正确设置设置:
LANGUAGE_CODE = 'nl' #default language
LANGUAGES = (
('nl', _('Dutch')),
('fr', _('French')),
)
别忘了,您不必自己编写代码即可在各种语言之间切换.最好使用特殊的django视图(来自 django图书的引用):
Don't forget, that you don't have to write code to switch between languages by your self. Better to use special django view (quote from django book):
为方便起见,Django附带了一个视图django.views.i18n.set_language,该视图可设置用户的语言偏好设置并重定向回上一页.
As a convenience, Django comes with a view, django.views.i18n.set_language, that sets a user’s language preference and redirects back to the previous page.
通过在您的URLconf中添加以下行来激活此视图:
Activate this view by adding the following line to your URLconf:
(r'^i18n/', include('django.conf.urls.i18n')),
这篇关于Django:默认语言i18n的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!