Added the get_multi_language_attr() function to the Mia core application, to deal with the multi-lingual attributes, and applied it in the data models.

This commit is contained in:
依瑪貓 2020-07-14 22:01:32 +08:00
parent a8d18ddd1e
commit fa034b9a6a
5 changed files with 92 additions and 11 deletions

View File

@ -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)

View File

@ -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 %}
<div class="btn-group">
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
<span class="d-none d-md-inline">{{ current_subject.title_zhtw }}</span>
<span class="d-none d-md-inline">{{ current_subject.title|title }}</span>
<span class="d-md-none">{% trans "Subject" context "Accounting|" as text %}{{ text|force_escape }}</span>
</button>
<div class="dropdown-menu subject-picker">
<div class="dropdown-header">{% trans "Shortcuts" context "Accounting|Subject|" as text %}{{ text|force_escape }}</div>
{% for subject in shortcut_subjects %}
<a class="dropdown-item {% if subject.code == current_subject.code %}{% endif %}>" href="{% url "accounting:cash" subject.code period.spec %}">{{ subject.title_zhtw|title }}</a>
<a class="dropdown-item {% if subject.code == current_subject.code %}{% endif %}>" href="{% url "accounting:cash" subject.code period.spec %}">{{ subject.title|title }}</a>
{% endfor %}
<div class="dropdown-header">{% trans "All" context "Accounting|Subject|" as text %}{{ text|force_escape }}</div>
{% for subject in all_sibjects %}
<a class="dropdown-item {% if subject.code == current_subject.code %}{% endif %}>" href="{% url "accounting:cash" subject.code period.spec %}">{{ subject.title_zhtw|title }}</a>
<a class="dropdown-item {% if subject.code == current_subject.code %}{% endif %}>" href="{% url "accounting:cash" subject.code period.spec %}">{{ subject.title|title }}</a>
{% endfor %}
</div>
</div>
@ -106,7 +106,7 @@ First written: 2020/7/1
{% for record in records %}
<tr class="{% if not record.transaction.is_balanced or record.transaction.has_order_hole %} table-danger {% endif %}">
<td>{{ record.transaction.date|smart_date }}</td>
<td>{{ record.subject.title_zhtw }}</td>
<td>{{ record.subject.title|title }}</td>
<td>{{ record.summary|default:"" }}{% if not record.transaction.is_balanced %}
<span class="badge badge-danger badge-pill">
{% trans "Unbalanced" context "Accounting|" as text %}
@ -141,7 +141,7 @@ First written: 2020/7/1
{% if record.sn is not None %}
<a class="list-group-item-action" href="{{ record.transaction.get_absolute_url }}">
<div class="date-subject-line d-flex justify-content-between align-items-center">
{{ record.transaction.date|smart_date }} {{ record.subject.title_zhtw }}
{{ record.transaction.date|smart_date }} {{ record.subject.title|title }}
</div>
<div class="d-flex justify-content-between align-items-center">
<div>
@ -168,7 +168,7 @@ First written: 2020/7/1
</a>
{% else %}
<div class="date-subject-line d-flex justify-content-between align-items-center">
{{ record.transaction.date|smart_date }} {{ record.subject.title_zhtw }}
{{ record.transaction.date|smart_date }} {{ record.subject.title }}
</div>
<div class="d-flex justify-content-between align-items-center">
<div>{{ record.summary|default:"" }}</div>

View File

@ -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

View File

@ -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"]

View File

@ -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: