[Libreoffice-commits] core.git: accessibility/inc accessibility/source include/unotools unotools/source

Noel Grandin (via logerrit) logerrit at kemper.freedesktop.org
Thu Jul 29 07:23:02 UTC 2021


 accessibility/inc/helper/accresmgr.hxx    |    4 +--
 accessibility/inc/strings.hrc             |    2 -
 accessibility/source/helper/accresmgr.cxx |    3 --
 include/unotools/resmgr.hxx               |   16 +++++++++++++++
 unotools/source/i18n/resmgr.cxx           |   31 ++++++++++++++++++++++++++++++
 5 files changed, 51 insertions(+), 5 deletions(-)

New commits:
commit 53840ffd9755bbf2965676136c9772089891bfe8
Author:     Noel Grandin <noel.grandin at collabora.co.uk>
AuthorDate: Wed Jul 28 15:21:26 2021 +0200
Commit:     Noel Grandin <noel.grandin at collabora.co.uk>
CommitDate: Thu Jul 29 09:22:30 2021 +0200

    Pass context and resource string down to boost::locale separately
    
    because this is often on a hot path, and we can avoid the splitting and
    joining of strings like this.
    
    Create a new small index type TranslateId for some type-safety.
    
    This change only updates accessibility to use the new API, other modules
    will follow in stages.
    
    Change-Id: I289245ad34fda775812302ca7ac1588710167b34
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/119632
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.grandin at collabora.co.uk>

diff --git a/accessibility/inc/helper/accresmgr.hxx b/accessibility/inc/helper/accresmgr.hxx
index c0926f190ac1..75ccc2df63fb 100644
--- a/accessibility/inc/helper/accresmgr.hxx
+++ b/accessibility/inc/helper/accresmgr.hxx
@@ -20,8 +20,8 @@
 #pragma once
 
 #include <rtl/ustring.hxx>
-#include <string_view>
+#include <unotools/resmgr.hxx>
 
-OUString AccResId(std::string_view aId);
+OUString AccResId(TranslateId aId);
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/accessibility/inc/strings.hrc b/accessibility/inc/strings.hrc
index 4a79f3306033..7e51ade2d46d 100644
--- a/accessibility/inc/strings.hrc
+++ b/accessibility/inc/strings.hrc
@@ -19,7 +19,7 @@
 
 #pragma once
 
-#define NC_(Context, String) reinterpret_cast<char const *>(Context "\004" u8##String)
+#define NC_(Context, String) TranslateId(Context, u8##String)
 
 #define RID_STR_ACC_NAME_BROWSEBUTTON           NC_("RID_STR_ACC_NAME_BROWSEBUTTON", "Browse")
 #define STR_SVT_ACC_ACTION_EXPAND               NC_("STR_SVT_ACC_ACTION_EXPAND", "Expand" )
diff --git a/accessibility/source/helper/accresmgr.cxx b/accessibility/source/helper/accresmgr.cxx
index b335f8ac185b..f120dcb2600d 100644
--- a/accessibility/source/helper/accresmgr.cxx
+++ b/accessibility/source/helper/accresmgr.cxx
@@ -18,8 +18,7 @@
  */
 
 #include <helper/accresmgr.hxx>
-#include <unotools/resmgr.hxx>
 
-OUString AccResId(std::string_view aId) { return Translate::get(aId, Translate::Create("acc")); }
+OUString AccResId(TranslateId aId) { return Translate::get(aId, Translate::Create("acc")); }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/include/unotools/resmgr.hxx b/include/unotools/resmgr.hxx
index 4fce82b82294..244963f598b1 100644
--- a/include/unotools/resmgr.hxx
+++ b/include/unotools/resmgr.hxx
@@ -28,11 +28,27 @@ class LanguageTag;
 
 typedef OUString (*ResHookProc)(const OUString& rStr);
 
+struct UNOTOOLS_DLLPUBLIC TranslateId
+{
+    const char* mpContext;
+    const char* mpId;
+
+    TranslateId() {}
+    TranslateId(const char* pContext, const char* pId)
+        : mpContext(pContext), mpId(pId) {}
+
+    operator bool() const { return mpId != nullptr; }
+
+    bool operator==(const TranslateId& other) const;
+    bool operator!=(const TranslateId& other) const { return !operator==(other); }
+};
+
 namespace Translate
 {
     UNOTOOLS_DLLPUBLIC std::locale Create(std::string_view aPrefixName, const LanguageTag& rLocale = SvtSysLocale().GetUILanguageTag());
     UNOTOOLS_DLLPUBLIC OUString get(std::string_view aId, const std::locale &loc);
     UNOTOOLS_DLLPUBLIC OUString nget(std::string_view aId, int n, const std::locale &loc);
+    UNOTOOLS_DLLPUBLIC OUString get(TranslateId sContextAndId, const std::locale &loc);
     UNOTOOLS_DLLPUBLIC void SetReadStringHook( ResHookProc pProc );
     UNOTOOLS_DLLPUBLIC ResHookProc GetReadStringHook();
     UNOTOOLS_DLLPUBLIC OUString ExpandVariables(const OUString& rString);
diff --git a/unotools/source/i18n/resmgr.cxx b/unotools/source/i18n/resmgr.cxx
index d35282025c4e..9d3aec3a607c 100644
--- a/unotools/source/i18n/resmgr.cxx
+++ b/unotools/source/i18n/resmgr.cxx
@@ -255,6 +255,31 @@ namespace Translate
         return result;
     }
 
+    OUString get(TranslateId sContextAndId, const std::locale &loc)
+    {
+        assert(!strchr(sContextAndId.mpId, '\004') && "should be using nget, not get");
+
+        //if it's a key id locale, generate it here
+        if (std::use_facet<boost::locale::info>(loc).language() == "qtz")
+        {
+            OString sKeyId(genKeyId(OString::Concat(sContextAndId.mpContext) + "|" + std::string_view(sContextAndId.mpId)));
+            return OUString::fromUtf8(sKeyId) + u"\u2016" + createFromUtf8(sContextAndId.mpId, strlen(sContextAndId.mpId));
+        }
+
+        //otherwise translate it
+        const std::string ret = boost::locale::pgettext(sContextAndId.mpContext, sContextAndId.mpId, loc);
+        OUString result(ExpandVariables(createFromUtf8(ret.data(), ret.size())));
+
+        if (comphelper::LibreOfficeKit::isActive())
+        {
+            // If it is de-CH, change sharp s to double s.
+            if (std::use_facet<boost::locale::info>(loc).country() == "CH" &&
+                std::use_facet<boost::locale::info>(loc).language() == "de")
+                result = result.replaceAll(OUString::fromUtf8("\xC3\x9F"), "ss");
+        }
+        return result;
+    }
+
     OUString nget(std::string_view aContextAndIds, int n, const std::locale &loc)
     {
         OString sContextIdId(aContextAndIds);
@@ -308,4 +333,10 @@ namespace Translate
     }
 }
 
+bool TranslateId::operator==(const TranslateId& other) const
+{
+    return strcmp(mpContext, other.mpContext) == 0 && strcmp(mpId,other.mpId) == 0;
+}
+
+
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */


More information about the Libreoffice-commits mailing list