Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
ICU Format
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Package Registry
Operate
Terraform modules
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
XCG
Odoo modules
ICU Format
Commits
02eab9709d6d
Commit
02eab9709d6d
authored
4 years ago
by
Vincent Hatakeyama
Browse files
Options
Downloads
Patches
Plain Diff
add a list formatter and a locale cache
parent
d766745524f8
No related branches found
No related tags found
1 merge request
!2
✨ add a list formatter and a locale cache
Pipeline
#15059
passed
3 years ago
Changes
4
Pipelines
3
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
__init__.py
+1
-1
1 addition, 1 deletion
__init__.py
hooks.py
+7
-1
7 additions, 1 deletion
hooks.py
icu_format.py
+32
-3
32 additions, 3 deletions
icu_format.py
tests/test_icuformat.py
+11
-1
11 additions, 1 deletion
tests/test_icuformat.py
with
51 additions
and
6 deletions
__init__.py
+
1
−
1
View file @
02eab970
from
.hooks
import
post_load
# noqa: F401
from
.icu_format
import
icu_format
# noqa: F401
from
.icu_format
import
icu_format
,
icu_list_format
# noqa: F401
from
.logger
import
get_logger
# noqa: F401
This diff is collapsed.
Click to expand it.
hooks.py
+
7
−
1
View file @
02eab970
...
...
@@ -24,7 +24,7 @@
from
odoo
import
_
from
odoo.models
import
BaseModel
from
.icu_format
import
icu_format
from
.icu_format
import
icu_format
,
icu_list_format
from
babel.messages
import
extract
from
icu
import
ICUError
...
...
@@ -90,5 +90,10 @@
)
return
icu_format
(
"
C
"
,
untranslated_message
,
args
,
raise_exception
)
def
model_icu_list_format
(
self
,
list_data
:
list
)
->
str
:
return
icu_list_format
(
self
.
env
.
context
.
get
(
"
lang
"
)
or
self
.
env
.
user
.
lang
,
list_data
)
setattr
(
BaseModel
,
"
icu_format
"
,
model_icu_format
)
setattr
(
BaseModel
,
ICU_FORMAT_TRANSLATE
,
model_icu_format_translate
)
...
...
@@ -93,5 +98,6 @@
setattr
(
BaseModel
,
"
icu_format
"
,
model_icu_format
)
setattr
(
BaseModel
,
ICU_FORMAT_TRANSLATE
,
model_icu_format_translate
)
setattr
(
BaseModel
,
"
icu_list_format
"
,
model_icu_list_format
)
def
__patch_babel_extract
():
...
...
This diff is collapsed.
Click to expand it.
icu_format.py
+
32
−
3
View file @
02eab970
...
...
@@ -20,7 +20,21 @@
import
datetime
from
typing
import
Dict
,
Optional
,
Union
from
icu
import
Formattable
,
ICUError
,
Locale
,
MessageFormat
from
icu
import
Formattable
,
ICUError
,
ListFormatter
,
Locale
,
MessageFormat
class
Cache
:
def
__init__
(
self
,
factory
):
self
.
__data
=
dict
()
self
.
__factory
=
factory
def
__getitem__
(
self
,
item
):
if
item
not
in
self
.
__data
:
self
.
__data
[
item
]
=
self
.
__factory
(
item
)
return
self
.
__data
[
item
]
__locale_cache
=
Cache
(
Locale
)
def
icu_format
(
...
...
@@ -40,7 +54,7 @@
return the original message without formatting
:return: formatted message
"""
# TODO cache the MessageFormat somehow
# TODO cache the MessageFormat somehow
, maybe using @lru_cache
key_list
=
list
()
value_list
=
list
()
for
key
,
value
in
args
.
items
():
...
...
@@ -55,10 +69,10 @@
if
not
lang
:
# Use the neutral locale if nothing is set
lang
=
"
C
"
locale
=
L
ocale
(
lang
)
locale
=
__l
ocale
_cache
[
lang
]
try
:
return
MessageFormat
(
msg
,
locale
).
format
(
key_list
,
value_list
)
except
ICUError
:
if
raise_exception
:
raise
return
msg
...
...
@@ -59,6 +73,21 @@
try
:
return
MessageFormat
(
msg
,
locale
).
format
(
key_list
,
value_list
)
except
ICUError
:
if
raise_exception
:
raise
return
msg
__list_formatter_cache
=
Cache
(
lambda
lang
:
ListFormatter
.
createInstance
(
__locale_cache
[
lang
])
)
def
icu_list_format
(
lang
:
str
,
list_data
:
list
)
->
str
:
"""
Format a list using ICU ListFormatter
:param lang: the lang to use
:param list_data: The list of elements to format
:return: formatted message
"""
return
__list_formatter_cache
[
lang
].
format
(
list_data
)
This diff is collapsed.
Click to expand it.
tests/test_icuformat.py
+
11
−
1
View file @
02eab970
...
...
@@ -21,7 +21,7 @@
from
odoo
import
tests
from
odoo.addons.icuformat
import
icu_format
from
odoo.addons.icuformat
import
icu_format
,
icu_list_format
class
Test
(
tests
.
TransactionCase
):
...
...
@@ -112,3 +112,13 @@
self
.
assertEqual
(
"
A message
"
,
user
.
icu_format_translate
(
"
A message
"
,
dict
())
)
def
test_list_format
(
self
):
"""
Test the list formatter provided on BaseModel.
"""
self
.
assertEqual
(
icu_list_format
(
"
fr
"
,
[
"
a
"
,
"
b
"
,
"
c
"
]),
"
a, b et c
"
)
self
.
assertEqual
(
icu_list_format
(
"
en_GB
"
,
[
"
1
"
,
"
2
"
,
"
5
"
]),
"
1, 2 and 5
"
)
user
=
self
.
env
[
"
res.users
"
].
search
([],
limit
=
1
)
self
.
assertEqual
(
user
.
icu_list_format
([
"
1
"
,
"
2
"
,
"
5
"
]),
"
1, 2, and 5
"
)
This diff is collapsed.
Click to expand it.
Vincent Hatakeyama
@vincent.hatakeyama
mentioned in commit
b338b7afd5bd
·
3 years ago
mentioned in commit
b338b7afd5bd
mentioned in commit b338b7afd5bd6c9e8a3db057366f8bbb6700a69a
Toggle commit list
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment