Renamed the get_multi_language_attr() method to get_multi_lingual_attr(), and renamed the multi-lingual model fields from _zhtw to _zh_hant, and _zhcn to _zh_hans.

This commit is contained in:
依瑪貓 2020-07-16 23:13:15 +08:00
parent a58f6d6d11
commit 418dcd9803
3 changed files with 25 additions and 24 deletions

View File

@ -23,7 +23,7 @@ from django.db import models
from django.urls import reverse
from mia_core.templatetags.mia_core import smart_month
from mia_core.utils import get_multi_language_attr
from mia_core.utils import get_multi_lingual_attr
class Subject(models.Model):
@ -33,10 +33,11 @@ class Subject(models.Model):
"self", on_delete=models.PROTECT, null=True, blank=True,
db_column="parent_sn")
code = models.CharField(max_length=5)
title_zhtw = models.CharField(max_length=32)
title_zh_hant = models.CharField(
max_length=32, db_column="title_zhtw")
title_en = models.CharField(max_length=128, null=True, blank=True)
title_zhcn = models.CharField(
max_length=32, null=True, blank=True)
title_zh_hans = models.CharField(
max_length=32, null=True, blank=True, db_column="title_zhcn")
created_at = models.DateTimeField(
auto_now_add=True, db_column="created")
created_by = models.ForeignKey(
@ -60,7 +61,7 @@ class Subject(models.Model):
@property
def title(self):
if self._title is None:
self._title = get_multi_language_attr(self, "title")
self._title = get_multi_lingual_attr(self, "title")
return self._title
@title.setter

View File

@ -21,7 +21,7 @@
from django.db import models
from mia_core.utils import get_multi_language_attr
from mia_core.utils import get_multi_lingual_attr
class Country(models.Model):
@ -29,8 +29,10 @@ class Country(models.Model):
sn = models.PositiveIntegerField(primary_key=True)
code = models.CharField(max_length=2, unique=True)
name_en = models.CharField(max_length=64)
name_zhtw = models.CharField(max_length=32, null=True)
name_zhcn = models.CharField(max_length=32, null=True)
name_zh_hant = models.CharField(
max_length=32, null=True, db_column="name_zhtw")
name_zh_hans = models.CharField(
max_length=32, null=True, db_column="name_zhcn")
is_special = models.BooleanField(
default=False, db_column="special")
created_at = models.DateTimeField(
@ -46,19 +48,19 @@ class Country(models.Model):
def __str__(self):
"""Returns the string representation of this country."""
return self.code.__str__() + " " + self.name_zhtw.__str__()
return self.code.__str__() + " " + self.name.__str__()
_title = None
_name = None
@property
def title(self):
if self._title is None:
self._title = get_multi_language_attr(self, "title")
return self._title
def name(self):
if self._name is None:
self._name = get_multi_lingual_attr(self, "name")
return self._name
@title.setter
def title(self, value):
self._title = value
@name.setter
def name(self, value):
self._name = value
class Meta:
db_table = "countries"

View File

@ -41,15 +41,13 @@ class Language:
is_default = False
def __init__(self, language):
self.db = "_" + language.lower().replace("-", "_")
if language == "zh-hant":
self.locale = "zh-TW"
self.db = "zhtw"
elif language == "zh-hans":
self.locale = "zh-CN"
self.db = "zhcn"
else:
self.locale = language
self.db = language
self.is_default = (language == settings.LANGUAGE_CODE)
@staticmethod
@ -61,8 +59,8 @@ class Language:
return Language(get_language())
def get_multi_language_attr(model, name):
"""Returns a multi-language attribute of a data model.
def get_multi_lingual_attr(model, name):
"""Returns a multi-lingual attribute of a data model.
Args:
model (object): The data model.
@ -73,12 +71,12 @@ def get_multi_language_attr(model, name):
language if there is no content in the current language.
"""
language = Language.current()
title = getattr(model, name + "_" + language.db)
title = getattr(model, name + language.db)
if language.is_default:
return title
if title is not None:
return title
return getattr(model, name + "_" + Language.default().db)
return getattr(model, name + Language.default().db)
class UrlBuilder: