1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <boost/version.hpp>
21 #if BOOST_VERSION < 106700
22 // Needed when #include <boost/locale.hpp> below includes Boost 1.65.1
23 // workdir/UnpackedTarball/boost/boost/locale/format.hpp using "std::auto_ptr<data> d;", but must
24 // come very early here in case <memory> is already (indirectly) included earlier:
25 #include <config_libcxx.h>
27 #define _LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR
28 #elif defined _MSC_VER
29 #define _HAS_AUTO_PTR_ETC 1
33 #include <sal/config.h>
39 #if defined UNX && !defined MACOSX && !defined IOS && !defined ANDROID && !defined EMSCRIPTEN
43 #include <comphelper/lok.hxx>
44 #include <unotools/resmgr.hxx>
45 #include <osl/thread.h>
46 #include <osl/file.hxx>
48 #include <rtl/bootstrap.hxx>
49 #include <i18nlangtag/languagetag.hxx>
51 #include <boost/locale.hpp>
52 #include <boost/locale/gnu_gettext.hpp>
54 #include <unordered_map>
57 #include <osl/detail/android-bootstrap.h>
61 #include <osl/detail/emscripten-bootstrap.h>
64 #if defined(_WIN32) && defined(DBG_UTIL)
65 #include <o3tl/char16_t2wchar_t.hxx>
73 OUString
createFromUtf8(const char* data
, size_t size
)
76 bool bSuccess
= rtl_convertStringToUString(&aTarget
.pData
,
79 RTL_TEXTENCODING_UTF8
,
80 RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR
|RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR
|RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR
);
86 OString
genKeyId(const OString
& rGenerator
)
88 sal_uInt32 nCRC
= rtl_crc32(0, rGenerator
.getStr(), rGenerator
.getLength());
89 // Use simple ASCII characters, exclude I, l, 1 and O, 0 to avoid confusing IDs
90 static const char sSymbols
[] =
91 "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789";
93 for (short nKeyInd
= 0; nKeyInd
< 5; ++nKeyInd
)
95 sKeyId
[nKeyInd
] = sSymbols
[(nCRC
& 63) % strlen(sSymbols
)];
103 #if defined(_WIN32) && defined(DBG_UTIL)
104 static int IgnoringCrtReportHook(int reportType
, wchar_t *message
, int * /* returnValue */)
107 if (reportType
== _CRT_WARN
)
109 else if (reportType
== _CRT_ERROR
)
111 else if (reportType
== _CRT_ASSERT
)
114 sType
= "?(" + OUString::number(reportType
) + ")";
116 SAL_WARN("unotools.i18n", "CRT Report Hook: " << sType
<< ": " << OUString(o3tl::toU(message
)));
125 std::locale
Create(std::string_view aPrefixName
, const LanguageTag
& rLocale
)
127 static std::unordered_map
<OString
, std::locale
> aCache
;
128 OString sIdentifier
= rLocale
.getGlibcLocaleString(u
".UTF-8").toUtf8();
129 OString sUnique
= sIdentifier
+ aPrefixName
;
130 auto aFind
= aCache
.find(sUnique
);
131 if (aFind
!= aCache
.end())
132 return aFind
->second
;
133 boost::locale::generator gen
;
134 #if BOOST_VERSION < 108100
135 gen
.characters(boost::locale::char_facet
);
136 gen
.categories(boost::locale::message_facet
| boost::locale::information_facet
);
138 gen
.characters(boost::locale::char_facet_t::char_f
);
139 gen
.categories(boost::locale::category_t::message
| boost::locale::category_t::information
);
141 #if defined(ANDROID) || defined(EMSCRIPTEN)
142 OString
sPath(OString(lo_get_app_data_dir()) + "/program/resource");
144 OUString
uri("$BRAND_BASE_DIR/$BRAND_SHARE_RESOURCE_SUBDIR/");
145 rtl::Bootstrap::expandMacros(uri
);
147 osl::File::getSystemPathFromFileURL(uri
, path
);
149 // add_messages_path is documented to treat path string in the *created* locale's encoding
150 // on Windows; creating an UTF-8 encoding, we're lucky to have Unicode path support here.
151 constexpr rtl_TextEncoding eEncoding
= RTL_TEXTENCODING_UTF8
;
153 const rtl_TextEncoding eEncoding
= osl_getThreadTextEncoding();
155 OString
sPath(OUStringToOString(path
, eEncoding
));
157 gen
.add_messages_path(std::string(sPath
));
158 #if defined UNX && !defined MACOSX && !defined IOS && !defined ANDROID && !defined EMSCRIPTEN
159 // allow gettext to find these .mo files e.g. so gtk dialogs can use them
160 bindtextdomain(aPrefixName
.data(), sPath
.getStr());
161 // tdf#131069 gtk, and anything sane, always wants utf-8 strings as output
162 bind_textdomain_codeset(aPrefixName
.data(), "UTF-8");
164 gen
.add_messages_domain(aPrefixName
.data());
166 #if defined(_WIN32) && defined(DBG_UTIL)
167 // With a newer C++ debug runtime (in an --enable-dbgutil build), passing an invalid locale
168 // name causes an attempt to display an error dialog. Which does not even show up, at least
169 // for me, but instead the process (gengal, at least) just hangs. Which is far from ideal.
171 // Passing a POSIX-style locale name to the std::locale constructor on Windows is a bit odd,
172 // but apparently in the normal C++ runtime it "just" causes an exception to be thrown, that
173 // boost catches (see the loadable(std::string name) in boost's
174 // libs\locale\src\std\std_backend.cpp), and then instead uses the Windows style locale name
175 // it knows how to construct. (Why does it even try the POSIX style name I can't
178 // Actually it isn't just the locale name part "en_US" of a locale like "en_US.UTF-8" that
179 // is problematic, but also the encoding part, "UTF-8". The Microsoft C/C++ library does not
180 // support UTF-8 locales. The error message that our own report hook catches says:
181 // "f:\dd\vctools\crt\crtw32\stdcpp\xmbtowc.c(89) : Assertion failed: ploc->_Mbcurmax == 1
182 // || ploc->_Mbcurmax == 2". Clearly in a UTF-8 locale (perhaps one that boost internally
183 // constructs?) the maximum bytes per character will be more than 2.
185 // With a debug C++ runtime, we need to avoid the error dialog, and just ignore the error.
187 struct CrtSetReportHook
189 int mnCrtSetReportHookSucceeded
;
193 mnCrtSetReportHookSucceeded
= _CrtSetReportHookW2(_CRT_RPTHOOK_INSTALL
, IgnoringCrtReportHook
);
198 if (mnCrtSetReportHookSucceeded
>= 0)
199 _CrtSetReportHookW2(_CRT_RPTHOOK_REMOVE
, IgnoringCrtReportHook
);
205 std::locale
aRet(gen(std::string(sIdentifier
)));
207 aCache
[sUnique
] = aRet
;
211 OUString
get(TranslateId sContextAndId
, const std::locale
&loc
)
213 assert(!strchr(sContextAndId
.mpId
, '\004') && "should be using nget, not get");
215 //if it's a key id locale, generate it here
216 if (std::use_facet
<boost::locale::info
>(loc
).language() == "qtz")
218 OString
sKeyId(genKeyId(OString::Concat(sContextAndId
.mpContext
) + "|" + std::string_view(sContextAndId
.mpId
)));
219 return OUString::fromUtf8(sKeyId
) + u
"\u2016" + createFromUtf8(sContextAndId
.mpId
, strlen(sContextAndId
.mpId
));
222 //otherwise translate it
223 const std::string ret
= boost::locale::pgettext(sContextAndId
.mpContext
, sContextAndId
.mpId
, loc
);
224 OUString
result(ExpandVariables(createFromUtf8(ret
.data(), ret
.size())));
226 if (comphelper::LibreOfficeKit::isActive())
228 // If it is de-CH, change sharp s to double s.
229 if (std::use_facet
<boost::locale::info
>(loc
).country() == "CH" &&
230 std::use_facet
<boost::locale::info
>(loc
).language() == "de")
231 result
= result
.replaceAll(OUString::fromUtf8("\xC3\x9F"), "ss");
236 OUString
nget(TranslateNId aContextSingularPlural
, int n
, const std::locale
&loc
)
238 //if it's a key id locale, generate it here
239 if (std::use_facet
<boost::locale::info
>(loc
).language() == "qtz")
241 OString
sKeyId(genKeyId(OString::Concat(aContextSingularPlural
.mpContext
) + "|" + aContextSingularPlural
.mpSingular
));
242 const char* pForm
= n
== 0 ? aContextSingularPlural
.mpSingular
: aContextSingularPlural
.mpPlural
;
243 return OUString::fromUtf8(sKeyId
) + u
"\u2016" + createFromUtf8(pForm
, strlen(pForm
));
246 //otherwise translate it
247 const std::string ret
= boost::locale::npgettext(aContextSingularPlural
.mpContext
, aContextSingularPlural
.mpSingular
, aContextSingularPlural
.mpPlural
, n
, loc
);
248 OUString
result(ExpandVariables(createFromUtf8(ret
.data(), ret
.size())));
250 if (comphelper::LibreOfficeKit::isActive())
252 if (std::use_facet
<boost::locale::info
>(loc
).country() == "CH" &&
253 std::use_facet
<boost::locale::info
>(loc
).language() == "de")
254 result
= result
.replaceAll(OUString::fromUtf8("\xC3\x9F"), "ss");
259 static ResHookProc pImplResHookProc
= nullptr;
261 OUString
ExpandVariables(const OUString
& rString
)
263 if (pImplResHookProc
)
264 return pImplResHookProc(rString
);
268 void SetReadStringHook( ResHookProc pProc
)
270 pImplResHookProc
= pProc
;
273 ResHookProc
GetReadStringHook()
275 return pImplResHookProc
;
279 bool TranslateId::operator==(const TranslateId
& other
) const
281 if (mpContext
== nullptr || other
.mpContext
== nullptr)
283 if (mpContext
!= other
.mpContext
)
286 else if (strcmp(mpContext
, other
.mpContext
) != 0)
289 if (mpId
== nullptr || other
.mpId
== nullptr)
291 return mpId
== other
.mpId
;
293 return strcmp(mpId
,other
.mpId
) == 0;
296 bool TranslateNId::operator==(const TranslateNId
& other
) const
298 if (mpContext
== nullptr || other
.mpContext
== nullptr)
300 if (mpContext
!= other
.mpContext
)
303 else if (strcmp(mpContext
, other
.mpContext
) != 0)
306 if (mpSingular
== nullptr || other
.mpSingular
== nullptr)
308 if (mpSingular
!= other
.mpSingular
)
311 else if (strcmp(mpSingular
, other
.mpSingular
) != 0)
314 if (mpPlural
== nullptr || other
.mpPlural
== nullptr)
316 return mpPlural
== other
.mpPlural
;
318 return strcmp(mpPlural
,other
.mpPlural
) == 0;
321 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */