Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
erp5
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alexander Emmerich
erp5
Commits
9d7f2574
Commit
9d7f2574
authored
Oct 30, 2012
by
Jérome Perrin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Localizer: add a context manager to temporarily change language
parent
0f5ff0f6
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
92 additions
and
1 deletion
+92
-1
product/ERP5Type/tests/testLocalizer.py
product/ERP5Type/tests/testLocalizer.py
+58
-0
product/Localizer/Localizer.py
product/Localizer/Localizer.py
+34
-1
No files found.
product/ERP5Type/tests/testLocalizer.py
View file @
9d7f2574
...
...
@@ -127,6 +127,64 @@ class TestLocalizer(ERP5TypeTestCase):
self
.
portal
.
portal_activities
.
process_timer
(
None
,
None
)
self
.
assertEquals
(
tmp_obj
.
getComment
(),
"C'est 1€."
)
def
test_get_selected_language
(
self
):
# default selected language is en
self
.
assertEquals
(
'en'
,
self
.
portal
.
Localizer
.
get_selected_language
())
def
test_translationContext
(
self
):
self
.
message_catalog
.
_messages
[
'This is 1€.'
]
=
PersistentMapping
(
{
'fr'
:
"C'est 1€."
,
'note'
:
''
,})
localizer
=
self
.
portal
.
Localizer
with
localizer
.
translationContext
(
'fr'
):
self
.
assertEquals
(
'fr'
,
localizer
.
get_selected_language
())
self
.
assertEquals
(
"C'est 1€."
,
self
.
portal
.
Base_translateString
(
"This is 1€."
))
# outside of this context manager we are back to english
self
.
assertEquals
(
'en'
,
localizer
.
get_selected_language
())
self
.
assertEquals
(
"This is 1€."
,
self
.
portal
.
Base_translateString
(
"This is 1€."
))
def
test_translationContextActivity
(
self
):
portal
=
self
.
portal
self
.
message_catalog
.
_messages
[
'This is 1€.'
]
=
PersistentMapping
(
{
'fr'
:
"C'est 1€."
,
'note'
:
''
,})
localizer
=
portal
.
Localizer
test_script
=
createZODBPythonScript
(
portal
.
portal_skins
.
custom
,
'test_script'
,
''
,
"""
def assertEquals(a, b):
if a != b:
raise AssertionError("%r != %r" % (a, b))
localizer = context.getPortalObject().Localizer
with localizer.translationContext('fr'):
assertEquals('fr', localizer.get_selected_language())
assertEquals("C'est 1€.", context.Base_translateString("This is 1€."))
# outside of this context manager we are back to english
assertEquals('en', localizer.get_selected_language())
assertEquals("This is 1€.", context.Base_translateString("This is 1€."))
"""
)
# normal activity
portal
.
portal_activities
.
activate
().
test_script
()
self
.
tic
()
# after activity execution we are still in english
self
.
assertEquals
(
'en'
,
localizer
.
get_selected_language
())
self
.
assertEquals
(
"This is 1€."
,
self
.
portal
.
Base_translateString
(
"This is 1€."
))
# execute activity with group_method
portal
.
portal_activities
.
activate
(
group_method_id
=
None
).
test_script
()
self
.
tic
()
# after activity execution we are still in english
self
.
assertEquals
(
'en'
,
localizer
.
get_selected_language
())
self
.
assertEquals
(
"This is 1€."
,
self
.
portal
.
Base_translateString
(
"This is 1€."
))
def
test_get_request
(
self
):
# check that Localizer's get_request hack works as expected
from
Products.Localizer.utils
import
get_request
self
.
assertEquals
(
get_request
(),
self
.
portal
.
REQUEST
)
def
test_suite
():
suite
=
unittest
.
TestSuite
()
suite
.
addTest
(
unittest
.
makeSuite
(
TestLocalizer
))
...
...
product/Localizer/Localizer.py
View file @
9d7f2574
...
...
@@ -16,6 +16,7 @@
# Import from the Standard Library
from
urllib
import
unquote
from
contextlib
import
contextmanager
# Import from Zope
from
AccessControl
import
ClassSecurityInfo
...
...
@@ -216,7 +217,10 @@ class Localizer(LanguageManager, Folder):
security
.
declarePublic
(
'changeLanguage'
)
changeLanguageForm
=
LocalDTMLFile
(
'ui/changeLanguageForm'
,
globals
())
def
changeLanguage
(
self
,
lang
,
goto
=
None
,
expires
=
None
):
""" """
"""Change the user language to `lang`.
This method will set a cookie and redirect to `goto` URL.
"""
request
=
self
.
REQUEST
response
=
request
.
RESPONSE
...
...
@@ -234,6 +238,35 @@ class Localizer(LanguageManager, Folder):
response
.
redirect
(
goto
)
security
.
declarePublic
(
'translationContext'
)
@
contextmanager
def
translationContext
(
self
,
lang
):
"""Context manager to temporarily change the current language.
"""
class
ForcedLanguage
:
__allow_access_to_unprotected_subobjects__
=
1
def
__init__
(
self
,
lang
):
self
.
lang
=
lang
def
select_language
(
self
,
available_languages
):
return
self
.
lang
def
set
(
self
,
lang
,
priority
):
if
lang
!=
self
.
lang
:
LOG
(
'Localizer'
,
PROBLEM
,
'Cannot change language inside a translationContext'
,
error
=
1
)
MARKER
=
[]
from
patches
import
get_request
# late import, as this is patched by
# unit tests
request
=
get_request
()
# Localizer always use this request internally
old_accept_language
=
request
.
get
(
'AcceptLanguage'
,
MARKER
)
request
.
set
(
'AcceptLanguage'
,
ForcedLanguage
(
lang
))
try
:
assert
self
.
get_selected_language
()
==
lang
yield
finally
:
request
.
other
.
pop
(
'AcceptLanguage'
)
if
old_accept_language
is
not
MARKER
:
request
.
set
(
'AcceptLanguage'
,
old_accept_language
)
security
.
declarePublic
(
'translate'
)
def
translate
(
self
,
domain
,
msgid
,
lang
=
None
,
*
args
,
**
kw
):
"""
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment