From e974e1aec889556033acd7f40b73167e56916f77 Mon Sep 17 00:00:00 2001
From: Toufik Ben Jaa <tbe@odoo.com>
Date: Mon, 12 Aug 2019 09:01:52 +0000
Subject: [PATCH] [FIX] tools: always retrieve a langage on formatLang

- Since commit https://github.com/odoo/odoo/commit/f5cd483216af01001f44a41f7d48f042b9f56ff9
the method `_lang_get` doesn't fallback on a language if the asked language
isn't found.

This causes issues with `formatLang` that uses the language on the
context, which is the language set on the client browser (in most cases).

Meaning, if 'en_US' is the only installed language and a customer
has set his browser to `fr_FR`, when calling `formatLang` this will
crash since no language has been found and `format` on `res.lang`
requires a singleton.

closes odoo/odoo#35633

Signed-off-by: Toufik Benjaa (tbe) <tbe@odoo.com>
---
 odoo/tools/misc.py | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/odoo/tools/misc.py b/odoo/tools/misc.py
index 7e6771060c63..ad51c5b74d00 100644
--- a/odoo/tools/misc.py
+++ b/odoo/tools/misc.py
@@ -1150,8 +1150,9 @@ def formatLang(env, value, digits=None, grouping=True, monetary=False, dp=False,
     if isinstance(value, str) and not value:
         return ''
 
-    lang = env.context.get('lang') or env.user.company_id.partner_id.lang or 'en_US'
-    lang_obj = env['res.lang']._lang_get(lang)
+    langs = [code for code, _ in env['res.lang'].get_installed()]
+    lang_code = env.context['lang'] if env.context.get('lang') in langs else (env.user.company_id.partner_id.lang or langs[0])
+    lang_obj = env['res.lang']._lang_get(lang_code)
 
     res = lang_obj.format('%.' + str(digits) + 'f', value, grouping=grouping, monetary=monetary)
 
-- 
GitLab