-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added i18n translate function from senaite.core
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# This file is part of SENAITE.PATIENT. | ||
# | ||
# SENAITE.PATIENT is free software: you can redistribute it and/or modify it | ||
# under the terms of the GNU General Public License as published by the Free | ||
# Software Foundation, version 2. | ||
# | ||
# This program is distributed in the hope that it will be useful, but WITHOUT | ||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
# details. | ||
# | ||
# You should have received a copy of the GNU General Public License along with | ||
# this program; if not, write to the Free Software Foundation, Inc., 51 | ||
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
# | ||
# Copyright 2020-2022 by it's authors. | ||
# Some rights reserved, see README and LICENSE. | ||
|
||
from bika.lims import api | ||
from zope.i18n import translate as ztranslate | ||
|
||
|
||
def translate(msgid, to_utf8=True, **kwargs): | ||
"""Translate any zope i18n msgid | ||
If msgid is from type i18n `Message`, the domain assigned to the msg has | ||
priority over the domain passed through kwargs. If no domain is set nor in | ||
kwargs neither in msgid, system defaults to "senaite.core" | ||
:param msgid: i18n message id or Message to translate | ||
:param to_utf8: whether the translated message should be encoded to utf8 | ||
:returns: the translated string for msgid | ||
""" | ||
msgid = api.safe_unicode(msgid) | ||
|
||
# XX: If the msgid is from type `Message`, Zope's i18n translate tool gives | ||
# priority `Message.domain` over the domain passed through kwargs | ||
domain = kwargs.pop("domain", "senaite.core") | ||
params = { | ||
"domain": getattr(msgid, "domain", domain), | ||
"context": api.get_request(), | ||
} | ||
params.update(kwargs) | ||
|
||
message = ztranslate(msgid, **params) | ||
return api.to_utf8(message) if to_utf8 else message |