Skip to content
Permalink
Browse files

[WIP] improve the code

  • Loading branch information...
mba-odoo committed Apr 16, 2019
1 parent f57b220 commit eac33238bd49f02102a35c76f5c5f7d48114cfc4
@@ -138,7 +138,7 @@ 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'/')
@@ -159,7 +159,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 +229,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,8 +262,8 @@ 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:
for code, iso_code, dummy in cls._get_language_codes():
if code == lang or iso_code == lang:
return lang
if not short_match and code.startswith(short):
short_match = 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,7 +429,7 @@ 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():
for code, iso_code, dummy in self.get_languages():
lg_path = ('/' + code) if code != default else ''
lg_codes = code.split('_')
shorts.append(lg_codes[0])
@@ -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 eac3323

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