include/sfx2/lokhelper.hxx     |    4 ++++
 sfx2/source/doc/objstor.cxx    |   33 +++++++++++++++++++++++++++++++++
 sfx2/source/view/lokhelper.cxx |    8 ++++++++
 3 files changed, 45 insertions(+)

New commits:
commit 469ea15f388c8c8fc263324b6be77e5bfa941b71
Author:     Ashod Nakashian <ashod.nakash...@collabora.co.uk>
AuthorDate: Sun May 1 16:52:30 2022 -0400
Commit:     Michael Meeks <michael.me...@collabora.com>
CommitDate: Thu Jul 28 12:53:22 2022 +0200

    sw: restore UI language to en while saving
    
    Because the XML writer used in sw invokes the
    translation logic, which uses the UI language,
    saving can fail in case there are multiple
    views with different langauges. This restores
    the language used for loading before saving
    to avoid such issues.
    
    Signed-off-by: Ashod Nakashian <ashod.nakash...@collabora.co.uk>
    Change-Id: I6675204bb68ea33b1395c779a64ab3e9b339d73e
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/135482
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    Reviewed-by: Michael Meeks <michael.me...@collabora.com>
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/137547

diff --git a/include/sfx2/lokhelper.hxx b/include/sfx2/lokhelper.hxx
index d5c3adbf9d95..d0ca34125354 100644
--- a/include/sfx2/lokhelper.hxx
+++ b/include/sfx2/lokhelper.hxx
@@ -72,6 +72,10 @@ public:
     static void setViewLanguage(int nId, const OUString& rBcp47LanguageTag);
     /// Set the default language for views.
     static void setDefaultLanguage(const OUString& rBcp47LanguageTag);
+    /// Get the language used by the loading view (used for all save 
operations).
+    static const LanguageTag & getLoadLanguage();
+    /// Set the language used by the loading view (used for all save 
operations).
+    static void setLoadLanguage(const OUString& rBcp47LanguageTag);
     /// Set the locale for the given view.
     static void setViewLocale(int nId, const OUString& rBcp47LanguageTag);
     /// Get the device form factor that should be used for a new view.
diff --git a/sfx2/source/doc/objstor.cxx b/sfx2/source/doc/objstor.cxx
index 7ccee4f3a970..cee35fc3443c 100644
--- a/sfx2/source/doc/objstor.cxx
+++ b/sfx2/source/doc/objstor.cxx
@@ -91,6 +91,7 @@
 #include <osl/file.hxx>
 #include <comphelper/scopeguard.hxx>
 #include <comphelper/lok.hxx>
+#include <i18nlangtag/languagetag.hxx>
 
 #include <sfx2/signaturestate.hxx>
 #include <sfx2/app.hxx>
@@ -102,6 +103,7 @@
 #include <sfx2/docfac.hxx>
 #include <appopen.hxx>
 #include <objshimp.hxx>
+#include <sfx2/lokhelper.hxx>
 #include <sfx2/strings.hrc>
 #include <sfx2/sfxsids.hrc>
 #include <sfx2/dispatch.hxx>
@@ -3190,6 +3192,37 @@ bool SfxObjectShell::SaveAsOwnFormat( SfxMedium& rMedium 
)
             pImpl->aBasicManager.storeLibrariesToStorage( xStorage );
         }
 #endif
+
+        if (comphelper::LibreOfficeKit::isActive())
+        {
+            // Because XMLTextFieldExport::ExportFieldDeclarations (called 
from SwXMLExport)
+            // calls SwXTextFieldMasters::getByName, which in turn maps 
property names by
+            // calling SwStyleNameMapper::GetTextUINameArray, which uses
+            // SvtSysLocale().GetUILanguageTag() to do the mapping, saving 
indirectly depends
+            // on the UI language. This is an unfortunate depenency. Here we 
use the loader's language.
+            const LanguageTag viewLanguage = 
comphelper::LibreOfficeKit::getLanguageTag();
+            const LanguageTag loadLanguage = SfxLokHelper::getLoadLanguage();
+
+            // Use the default language for saving and restore later if 
necessary.
+            bool restoreLanguage = false;
+            if (viewLanguage != loadLanguage)
+            {
+                restoreLanguage = true;
+                comphelper::LibreOfficeKit::setLanguageTag(loadLanguage);
+            }
+
+            // Restore the view's original language automatically and as 
necessary.
+            const ::comphelper::ScopeGuard aGuard(
+                [&viewLanguage, restoreLanguage]()
+                {
+                    if (restoreLanguage
+                        && viewLanguage != 
comphelper::LibreOfficeKit::getLanguageTag())
+                        
comphelper::LibreOfficeKit::setLanguageTag(viewLanguage);
+                });
+
+            return SaveAs(rMedium);
+        }
+
         return SaveAs( rMedium );
     }
     else return false;
diff --git a/sfx2/source/view/lokhelper.cxx b/sfx2/source/view/lokhelper.cxx
index a8dc5b5bf3b7..100cb3595e46 100644
--- a/sfx2/source/view/lokhelper.cxx
+++ b/sfx2/source/view/lokhelper.cxx
@@ -69,6 +69,7 @@ int DisableCallbacks::m_nDisabled = 0;
 namespace
 {
 LanguageTag g_defaultLanguageTag("en-US", true);
+LanguageTag g_loadLanguageTag("en-US", true); //< The language used to load.
 LOKDeviceFormFactor g_deviceFormFactor = LOKDeviceFormFactor::UNKNOWN;
 }
 
@@ -275,6 +276,13 @@ void SfxLokHelper::setDefaultLanguage(const OUString& 
rBcp47LanguageTag)
     g_defaultLanguageTag = LanguageTag(rBcp47LanguageTag, true);
 }
 
+const LanguageTag& SfxLokHelper::getLoadLanguage() { return g_loadLanguageTag; 
}
+
+void SfxLokHelper::setLoadLanguage(const OUString& rBcp47LanguageTag)
+{
+    g_loadLanguageTag = LanguageTag(rBcp47LanguageTag, true);
+}
+
 void SfxLokHelper::setViewLanguage(int nId, const OUString& rBcp47LanguageTag)
 {
     SfxViewShellArr_Impl& rViewArr = SfxGetpApp()->GetViewShells_Impl();

Reply via email to