本文介绍了如何链接到django-cms中未翻译页面的语言主页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在django-cms中,我有两种语言,其中一页荷兰语目前尚未翻译成英文.在查看此页面时,language_chooser不提供英文翻译的链接(因为没有).但是,我想将链接链接到英文翻译,以链接到首页(或其他有意义的英文页面).现在,我可以自己创建所需的模板标签,也可以创建一些模板技巧,但是我认为这个问题已经解决了.可悲的是,我找不到这种解决方案的任何例子.

Within django-cms I have two languages, with one page in Dutch currently not translated into English. When viewing this page, language_chooser does not provide a link for the English translation (as there is not). However, I would like to link the link to the English translation to link to the homepage (or some other English page if it makes sense). Now I could create the needed template tag myself, or some template trickery, but I think this problem has been solved before. Sadly though, I could not find any example of such a solution.

语言选择器的用法如下:

Language chooser is used like this:

<p><small>Choose your language: {% language_chooser %}</small></p>

此模板标记使用的默认模板(在github上的源代码):

The default template used by this template tag (source on github):

{% load menu_tags %}
{% for language in languages %}
<a href="{% page_language_url language.0 %}"{% ifequal current_language language.0 %} class="current"{% endifequal %}>{{ language.1 }}</a>
{% endfor %}

呈现的html(请注意空白的href链接为英文):

Rendered html (note the empty href for link to English):

<p><small>Choose your language:
<a href="">English</a>
<a href="/nl/contact/" class="current">Nederlands</a>
</small></p>

推荐答案

我建议以下内容:

通过子类化现有标签为 page_language_url 制作自己的模板标签,将其放在您的一个项目应用程序的模板标签文件中,我们将其称为 menu_extra_tags.py :

Make an own template tag for page_language_url by subclassing it's existing tag, put it in a template tag file in one of your projects apps, let's call it menu_extra_tags.py:

from django import template
from menus.templatetags.menu_tags import PageLanguageUrl
from classytags.arguments import Argument
from classytags.core import Options

register = template.Library()

class PageLanguageUrlAsVariable(PageLanguageUrl):
    name = 'page_language_url_as_variable'
    options = Options(
        Argument('lang'),
        'as',
        Argument('varname', resolve=False),
    )
    def render_tag(self, context, **kwargs):
        varname = kwargs.pop('varname')
        url = super(PageLanguageUrlAsVariable, self).render_tag(context, **kwargs)
        context[varname] = url
        return ''
register.tag(PageLanguageUrlAsVariable)

现在在您的语言选择器模板中执行以下操作:

Now in your language chooser template do:

{% load menu_tags menu_extra_tags %}
{% for language in languages %}
    {% page_language_url_as_variable language.0 as pageurl %}
        <a href="{% if pageurl %}{{ pageurl }}{% else %}/{{ language.0 }}/{% endif %}"{% ifequal current_language language.0 %} class="current"{% endifequal %}>{{ language.1 }}</a>
{% endfor %}

如果该页面的英文翻译不可用,它将重定向到/en/(英语首页).

That will redirect you to /en/ (the home page in English) if the English translation for this page is not available.

这篇关于如何链接到django-cms中未翻译页面的语言主页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 18:29