【问题标题】:How to localize django_tables2 column names?如何本地化 django_tables2 列名?
【发布时间】:2014-06-02 10:56:40
【问题描述】:

我在我的网页中使用了 django_tables2 来显示一个表格。

我的这个表的代码在tables.py中:

import django_tables2 as tables
from django.utils.translation import ugettext_lazy
from django.utils.encoding import force_text

    class patchTable(tables.Table):
        release_version=tables.Column(verbose_name=force_text(ugettext_lazy("Release Version"),orderable=False, localize=True)
        patch_version=tables.Column(verbose_name=force_text(ugettext_lazy("Patch Version")),orderable=False, localize=True)
        release_date = tables.Column(verbose_name=force_text(ugettext_lazy("Release Date")),orderable=False, localize=True)
        upload_date = tables.Column(verbose_name=force_text(ugettext_lazy("Upload Date")),orderable=False, localize=True)
        apply_status = tables.Column(verbose_name=force_text(ugettext_lazy("Status")),orderable=False, localize=True)
        installation_date = tables.Column(verbose_name=force_text(ugettext_lazy("Installation Date")),orderable=False, localize=True)

在我的 views.py 方法中,我正在执行“从 myapp.tables 导入 patchTable”,然后更新表格内容、分页并呈现到模板。

上面的代码工作正常,并以我当前正在使用的语言显示列名(在 runserver 命令期间)。但是如果我更改 HTML 页面上的语言选择,页面上的所有其他内容都会被翻译,但该表的列名不会。

如果我重新启动 django 服务器(cntrl+c 和 python2.7 管理 runserver 0.0.0.0:8060),那么这些名称会更改为当前语言,但它们不会在语言选择时动态发生。

我尝试使用 1)“ugettext”2)“ugettext_lazy”(这会引发异常:'Lazy 对象返回了意外类型。)'3)“force_text”和“ugettext_lazy”组合。但他们没有工作。谁能给我一个可行的方法吗?

顺便说一下,我使用的是 Python 2.7、Django 1.5.1 和 Django 开发服务器。所有本地化设置要求都包含在项目中,settings.py 有“USE_L10N = True”和“USE_I18N = True”。 任何帮助,将不胜感激。如果这个问题需要更多细节,请告诉我。提前致谢。

【问题讨论】:

    标签: python django django-tables2


    【解决方案1】:

    class patchTable(tables.Table):下,请添加:

    def __init__(self, *args, **kwargs):
            super(patchTable, self).__init__(*args, **kwargs)
    

    __init__ 是一个构造函数,所以当类被调用时,会创建该类的一个对象。详情here.

    【讨论】:

    • 这是一个有用的信息。我在我的班级下定义了这个。并且看到每次都会创建该类的对象。但我仍然没有得到列标题的翻译字符串。我尝试将 views.py 方法中的翻译字符串作为参数传递给 __init__() 函数,并尝试将它们分配给列属性。但我仍然没有找到解决它的方法。
    • super(patchTable, self).__init__(*args, **kwargs) 下,尝试将翻译后的字符串分配为列的属性
    【解决方案2】:

    我将代码重写为,

    class test():
        _name = None
        def __init__(self, name):
            self._name = name
    
        def __unicode__(self):
            english_string = get_string()
            params = {}
            params = deepcopy(english_string)
            var = force_text(params['patch_text'][self._name])
            return unicode(var )
    
    class patchTable(tables.Table):
    
        release_version = tables.Column(verbose_name=test('release_version'),orderable=False)
        patch_version = tables.Column(verbose_name=test('patch_version'),orderable=False)
        release_date = tables.Column(verbose_name=test('release_date'),orderable=False)
        upload_date = tables.Column(verbose_name=test('upload_date'),orderable=False)
        apply_status = tables.Column(verbose_name=test('status'),orderable=False)
        installation_date = tables.Column(verbose_name=test('installation_date'),orderable=False)
        class Meta:
            attrs = {"class": "paleblue", "id":"patch_details", "style":"cursor: pointer;"}
            orderable = False
    

    get_string() 是一个将翻译后的字符串作为键值对返回的函数。键只不过是列名,值是它们各自的翻译字符串(语言选择的变化)。 详细名称由类 test() 定义,该类为每个新对象初始化列名。 initunicode 帮助我解决了这个问题。

    感谢@ruddra 和@VikasVerma,您的建议帮助了我。

    【讨论】:

    • 您介意提供您使用的 get_string() 函数吗?
    【解决方案3】:

    基于模型的表格通用解决方案:

    class TranslatedTable(tables.Table):
    def __init__(self, *args, **kwargs):
        super(TranslatedTable, self).__init__(*args, **kwargs)
        for column in self.base_columns:
                self.base_columns[column].verbose_name = \
                    self.Meta.model._meta.get_field(column).verbose_name
    

    列标题将由相应字段的verbose_name 定义并正确翻译。请注意,在这种情况下,verbose_name 应在模型定义中进行修饰以进行翻译,例如:

    from django.db import models
    from django.utils.translation import ugettext_lazy as _
    
    class SomeModel(models.Model):
        name = models.CharField(max_length=100, verbose_name=_("Name"))
    

    【讨论】:

      猜你喜欢
      • 2014-01-13
      • 1970-01-01
      • 2011-08-02
      • 2011-06-10
      • 1970-01-01
      • 2016-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多