diff --git a/accounting/models.py b/accounting/models.py index ce0e95d..2a18fd8 100644 --- a/accounting/models.py +++ b/accounting/models.py @@ -22,6 +22,8 @@ from django.conf import settings from django.db import models from django.urls import reverse +from mia_core.utils import get_multi_language_attr + class Subject(models.Model): """An accounting subject.""" @@ -50,7 +52,19 @@ class Subject(models.Model): def __str__(self): """Returns the string representation of this accounting subject.""" - return self.code.__str__() + " " + self.title_zhtw.__str__() + return self.code.__str__() + " " + self.title + + _title = None + + @property + def title(self): + if self._title is None: + self._title = get_multi_language_attr(self, "title") + return self._title + + @title.setter + def title(self, value): + self._title = value class Meta: db_table = "accounting_subjects" @@ -241,7 +255,7 @@ class Record(models.Model): record.""" return "%s %s %s %s" % ( self.transaction.date, - self.subject.title_zhtw, + self.subject.title, self.summary, self.amount) diff --git a/accounting/templates/accounting/cash.html b/accounting/templates/accounting/cash.html index 2847913..d5a1057 100644 --- a/accounting/templates/accounting/cash.html +++ b/accounting/templates/accounting/cash.html @@ -25,7 +25,7 @@ First written: 2020/7/1 {% load accounting %} {% block settings %} - {% blocktrans asvar title with subject=current_subject.title_zhtw period=period.description context "Accounting|" %}Cash Account for {{ subject }} in {{ period }}{% endblocktrans %} + {% blocktrans asvar title with subject=current_subject.title|title period=period.description context "Accounting|" %}Cash Account for {{ subject }} in {{ period }}{% endblocktrans %} {% setvar "title" title %} {% setvar "use_period_chooser" True %} {% endblock %} @@ -63,17 +63,17 @@ First written: 2020/7/1 {% endwith %}
@@ -106,7 +106,7 @@ First written: 2020/7/1 {% for record in records %} {{ record.transaction.date|smart_date }} - {{ record.subject.title_zhtw }} + {{ record.subject.title|title }} {{ record.summary|default:"" }}{% if not record.transaction.is_balanced %} {% trans "Unbalanced" context "Accounting|" as text %} @@ -141,7 +141,7 @@ First written: 2020/7/1 {% if record.sn is not None %}
- {{ record.transaction.date|smart_date }} {{ record.subject.title_zhtw }} + {{ record.transaction.date|smart_date }} {{ record.subject.title|title }}
@@ -168,7 +168,7 @@ First written: 2020/7/1 {% else %}
- {{ record.transaction.date|smart_date }} {{ record.subject.title_zhtw }} + {{ record.transaction.date|smart_date }} {{ record.subject.title }}
{{ record.summary|default:"" }}
diff --git a/accounting/views/__init__.py b/accounting/views/__init__.py index 3ebaa8f..8fada30 100644 --- a/accounting/views/__init__.py +++ b/accounting/views/__init__.py @@ -88,7 +88,7 @@ FROM accounting_subjects AS s ORDER BY s.code""")) subjects.insert(0, Subject( code="0", - title_zhtw=pgettext( + title=pgettext( "Accounting|", "current assets and liabilities"), )) current_subject = None diff --git a/mia_core/models.py b/mia_core/models.py index 5eb98ad..c1a0a3c 100644 --- a/mia_core/models.py +++ b/mia_core/models.py @@ -21,6 +21,8 @@ from django.db import models +from mia_core.utils import get_multi_language_attr + class Country(models.Model): """A country.""" @@ -46,6 +48,18 @@ class Country(models.Model): """Returns the string representation of this country.""" return self.code.__str__() + " " + self.name_zhtw.__str__() + _title = None + + @property + def title(self): + if self._title is None: + self._title = get_multi_language_attr(self, "title") + return self._title + + @title.setter + def title(self, value): + self._title = value + class Meta: db_table = "countries" ordering = ["code"] diff --git a/mia_core/utils.py b/mia_core/utils.py index 7480b5a..63ca8de 100644 --- a/mia_core/utils.py +++ b/mia_core/utils.py @@ -21,7 +21,60 @@ import urllib.parse -from django.utils.translation import pgettext +from django.conf import settings +from django.utils.translation import pgettext, get_language + + +class Language: + """A language. + + Args: + language (str): The Django language code. + + Attributes: + db (str): The database column suffix of this language. + locale (str); The locale name of this language. + is_default (bool): Whether this is the default language. + """ + db = None + locale = None + is_default = False + + def __init__(self, language): + 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 + def get_default(): + return Language(settings.LANGUAGE_CODE) + + +def get_multi_language_attr(model, name): + """Returns a multi-language attribute of a data model. + + Args: + model (object): The data model. + name (str): The attribute name. + + Returns: + (any): The attribute in this language, or in the default + language if there is no content in the current language. + """ + language = Language(get_language()) + title = getattr(model, name + "_" + language.db) + if language.is_default: + return title + if title is not None: + return title + return getattr(model, name + "_" + Language.get_default().db) class UrlBuilder: