URL: https://github.com/freeipa/freeipa/pull/1511
Author: Tiboris
 Title: #1511: [Backport][ipa-4-5] Fixing translation problems
Action: opened

PR body:
"""
This PR was opened automatically because PR #1493 was pushed to master and 
backport to ipa-4-5 is required.
"""

To pull the PR as Git branch:
git remote add ghfreeipa https://github.com/freeipa/freeipa
git fetch ghfreeipa pull/1511/head:pr1511
git checkout pr1511
From a6bc27c39dc5064a90cba38dcd659739d243e4ce Mon Sep 17 00:00:00 2001
From: Aleksei Slaikovskii <aslai...@redhat.com>
Date: Tue, 23 Jan 2018 14:41:25 +0100
Subject: [PATCH] Fixing translation problems

ipa rpc server did set the LANG environment variable on each
request and it was not thread safe which led to unpredictable
mixed languages output. Also, there were mistakes regarding
setting the Accept-Language HTTP header.

Now on each request we're setting the "languages" property
in the context thread local variable and client is setting
the Accept-Language HTTP header correctly.

Also, as the server is caching the schema and the schema can
be generated for several languages it's good to store different
schema fingerprint for each language separately.

pagure: https://pagure.io/freeipa/issue/7238
---
 ipaclient/remote_plugins/__init__.py |  6 +++---
 ipalib/rpc.py                        |  4 +++-
 ipaserver/plugins/schema.py          | 13 +++++++++----
 ipaserver/rpcserver.py               | 14 ++++++--------
 4 files changed, 21 insertions(+), 16 deletions(-)

diff --git a/ipaclient/remote_plugins/__init__.py b/ipaclient/remote_plugins/__init__.py
index 63fe39aa85..28ce195893 100644
--- a/ipaclient/remote_plugins/__init__.py
+++ b/ipaclient/remote_plugins/__init__.py
@@ -30,9 +30,9 @@ def __init__(self, api):
 
         # copy-paste from ipalib/rpc.py
         try:
-            self._language = (
-                 locale.setlocale(locale.LC_ALL, '').split('.')[0].lower()
-            )
+            self._language = locale.setlocale(
+                locale.LC_MESSAGES, ''
+            ).split('.')[0].lower()
         except locale.Error:
             self._language = 'en_us'
 
diff --git a/ipalib/rpc.py b/ipalib/rpc.py
index e3b8d67d69..c53eea725d 100644
--- a/ipalib/rpc.py
+++ b/ipalib/rpc.py
@@ -515,7 +515,9 @@ def get_host_info(self, host):
             self, host)
 
         try:
-            lang = locale.setlocale(locale.LC_ALL, '').split('.')[0].lower()
+            lang = locale.setlocale(
+                locale.LC_MESSAGES, ''
+            ).split('.')[0].lower()
         except locale.Error:
             # fallback to default locale
             lang = 'en_us'
diff --git a/ipaserver/plugins/schema.py b/ipaserver/plugins/schema.py
index 8fd7c6ba1c..b554cdc3f3 100644
--- a/ipaserver/plugins/schema.py
+++ b/ipaserver/plugins/schema.py
@@ -16,6 +16,7 @@
 from ipalib.output import Entry, ListOfEntries, ListOfPrimaryKeys, PrimaryKey
 from ipalib.parameters import Bool, Dict, Flag, Str
 from ipalib.plugable import Registry
+from ipalib.request import context
 from ipalib.text import _
 from ipapython.version import API_VERSION
 
@@ -803,11 +804,15 @@ def _generate_schema(self, **kwargs):
         return schema
 
     def execute(self, *args, **kwargs):
-        try:
-            schema = self.api._schema
-        except AttributeError:
+        langs = "".join(getattr(context, "languages", []))
+
+        if getattr(self.api, "_schema", None) is None:
+            setattr(self.api, "_schema", {})
+
+        schema = self.api._schema.get(langs)
+        if schema is None:
             schema = self._generate_schema(**kwargs)
-            setattr(self.api, '_schema', schema)
+            self.api._schema[langs] = schema
 
         schema['ttl'] = SCHEMA_TTL
 
diff --git a/ipaserver/rpcserver.py b/ipaserver/rpcserver.py
index 9efe3c1f4b..c37f621190 100644
--- a/ipaserver/rpcserver.py
+++ b/ipaserver/rpcserver.py
@@ -331,7 +331,6 @@ def wsgi_execute(self, environ):
         result = None
         error = None
         _id = None
-        lang = os.environ['LANG']
         name = None
         args = ()
         options = {}
@@ -346,12 +345,9 @@ def wsgi_execute(self, environ):
             if ('HTTP_ACCEPT_LANGUAGE' in environ):
                 lang_reg_w_q = environ['HTTP_ACCEPT_LANGUAGE'].split(',')[0]
                 lang_reg = lang_reg_w_q.split(';')[0]
-                lang_ = lang_reg.split('-')[0]
-                if '-' in lang_reg:
-                    reg = lang_reg.split('-')[1].upper()
-                else:
-                    reg = lang_.upper()
-                os.environ['LANG'] = '%s_%s' % (lang_, reg)
+                lang = lang_reg.split('-')[0]
+                setattr(context, "languages", [lang])
+
             if (
                 environ.get('CONTENT_TYPE', '').startswith(self.content_type)
                 and environ['REQUEST_METHOD'] == 'POST'
@@ -360,6 +356,7 @@ def wsgi_execute(self, environ):
                 (name, args, options, _id) = self.unmarshal(data)
             else:
                 (name, args, options, _id) = self.simple_unmarshal(environ)
+
             if name in self._system_commands:
                 result = self._system_commands[name](self, *args, **options)
             else:
@@ -375,7 +372,8 @@ def wsgi_execute(self, environ):
             )
             error = InternalError()
         finally:
-            os.environ['LANG'] = lang
+            if hasattr(context, "languages"):
+                delattr(context, "languages")
 
         principal = getattr(context, 'principal', 'UNKNOWN')
         if command is not None:
_______________________________________________
FreeIPA-devel mailing list -- freeipa-devel@lists.fedorahosted.org
To unsubscribe send an email to freeipa-devel-le...@lists.fedorahosted.org

Reply via email to