【问题标题】:Allowing language change through a query string parameter ?lang=<code> in django with i18n使用 i18n 在 django 中通过查询字符串参数 ?lang=<code> 允许语言更改
【发布时间】:2017-04-06 16:43:32
【问题描述】:

在 django 中,我想允许 ?...&lang=&... 形式的查询字符串设置当前 url。理想情况下, lang= 将从查询字符串中删除,并将会话语言键设置为新语言。

最终生成的 url 将是浏览器登陆的 url。除了 i18n 提供的其他语言选择方法(所以我猜这将是一个中间件)之外,网站上的每个 url 都应该发生这种情况。

我不喜欢 POST 来查看似乎是 django-i18n 标准的方法。

这样的东西已经存在了吗?

【问题讨论】:

    标签: python django internationalization


    【解决方案1】:

    Django 中间件语言更改查询字符串 ?hl=langcode

    https://gist.github.com/teury/5d2213181ed0abe06c316c19431f355e

    import django
    from django.utils import translation
    from django.shortcuts import redirect, reverse
    
    
    def is_django_greater_than_1_10():
        main_version = django.VERSION[0]
        if main_version > 1:
            return True
    
        sub_version = django.VERSION[1]
        if main_version == 1 and sub_version >= 10:
            return True
    
        return False
    
    
    if is_django_greater_than_1_10():
        from django.utils.deprecation import MiddlewareMixin
        superclass = MiddlewareMixin
    else:
        superclass = object
    
    
    class LanguageMiddleware(superclass):
        def process_response(self, request, response):
            if request.resolver_match.view_name is "home":   
                lang_url = request.GET.get('hl')
                if not lang_url:
                    return response
    
                current_language = translation.get_language()
                if lang_url == current_language:
                    return response
    
                translation.activate(lang_url)
                request.session[translation.LANGUAGE_SESSION_KEY] = lang_url
                return redirect(reverse('home') + '?hl=' + lang_url)  
    
            else:
                return response
    
    

    我已经在一个“主页”网址中实现了这个系统

    根据键值改变语言

    由 Google 翻译。 成语 nativo = 西班牙语

    【讨论】:

      【解决方案2】:

      在阅读了 Django 文档后,意识到 Django 开发人员似乎也在执行他们自己的编码标准(这里我特别考虑了源代码中的这条评论:

       Since this view changes how the user will see the rest of the site, it must
      only be accessed as a POST request. If called as a GET request, it will
      redirect to the page in the request (the 'next' parameter) without changing
      any state.
      

      ) 我决定采用in this post 概述的解决方案,我将(几乎)逐字粘贴在下面(因此是非语义表格布局):

      <div id="languages">
            <table >
              <tr>
            {% get_available_languages as languages %}
            {% for key, item in languages %}
              <td>
              <form action="/i18n/setlang/" method="post">
            {% csrf_token %}
                <input type="hidden" name="language" value="{{key}}"/>
                <input id="lang_select_{{key}}" type = "image" src="/media/img/{{ key }}.gif" alt="{{ item }}"/>
              </form>
                </td>
                {% endfor %}
              </tr>
            </table>     
      </div>
      

      我还提出了我自己的 JS 解决方案,它更灵活,也更复杂,所以它并不值得(虽然它提供了链接:但因为他们需要一个 onClick 属性,或道德等价物,他们对 SEO 目的毫无用处)。

      当然,抽出代码并消除愚蠢的 GET 限制也是一种选择 - 尽管维护性更高。


      P.S:那些发现我的评论高于对抗性的人(而且,毫无疑问,我的任务被误导了)应该考虑需要 POST 来切换语言的立场如何在 SE 遍历中飞行,同时强制跳过这种箍尊重既定的设计惯例,即允许用户通过单击标志图标来切换语言。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-12-16
        • 1970-01-01
        • 2015-02-06
        • 2020-08-29
        • 2014-09-02
        • 1970-01-01
        • 2019-06-23
        相关资源
        最近更新 更多