Passed
Push — 2.x ( 42e450...4bf470 )
by Ramon
06:42
created

senaite.core.i18n   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 12
dl 0
loc 49
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A translate() 0 25 2
1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of SENAITE.PATIENT.
4
#
5
# SENAITE.PATIENT is free software: you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by the Free
7
# Software 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 2020-2022 by it's authors.
19
# Some rights reserved, see README and LICENSE.
20
21
from bika.lims import api
22
23
24
def translate(msgid, to_utf8=True, **kwargs):
25
    """Translate any zope i18n msgid
26
27
    If msgid is from type i18n `Message`, the domain assigned to the msg has
28
    priority over the domain passed through kwargs. If no domain is set nor in
29
    kwargs neither in msgid, system defaults to "senaite.core"
30
31
    :param msgid: i18n message id or Message to translate
32
    :param to_utf8: whether the translated message should be encoded to utf8
33
    :returns: the translated string for msgid
34
    """
35
    msgid = api.safe_unicode(msgid)
36
37
    # XX: If the msgid is from type `Message`, Zope's i18n translate tool gives
38
    #     priority `Message.domain` over the domain passed through kwargs
39
    domain = kwargs.pop("domain", "senaite.core")
40
    params = {
41
        "domain": getattr(msgid, "domain", domain),
42
        "context": api.get_request(),
43
    }
44
    params.update(kwargs)
45
46
    ts = api.get_tool("translation_service")
47
    message = ts.translate(msgid, **params)
48
    return api.to_utf8(message) if to_utf8 else message
49