Remove sub-locale to prevent Babel and Zope exceptions for unknown locales
authorThierry Florac <thierry.florac@onf.fr>
Mon, 07 Dec 2015 12:00:41 +0100
changeset 20 62e443d3c3d9
parent 19 1b8004d2f47a
child 21 a801552ffad0
Remove sub-locale to prevent Babel and Zope exceptions for unknown locales
src/pyams_i18n/negotiator.py
--- a/src/pyams_i18n/negotiator.py	Thu Oct 08 09:27:36 2015 +0200
+++ b/src/pyams_i18n/negotiator.py	Mon Dec 07 12:00:41 2015 +0100
@@ -86,8 +86,13 @@
     """Negotiate language based on server, browser and user settings"""
     negotiator = query_utility(INegotiator)
     if negotiator is not None:
-        return negotiator.get_language(request)
-    return get_browser_language(request)
+        locale_name = negotiator.get_language(request)
+    else:
+        locale_name = get_browser_language(request)
+    if '-' in locale_name:
+        # remove 'sub-locale' to prevent Babel and Zope exceptions for unknown locales
+        locale_name = locale_name.split('-')[0]
+    return locale_name
 
 
 def get_locale(request):