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:
parent
a8d18ddd1e
commit
fa034b9a6a
@ -22,6 +22,8 @@ from django.conf import settings
|
|||||||
from django.db import models
|
from django.db import models
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
|
||||||
|
from mia_core.utils import get_multi_language_attr
|
||||||
|
|
||||||
|
|
||||||
class Subject(models.Model):
|
class Subject(models.Model):
|
||||||
"""An accounting subject."""
|
"""An accounting subject."""
|
||||||
@ -50,7 +52,19 @@ class Subject(models.Model):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
"""Returns the string representation of this accounting
|
"""Returns the string representation of this accounting
|
||||||
subject."""
|
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:
|
class Meta:
|
||||||
db_table = "accounting_subjects"
|
db_table = "accounting_subjects"
|
||||||
@ -241,7 +255,7 @@ class Record(models.Model):
|
|||||||
record."""
|
record."""
|
||||||
return "%s %s %s %s" % (
|
return "%s %s %s %s" % (
|
||||||
self.transaction.date,
|
self.transaction.date,
|
||||||
self.subject.title_zhtw,
|
self.subject.title,
|
||||||
self.summary,
|
self.summary,
|
||||||
self.amount)
|
self.amount)
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ First written: 2020/7/1
|
|||||||
{% load accounting %}
|
{% load accounting %}
|
||||||
|
|
||||||
{% block settings %}
|
{% 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 "title" title %}
|
||||||
{% setvar "use_period_chooser" True %}
|
{% setvar "use_period_chooser" True %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
@ -63,17 +63,17 @@ First written: 2020/7/1
|
|||||||
{% endwith %}
|
{% endwith %}
|
||||||
<div class="btn-group">
|
<div class="btn-group">
|
||||||
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
|
<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>
|
<span class="d-md-none">{% trans "Subject" context "Accounting|" as text %}{{ text|force_escape }}</span>
|
||||||
</button>
|
</button>
|
||||||
<div class="dropdown-menu subject-picker">
|
<div class="dropdown-menu subject-picker">
|
||||||
<div class="dropdown-header">{% trans "Shortcuts" context "Accounting|Subject|" as text %}{{ text|force_escape }}</div>
|
<div class="dropdown-header">{% trans "Shortcuts" context "Accounting|Subject|" as text %}{{ text|force_escape }}</div>
|
||||||
{% for subject in shortcut_subjects %}
|
{% 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 %}
|
{% endfor %}
|
||||||
<div class="dropdown-header">{% trans "All" context "Accounting|Subject|" as text %}{{ text|force_escape }}</div>
|
<div class="dropdown-header">{% trans "All" context "Accounting|Subject|" as text %}{{ text|force_escape }}</div>
|
||||||
{% for subject in all_sibjects %}
|
{% 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 %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@ -106,7 +106,7 @@ First written: 2020/7/1
|
|||||||
{% for record in records %}
|
{% for record in records %}
|
||||||
<tr class="{% if not record.transaction.is_balanced or record.transaction.has_order_hole %} table-danger {% endif %}">
|
<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.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 %}
|
<td>{{ record.summary|default:"" }}{% if not record.transaction.is_balanced %}
|
||||||
<span class="badge badge-danger badge-pill">
|
<span class="badge badge-danger badge-pill">
|
||||||
{% trans "Unbalanced" context "Accounting|" as text %}
|
{% trans "Unbalanced" context "Accounting|" as text %}
|
||||||
@ -141,7 +141,7 @@ First written: 2020/7/1
|
|||||||
{% if record.sn is not None %}
|
{% if record.sn is not None %}
|
||||||
<a class="list-group-item-action" href="{{ record.transaction.get_absolute_url }}">
|
<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">
|
<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>
|
||||||
<div class="d-flex justify-content-between align-items-center">
|
<div class="d-flex justify-content-between align-items-center">
|
||||||
<div>
|
<div>
|
||||||
@ -168,7 +168,7 @@ First written: 2020/7/1
|
|||||||
</a>
|
</a>
|
||||||
{% else %}
|
{% else %}
|
||||||
<div class="date-subject-line d-flex justify-content-between align-items-center">
|
<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>
|
||||||
<div class="d-flex justify-content-between align-items-center">
|
<div class="d-flex justify-content-between align-items-center">
|
||||||
<div>{{ record.summary|default:"" }}</div>
|
<div>{{ record.summary|default:"" }}</div>
|
||||||
|
@ -88,7 +88,7 @@ FROM accounting_subjects AS s
|
|||||||
ORDER BY s.code"""))
|
ORDER BY s.code"""))
|
||||||
subjects.insert(0, Subject(
|
subjects.insert(0, Subject(
|
||||||
code="0",
|
code="0",
|
||||||
title_zhtw=pgettext(
|
title=pgettext(
|
||||||
"Accounting|", "current assets and liabilities"),
|
"Accounting|", "current assets and liabilities"),
|
||||||
))
|
))
|
||||||
current_subject = None
|
current_subject = None
|
||||||
|
@ -21,6 +21,8 @@
|
|||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
|
from mia_core.utils import get_multi_language_attr
|
||||||
|
|
||||||
|
|
||||||
class Country(models.Model):
|
class Country(models.Model):
|
||||||
"""A country."""
|
"""A country."""
|
||||||
@ -46,6 +48,18 @@ class Country(models.Model):
|
|||||||
"""Returns the string representation of this country."""
|
"""Returns the string representation of this country."""
|
||||||
return self.code.__str__() + " " + self.name_zhtw.__str__()
|
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:
|
class Meta:
|
||||||
db_table = "countries"
|
db_table = "countries"
|
||||||
ordering = ["code"]
|
ordering = ["code"]
|
||||||
|
@ -21,7 +21,60 @@
|
|||||||
|
|
||||||
import urllib.parse
|
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:
|
class UrlBuilder:
|
||||||
|
Loading…
Reference in New Issue
Block a user