Skip to content
Permalink
Browse files

[WIP] improve the code

  • Loading branch information...
mba-odoo committed Apr 16, 2019
1 parent f57b220 commit 44698ba6a5a048eb09b586a6933f03d3f6709caa
@@ -138,19 +138,20 @@ def url_for(path_or_uri, lang=None):
if not lang:
lang = pycompat.to_text(request.context.get('lang') or 'en_US')
lang = request.env['ir.http']._get_languages().filtered(lambda l: l.code == lang).request_lang
langs = [lg[0] for lg in request.env['ir.http']._get_language_codes()]
langs = [lg[1] for lg in request.env['ir.http']._get_language_codes()]

if (len(langs) > 1 or force_lang) and is_multilang_url(location, langs):
ps = location.split(u'/')
def_request_lang = request.env['ir.http']._get_default_lang().request_lang
if ps[1] in langs:
# Replace the language only if we explicitly provide a language to url_for
if force_lang:
ps[1] = lang
# Remove the default language unless it's explicitly provided
elif ps[1] == request.env['ir.http']._get_default_lang().request_lang:
elif ps[1] == def_request_lang:
ps.pop(1)
# Insert the context language or the provided language
elif lang != request.env['ir.http']._get_default_lang().request_lang or force_lang:
elif lang != def_request_lang or force_lang:
ps.insert(1, lang)
location = u'/'.join(ps)

@@ -159,7 +160,7 @@ def url_for(path_or_uri, lang=None):

def is_multilang_url(local_url, langs=None):
if not langs:
langs = [lg[0] for lg in request.env['ir.http']._get_language_codes()]
langs = [lg[1] for lg in request.env['ir.http']._get_language_codes()]
spath = local_url.split('/')
# if a language is already in the path, remove it
if spath[1] in langs:
@@ -229,7 +230,7 @@ def _get_languages(cls):
@classmethod
def _get_language_codes(cls):
languages = cls._get_languages()
return [(lang.code, lang.name) for lang in languages]
return [(lang.code, lang.request_lang, lang.name) for lang in languages]

@classmethod
def _get_default_lang(cls):
@@ -262,9 +263,9 @@ def get_nearest_lang(cls, lang):
# Try to find a similar lang. Eg: fr_BE and fr_FR
short = lang.partition('_')[0]
short_match = False
for code, dummy in cls._get_language_codes():
if code == lang:
return lang
for code, iso_code, dummy in cls._get_language_codes():
if code == lang or iso_code == lang:
return iso_code
if not short_match and code.startswith(short):
short_match = code
return short_match
@@ -302,10 +303,9 @@ def _add_dispatch_parameters(cls, func):
or (not is_a_bot and cls.get_nearest_lang(request.lang))
or cls._get_default_lang().request_lang)
lang = nearest_lang or preferred_lang
lang_code = cls._get_languages().filtered(lambda l: l.request_lang == lang)

request.lang = lang
context['lang'] = lang_code.code
context['lang'] = cls._get_languages().filtered(lambda l: l.request_lang == lang).code
# bind modified context
request.context = context

@@ -438,8 +438,8 @@ def _postprocess_args(cls, arguments, rule):
if getattr(request, 'is_frontend_multilang', False) and request.httprequest.method in ('GET', 'HEAD'):
generated_path = werkzeug.url_unquote_plus(path)
current_path = werkzeug.url_unquote_plus(request.httprequest.path)
def_lang_code = cls._get_default_lang().request_lang
if generated_path != current_path:
def_lang_code = cls._get_default_lang().request_lang
if request.lang != def_lang_code:
path = '/' + request.lang + path
if request.httprequest.query_string:
@@ -11,4 +11,4 @@ def _get_request_lang(self):
for lang in self:
lang.request_lang = lang.iso_code

request_lang = fields.Char(string='Request Lang code', compute='_get_request_lang')
request_lang = fields.Char(compute='_get_request_lang', string='Request Lang code')
@@ -411,12 +411,7 @@ def get_languages(self):

@tools.cache('self.id')
def _get_languages(self):
return [(lg.request_lang, lg.name) for lg in self.language_ids]

@api.multi
def get_language_code(self):
self.ensure_one()
return [(lg.code, lg.name) for lg in self.language_ids]
return [(lg.code, lg.request_lang, lg.name) for lg in self.language_ids]

@api.multi
def get_alternate_languages(self, req=None):
@@ -434,11 +429,11 @@ def get_url_localized(router, lang):
return router.build(request.endpoint, arguments)

router = request.httprequest.app.get_db_router(request.db).bind('')
for code, dummy in self.get_languages():
lg_path = ('/' + code) if code != default else ''
for code, iso_code, dummy in self.get_languages():
lg_path = ('/' + iso_code) if iso_code != default else ''
lg_codes = code.split('_')
shorts.append(lg_codes[0])
uri = get_url_localized(router, code) if request.endpoint else request.httprequest.path
uri = get_url_localized(router, iso_code) if request.endpoint else request.httprequest.path
if req.query_string:
uri += u'?' + req.query_string.decode('utf-8')
lang = {
@@ -143,7 +143,6 @@ def test_process_att_monolang_route(self):
with MockRequest(self.env, website=self.website, multilang=False) as request:
# lang not changed in URL but CDN enabled
self._test_att('/a', {'href': 'http://test.cdn/a'})
# import pdb; pdb.set_trace()
self._test_att('/en/a', {'href': 'http://test.cdn/en/a'})
self._test_att('/b', {'href': 'http://test.cdn/b'})
self._test_att('/en/b', {'href': '/en/b'})
@@ -404,16 +404,16 @@
<li class="list-inline-item">
<div class="dropup">
<button class="btn btn-sm btn-secondary dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
<span t-esc="list(filter(lambda lg : lg[0] == lang, website.get_language_code()))[0][1].split('/').pop()"/>
<span t-esc="list(filter(lambda lg : lg[0] == lang, languages))[0][2].split('/').pop()"/>
<span class="caret ml4"/>
</button>
<div class="dropdown-menu" role="menu">
<t t-foreach="languages" t-as="lg">
<a t-att-href="url_for(request.httprequest.path + '?' + keep_query(), lang=lg[0])"
<a t-att-href="url_for(request.httprequest.path + '?' + keep_query(), lang=lg[1])"
class="dropdown-item js_change_lang"
t-att-data-default-lang="(editable or translatable) and 'true' if website and lg[0] == website.default_website_lang_code else None"
t-att-data-lang="lg[0]">
<t t-esc="lg[1].split('/').pop()"/>
t-att-data-default-lang="(editable or translatable) and 'true' if website and lg[1] == website.default_website_lang_code else None"
t-att-data-lang="lg[1]">
<t t-esc="lg[2].split('/').pop()"/>
</a>
</t>
</div>

0 comments on commit 44698ba

Please sign in to comment.
You can’t perform that action at this time.