[英]How to localize django_tables2 column names?
我在網頁中使用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管理運行服務器0.0.0.0:8060),則這些名稱將更改為當前語言,但在選擇語言時不會動態發生。
我嘗試使用1)“ ugettext” 2)“ ugettext_lazy”(拋出異常:“惰性對象返回了意外的類型。”)3)“ force_text”和“ ugettext_lazy”的組合。 但是他們沒有工作。 有人可以建議我這樣做嗎?
順便說一句,我正在使用Python 2.7,Django 1.5.1和Django開發服務器。 所有本地化設置要求都包含在項目中,並且settings.py具有“ USE_L10N = True”和“ USE_I18N = True”。 任何幫助,將不勝感激。 如果這個問題需要更多細節,請告訴我。 提前致謝。
在class patchTable(tables.Table):
,請添加以下內容:
def __init__(self, *args, **kwargs):
super(patchTable, self).__init__(*args, **kwargs)
__init__
是一個構造函數,因此當調用該類時,將創建該Class的對象。 詳細信息在這里 。
我將代碼重新編寫為
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()定義,該類初始化每個新對象的列名稱。 使用unicode進行 初始化有助於解決此問題。
感謝@ruddra和@VikasVerma,您的建議對我有所幫助。
基於模型的表的一般解決方案:
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"))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.