Skip to content
Snippets Groups Projects
Commit 3d2a2e974645 authored by Vincent Hatakeyama's avatar Vincent Hatakeyama
Browse files

:sparkles: New module with handy method to format using ICU library.

parents
No related branches found
No related tags found
No related merge requests found
This diff is collapsed.
from .icuformat import icuformat # noqa: F401
##############################################################################
#
# 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/>.
#
##############################################################################
{
'name': 'ICU Format',
'summary': 'Format message using ICU',
'version': '11.0.0.1',
'category': 'Technical',
'author': 'XCG Consulting',
'website': 'http://odoo.consulting/',
'license': 'AGPL-3',
'depends': [
'base',
],
'external_dependencies': {
'python': [
'PyICU',
],
},
'installable': True,
}
##############################################################################
#
# 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 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)
from . import test_icuformat # noqa: F401
###############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2019 XCG Consulting (http://www.xcg-consulting.fr/)
#
# 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 odoo import tests
from odoo.addons.icuformat import icuformat
class Test(tests.TransactionCase):
def test_simple(self):
msg = '{test}'
d = {'test': 'foobar'}
expected = 'foobar'
self.assertEqual(expected, icuformat('en', msg, d))
def test_nothing_to_format(self):
msg = 'test'
d = dict()
self.assertEqual(msg, icuformat('en', msg, d))
def test_nested_plural_select(self):
msg = (
"{gender_of_host, select, "
"female {"
"{num_guests, plural, offset:1 "
"=0 {{host} does not give a party.}"
"=1 {{host} invites {guest} to her party.}"
"=2 {{host} invites {guest} and one other person to her party.}"
"other {{host} invites {guest} and # other people to her party.}}}"
"male {"
"{num_guests, plural, offset:1 "
"=0 {{host} does not give a party.}"
"=1 {{host} invites {guest} to his party.}"
"=2 {{host} invites {guest} and one other person to his party.}"
"other {{host} invites {guest} and # other people to his party.}}}"
"other {"
"{num_guests, plural, offset:1 "
"=0 {{host} does not give a party.}"
"=1 {{host} invites {guest} to their party.}"
"=2 {{host} invites {guest} and one other person to their party.}"
"other {{host} invites {guest} and # other people to their party.}"
"}}}"
)
d = dict(
gender_of_host='female',
num_guests=1005,
host='Ms. Marple',
guest='Robert')
expected = (
'Ms. Marple invites Robert and 1,004 other people to her party.'
)
self.assertEqual(expected, icuformat('en', msg, d))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment