Skip to content
Snippets Groups Projects
icuformat.py 1.59 KiB
Newer Older
##############################################################################
#
#    Copyright (C) 2019 XCG Consulting <http://odoo.consulting>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    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 Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from typing import Dict, Union

from odoo.models import BaseModel

from icu import Formattable, Locale, MessageFormat


def icuformat(lang, msg: str, args: Dict[str, Union[int, str]]) -> str:
    key_list = list()
    value_list = list()
    for key, value in args.items():
        key_list.append(key)
        value_list.append(Formattable(value))
    if not lang:
        lang = 'en'
    locale = Locale(lang)
    return MessageFormat(msg, locale).format(key_list, value_list)


def _icuformat(self, msg: str, args: Dict[str, Union[int, str]]) -> str:
    return icuformat(
        self.env.context.get('lang') or self.env.user.lang,
        msg,
        args,
    )


setattr(BaseModel, 'icuformat', _icuformat)