|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
# |
|
3
|
|
|
# This file is part of SENAITE.CORE. |
|
4
|
|
|
# |
|
5
|
|
|
# SENAITE.CORE is free software: you can redistribute it and/or modify it under |
|
6
|
|
|
# the terms of the GNU General Public License as published by the Free Software |
|
7
|
|
|
# Foundation, version 2. |
|
8
|
|
|
# |
|
9
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT |
|
10
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
11
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
|
12
|
|
|
# details. |
|
13
|
|
|
# |
|
14
|
|
|
# You should have received a copy of the GNU General Public License along with |
|
15
|
|
|
# this program; if not, write to the Free Software Foundation, Inc., 51 |
|
16
|
|
|
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
|
17
|
|
|
# |
|
18
|
|
|
# Copyright 2018-2025 by it's authors. |
|
19
|
|
|
# Some rights reserved, see README and LICENSE. |
|
20
|
|
|
|
|
21
|
|
|
from bika.lims import api |
|
22
|
|
|
from zope.i18n import translate as ztranslate |
|
23
|
|
|
from zope.i18nmessageid import MessageFactory |
|
24
|
|
|
|
|
25
|
|
|
_pl = MessageFactory("plonelocales") |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
def translate(msgid, to_utf8=True, **kwargs): |
|
29
|
|
|
"""Translate any zope i18n msgid |
|
30
|
|
|
|
|
31
|
|
|
If msgid is from type i18n `Message`, the domain assigned to the msg has |
|
32
|
|
|
priority over the domain passed through kwargs. If no domain is set nor in |
|
33
|
|
|
kwargs neither in msgid, system defaults to "senaite.core" |
|
34
|
|
|
|
|
35
|
|
|
:param msgid: i18n message id or Message to translate |
|
36
|
|
|
:param to_utf8: whether the translated message should be encoded to utf8 |
|
37
|
|
|
:returns: the translated string for msgid |
|
38
|
|
|
""" |
|
39
|
|
|
msgid = api.safe_unicode(msgid) |
|
40
|
|
|
|
|
41
|
|
|
# XX: If the msgid is from type `Message`, Zope's i18n translate tool gives |
|
42
|
|
|
# priority `Message.domain` over the domain passed through kwargs |
|
43
|
|
|
domain = kwargs.pop("domain", "senaite.core") |
|
44
|
|
|
params = { |
|
45
|
|
|
"domain": getattr(msgid, "domain", domain), |
|
46
|
|
|
"context": api.get_request(), |
|
47
|
|
|
} |
|
48
|
|
|
params.update(kwargs) |
|
49
|
|
|
|
|
50
|
|
|
message = ztranslate(msgid, **params) |
|
51
|
|
|
return api.to_utf8(message) if to_utf8 else message |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
def get_dt_format(msgid): |
|
55
|
|
|
"""Returns the date/time msgstr format for the current locale |
|
56
|
|
|
:param id: locale msgid or "date"/"time"/"datetime" |
|
57
|
|
|
""" |
|
58
|
|
|
mapping = { |
|
59
|
|
|
"date": "date_format_short", |
|
60
|
|
|
"datetime": "date_format_long", |
|
61
|
|
|
"time": "time_format", |
|
62
|
|
|
} |
|
63
|
|
|
defaults = { |
|
64
|
|
|
"time_format": "${H}:${M}", |
|
65
|
|
|
"date_format_short": "${Y}-${m}-${d}", |
|
66
|
|
|
"date_format_long": "${Y}-${m}-${d} ${H}:${M}", |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
# extract the current locale for the given msgid from TranslationService |
|
70
|
|
|
msgid = mapping.get(msgid, msgid) |
|
71
|
|
|
fmt = translate(msgid, to_utf8=False) |
|
72
|
|
|
if not fmt or fmt == msgid: |
|
73
|
|
|
return defaults.get(msgid) |
|
74
|
|
|
return fmt |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
def get_weekday_name(day, abbr=False, to_utf8=True): |
|
78
|
|
|
"""Returns the name of the day of the week for the current locale, starting |
|
79
|
|
|
with Sunday == 0 |
|
80
|
|
|
""" |
|
81
|
|
|
ids = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"] |
|
82
|
|
|
msgid = "weekday_%s_abbr" if abbr else "weekday_%s" |
|
83
|
|
|
msgid = msgid % ids[day] |
|
84
|
|
|
return translate(_pl(msgid), to_utf8=to_utf8) |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
def get_month_name(month, abbr=False, to_utf8=True): |
|
88
|
|
|
"""Returns the name of the month for the current locale, starting with |
|
89
|
|
|
""" |
|
90
|
|
|
ids = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", |
|
91
|
|
|
"oct", "nov", "dec"] |
|
92
|
|
|
msgid = "month_%s_abbr" if abbr else "month_%s" |
|
93
|
|
|
msgid = msgid % ids[month-1] |
|
94
|
|
|
return translate(_pl(msgid), to_utf8=to_utf8) |
|
95
|
|
|
|