From 2b19b69f91a7b826ce3e9dfa7e97e1e3e45db799 Mon Sep 17 00:00:00 2001
From: Aline Gondim Santos <aline.gondimsantos@savoirfairelinux.com>
Date: Mon, 8 Aug 2022 12:15:18 -0300
Subject: [PATCH] plugins: use JAMI_LANG variable

To apply translations, first try to read if the JAMI_LANG variable is
set, if not, try to get the system language.

GitLab: #747

Change-Id: Ie458abcc07c0d0fd151172e172fe1418e5f06e7f
---
 src/plugin/pluginpreferencesutils.cpp | 44 ++++++++++++++++-----------
 test/unitTest/plugins/plugins.cpp     | 27 +++++++++++++---
 2 files changed, 49 insertions(+), 22 deletions(-)

diff --git a/src/plugin/pluginpreferencesutils.cpp b/src/plugin/pluginpreferencesutils.cpp
index 03ae4498c7..9671f85ac9 100644
--- a/src/plugin/pluginpreferencesutils.cpp
+++ b/src/plugin/pluginpreferencesutils.cpp
@@ -159,26 +159,34 @@ PluginPreferencesUtils::getPreferences(const std::string& rootPath, const std::s
     std::vector<std::map<std::string, std::string>> preferences;
     if (file) {
         // Get preferences locale
-#ifdef WIN32
         std::string lang;
-        WCHAR localeBuffer[LOCALE_NAME_MAX_LENGTH];
-        if (GetUserDefaultLocaleName(localeBuffer, LOCALE_NAME_MAX_LENGTH) != 0) {
-            char utf8Buffer[LOCALE_NAME_MAX_LENGTH] {};
-            WideCharToMultiByte(CP_UTF8,
-                                0,
-                                localeBuffer,
-                                LOCALE_NAME_MAX_LENGTH,
-                                utf8Buffer,
-                                LOCALE_NAME_MAX_LENGTH,
-                                nullptr,
-                                nullptr);
-
-            lang.append(utf8Buffer);
-            string_replace(lang, "-", "_");
-        }
+        if (auto envLang = std::getenv("JAMI_LANG"))
+            lang = envLang;
+        else
+            JAMI_ERR() << "Error getting JAMI_LANG env, trying to get system language";
+        // If language preference is empty, try to get from the system.
+        if (lang.empty()) {
+#ifdef WIN32
+            WCHAR localeBuffer[LOCALE_NAME_MAX_LENGTH];
+            if (GetUserDefaultLocaleName(localeBuffer, LOCALE_NAME_MAX_LENGTH) != 0) {
+                char utf8Buffer[LOCALE_NAME_MAX_LENGTH] {};
+                WideCharToMultiByte(CP_UTF8,
+                                    0,
+                                    localeBuffer,
+                                    LOCALE_NAME_MAX_LENGTH,
+                                    utf8Buffer,
+                                    LOCALE_NAME_MAX_LENGTH,
+                                    nullptr,
+                                    nullptr);
+
+                lang.append(utf8Buffer);
+                string_replace(lang, "-", "_");
+            }
 #else
-        std::string lang = std::locale("").name();
-#endif //
+            // For Android this should not work since std::locale is not supported by the NDK.
+            lang = std::locale("").name();
+#endif // WIN32
+        }
         auto locales = getLocales(rootPath, std::string(string_remove_suffix(lang, '.')));
 
         // Read the file to a json format
diff --git a/test/unitTest/plugins/plugins.cpp b/test/unitTest/plugins/plugins.cpp
index a74a1b87be..72dcf359e4 100644
--- a/test/unitTest/plugins/plugins.cpp
+++ b/test/unitTest/plugins/plugins.cpp
@@ -119,6 +119,7 @@ private:
     void testInstallAndLoad();
     void testHandlers();
     void testDetailsAndPreferences();
+    void testTranslations();
     void testCall();
     void testMessage();
 
@@ -127,6 +128,7 @@ private:
     CPPUNIT_TEST(testInstallAndLoad);
     CPPUNIT_TEST(testHandlers);
     CPPUNIT_TEST(testDetailsAndPreferences);
+    CPPUNIT_TEST(testTranslations);
     CPPUNIT_TEST(testCall);
     CPPUNIT_TEST(testMessage);
     CPPUNIT_TEST_SUITE_END();
@@ -388,8 +390,6 @@ PluginsTest::testDetailsAndPreferences()
     CPPUNIT_ASSERT(preferencesValuesOrig[key] == preferencesValuesNew[key]);
     CPPUNIT_ASSERT(preferencesValuesNew[key] != preferenceNewValue);
 
-
-
     // Get-set-reset - alice account
     preferences = Manager::instance().getJamiPluginManager().getPluginPreferences(installationPath_, aliceData.accountId_);
     CPPUNIT_ASSERT(!preferences.empty());
@@ -428,11 +428,30 @@ PluginsTest::testDetailsAndPreferences()
     CPPUNIT_ASSERT(preferencesValuesNew[key] == preferencesValuesBobOrig[key]);
     CPPUNIT_ASSERT(preferencesValuesNew[key] != preferenceNewValue);
 
-    // Test translations
-
     CPPUNIT_ASSERT(!Manager::instance().getJamiPluginManager().uninstallPlugin(installationPath_));
 }
 
+void
+PluginsTest::testTranslations()
+{
+    Manager::instance().pluginPreferences.setPluginsEnabled(true);
+    setenv("JAMI_LANG", "en", true);
+    Manager::instance().getJamiPluginManager().installPlugin(jplPath_, true);
+
+    auto preferences = Manager::instance().getJamiPluginManager().getPluginPreferences(installationPath_, "");
+    CPPUNIT_ASSERT(!preferences.empty());
+    auto preferencesValuesEN = Manager::instance().getJamiPluginManager().getPluginPreferencesValuesMap(installationPath_, "");
+
+    setenv("JAMI_LANG", "fr", true);
+
+    CPPUNIT_ASSERT(Manager::instance().getJamiPluginManager().getPluginPreferencesValuesMap(installationPath_, "") != preferencesValuesEN);
+
+    setenv("JAMI_LANG", "en", true);
+
+    CPPUNIT_ASSERT(Manager::instance().getJamiPluginManager().getPluginPreferencesValuesMap(installationPath_, "") == preferencesValuesEN);
+
+    CPPUNIT_ASSERT(!Manager::instance().getJamiPluginManager().uninstallPlugin(installationPath_));
+}
 
 bool
 PluginsTest::waitForSignal(CallData& callData,
-- 
GitLab