简体   繁体   English

如何本地化django_tables2列名?

[英]How to localize django_tables2 column names?

I have used django_tables2 in my web page to display a table. 我在网页中使用django_tables2来显示表格。

My code for this table is in tables.py: 我对此表的代码在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)

In my views.py method i am doing "from myapp.tables import patchTable" and then updating the table contents,paginating and rendering to a template. 在我的views.py方法中,我正在“从myapp.tables导入patchTable”,然后更新表的内容,将其分页并呈现为模板。

Above code works fine and displays the column name in a language which is currently i am using(during runserver command). 上面的代码运行良好,并以我当前正在使用的语言(在runserver命令期间)显示列名称。 but if i change the language selection on my HTML page all other contents on page gets translated but column name of this table doesn't. 但是,如果我更改HTML页面上的语言选择,则页面上的所有其他内容都会被翻译,但此表的列名不会。

If i restart the django server (cntrl+c and python2.7 manage runserver 0.0.0.0:8060) then these names changes to current language but they are not happening dynamically on language selection. 如果我重新启动django服务器(cntrl + c和python2.7管理运行服务器0.0.0.0:8060),则这些名称将更改为当前语言,但在选择语言时不会动态发生。

I tried using 1) "ugettext" 2) "ugettext_lazy" (this throws exception:'Lazy object returned unexpected type.)' 3) "force_text" and "ugettext_lazy" combination. 我尝试使用1)“ ugettext” 2)“ ugettext_lazy”(抛出异常:“惰性对象返回了意外的类型。”)3)“ force_text”和“ ugettext_lazy”的组合。 But they are not working. 但是他们没有工作。 Can anyone suggest me a feasible way of doing it? 有人可以建议我这样做吗?

By the way I am using Python 2.7, Django 1.5.1 and Django development server. 顺便说一句,我正在使用Python 2.7,Django 1.5.1和Django开发服务器。 All the localization settings requirement are included in project and settings.py has "USE_L10N = True" and "USE_I18N = True". 所有本地化设置要求都包含在项目中,并且settings.py具有“ USE_L10N = True”和“ USE_I18N = True”。 Any help would be appreciated. 任何帮助,将不胜感激。 Please let me know if this question needs much more details. 如果这个问题需要更多细节,请告诉我。 Thanks in advance. 提前致谢。

Under class patchTable(tables.Table): , please add this: class patchTable(tables.Table): ,请添加以下内容:

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

__init__ is a constructor, so when the class is called, an object of that Class will be created. __init__是一个构造函数,因此当调用该类时,将创建该Class的对象。 Details here . 详细信息在这里

I re-wrote the code as, 我将代码重新编写为

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() is a function that returns translated strings as key value pair. get_string()是一个将转换后的字符串作为键值对返回的函数。 keys are nothing but the column names, values are their respective translated strings(changes on language selection). 键不过是列名,值是它们各自的翻译字符串(语言选择的更改)。 verbose name is defined by class test() which initializes column names for each new object. 详细名称由类test()定义,该类初始化每个新对象的列名称。 init with unicode helped me to solve this. 使用unicode进行 初始化有助于解决此问题。

Thanks to @ruddra and @VikasVerma, Your suggestions helped me. 感谢@ruddra和@VikasVerma,您的建议对我有所帮助。

A general solution for tables based on a model: 基于模型的表的一般解决方案:

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

The column headers will be defined by verbose_name of the corresponding field and properly translated. 列标题将由相应字段的verbose_name定义并正确翻译。 Note that verbose_name in this case should be decorated for translation in the model definition, for example: 请注意,在这种情况下, 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"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM