[Libreoffice-commits] core.git: shell/Library_localebe.mk shell/source

Eike Rathke erack at redhat.com
Wed Dec 6 19:15:01 UTC 2017


 shell/Library_localebe.mk                        |    1 
 shell/source/backends/localebe/localebackend.cxx |   34 ++++++++++++++++++-----
 2 files changed, 28 insertions(+), 7 deletions(-)

New commits:
commit c2bd06120b932bf3757f19bdf8c8d9ee8a31f557
Author: Eike Rathke <erack at redhat.com>
Date:   Wed Dec 6 13:21:50 2017 +0100

    Handle conversion from glibc locale to BCP 47 language tag
    
    The backend's ImplGetLocale() didn't handle variants, so
    ca_ES at valencia ended up as ca-ES instead of ca-ES-valencia, which
    made a difference with for example the UI language being set to
    Default resulting in only ca instead of ca-valencia, which then
    is also written to /org.openoffice.Setup/L10N/ooLocale during
    startup and obtained later.
    
    This only for the *iX branch, no idea if and what could be
    adjusted for Windows or MacOSX.
    
    Change-Id: I050f6f643571ccdc669fb91b06f3bb516f96e8d5
    Reviewed-on: https://gerrit.libreoffice.org/45946
    Reviewed-by: Eike Rathke <erack at redhat.com>
    Tested-by: Jenkins <ci at libreoffice.org>

diff --git a/shell/Library_localebe.mk b/shell/Library_localebe.mk
index 7c4931b2ce39..b8d8aba8f15c 100644
--- a/shell/Library_localebe.mk
+++ b/shell/Library_localebe.mk
@@ -22,6 +22,7 @@ $(eval $(call gb_Library_use_libraries,localebe1,\
 	cppu \
 	cppuhelper \
 	sal \
+	i18nlangtag \
 ))
 
 $(eval $(call gb_Library_set_componentfile,localebe1,shell/source/backends/localebe/localebe1))
diff --git a/shell/source/backends/localebe/localebackend.cxx b/shell/source/backends/localebe/localebackend.cxx
index 7ff388a1f26d..3392bb73cfc0 100644
--- a/shell/source/backends/localebe/localebackend.cxx
+++ b/shell/source/backends/localebe/localebackend.cxx
@@ -28,6 +28,8 @@
 #include <osl/time.h>
 #include <rtl/character.hxx>
 #include <o3tl/char16_t2wchar_t.hxx>
+#include <i18nlangtag/languagetag.hxx>
+#include <i18nlangtag/mslangid.hxx>
 
 #include <stdio.h>
 
@@ -184,16 +186,19 @@ static css::beans::Optional<css::uno::Any> ImplGetLocale(char const * category)
 
     const char *cp;
     const char *uscore = nullptr;
+    const char *end = nullptr;
 
     // locale string have the format lang[_ctry][.encoding][@modifier]
-    // we are only interested in the first two items, so we handle
-    // '.' and '@' as string end.
+    // Let LanguageTag handle all conversion, but do a sanity and length check
+    // first.
+    // For the fallback we are only interested in the first two items, so we
+    // handle '.' and '@' as string end for that.
     for (cp = locale; *cp; cp++)
     {
-        if (*cp == '_')
+        if (*cp == '_' && !uscore)
             uscore = cp;
-        if (*cp == '.' || *cp == '@')
-            break;
+        if ((*cp == '.' || *cp == '@') && !end)
+            end = cp;
         if (!rtl::isAscii(static_cast<unsigned char>(*cp))) {
             SAL_INFO("shell", "locale env var with non-ASCII content");
             return {false, {}};
@@ -205,16 +210,31 @@ static css::beans::Optional<css::uno::Any> ImplGetLocale(char const * category)
         return {false, {}};
     }
 
+    // This is a tad awkward.. but the easiest way to obtain what we're
+    // actually interested in. For example this also converts
+    // "ca_ES.UTF-8 at valencia" to "ca-ES-valencia".
+    const OString aLocaleStr(locale);
+    const LanguageType nLang = MsLangId::convertUnxByteStringToLanguage( aLocaleStr);
+    if (nLang != LANGUAGE_DONTKNOW)
+    {
+        const OUString aLangTagStr( LanguageTag::convertToBcp47( nLang));
+        return {true, css::uno::Any(aLangTagStr)};
+    }
+
+    // As a fallback, strip encoding and modifier and return just a
+    // language-country combination and let the caller handle unknowns.
     OUStringBuffer aLocaleBuffer;
+    if (!end)
+        end = cp;
     if( uscore != nullptr )
     {
         aLocaleBuffer.appendAscii(locale, uscore++ - locale);
         aLocaleBuffer.append("-");
-        aLocaleBuffer.appendAscii(uscore, cp - uscore);
+        aLocaleBuffer.appendAscii(uscore, end - uscore);
     }
     else
     {
-        aLocaleBuffer.appendAscii(locale, cp - locale);
+        aLocaleBuffer.appendAscii(locale, end - locale);
     }
 
     return {true, css::uno::Any(aLocaleBuffer.makeStringAndClear())};


More information about the Libreoffice-commits mailing list