diff options
author | Caolán McNamara <caolanm@redhat.com> | 2017-07-31 17:16:27 +0100 |
---|---|---|
committer | Caolán McNamara <caolanm@redhat.com> | 2017-08-01 13:42:21 +0100 |
commit | b1d829e52e826b6ea4ae884a64fdb68b66c74dd7 (patch) | |
tree | d6aa2ee59178ecf5ea78e80917df18149ae9bb66 /unotools | |
parent | f4e5940abbbaa3c2747108b0954e8912d164f3e5 (diff) |
move resmgr to unotools
and the vast majority of translations is to the ui language so default
ctor with that arg
and now drop OModuleResourceClient
Change-Id: I3b85a560ffdfe5f019c2271ac56a5fe4a361522b
Diffstat (limited to 'unotools')
-rw-r--r-- | unotools/Library_utl.mk | 7 | ||||
-rw-r--r-- | unotools/source/i18n/resmgr.cxx | 158 | ||||
-rw-r--r-- | unotools/source/misc/componentresmodule.cxx | 87 |
3 files changed, 163 insertions, 89 deletions
diff --git a/unotools/Library_utl.mk b/unotools/Library_utl.mk index 8656ebbe594c..0bfcbad5e383 100644 --- a/unotools/Library_utl.mk +++ b/unotools/Library_utl.mk @@ -11,7 +11,10 @@ # utl is the name of the library as it is found in Repository.mk $(eval $(call gb_Library_Library,utl)) -$(eval $(call gb_Library_use_external,utl,boost_headers)) +$(eval $(call gb_Library_use_externals,utl,\ + boost_headers \ + boost_locale \ +)) $(eval $(call gb_Library_use_custom_headers,utl,\ officecfg/registry \ @@ -92,10 +95,10 @@ $(eval $(call gb_Library_add_exception_objects,utl,\ unotools/source/i18n/localedatawrapper \ unotools/source/i18n/nativenumberwrapper \ unotools/source/i18n/readwritemutexguard \ + unotools/source/i18n/resmgr \ unotools/source/i18n/textsearch \ unotools/source/i18n/transliterationwrapper \ unotools/source/misc/closeveto \ - unotools/source/misc/componentresmodule \ unotools/source/misc/datetime \ unotools/source/misc/desktopterminationobserver \ unotools/source/misc/eventlisteneradapter \ diff --git a/unotools/source/i18n/resmgr.cxx b/unotools/source/i18n/resmgr.cxx new file mode 100644 index 000000000000..492c7fcf8632 --- /dev/null +++ b/unotools/source/i18n/resmgr.cxx @@ -0,0 +1,158 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <config_folders.h> + +#include <sal/config.h> + +#include <cassert> + +#include <string.h> +#include <stdio.h> +#include <stdlib.h> + +#include <tools/debug.hxx> +#include <tools/stream.hxx> +#include <unotools/resmgr.hxx> +#include <osl/endian.h> +#include <osl/process.h> +#include <osl/thread.h> +#include <osl/file.hxx> +#include <osl/mutex.hxx> +#include <osl/signal.h> +#include <rtl/crc.h> +#include <rtl/ustrbuf.hxx> +#include <rtl/strbuf.hxx> +#include <sal/log.hxx> +#include <rtl/instance.hxx> +#include <rtl/bootstrap.hxx> +#include <i18nlangtag/languagetag.hxx> +#include <i18nlangtag/mslangid.hxx> + +#include <boost/locale.hpp> +#include <boost/locale/gnu_gettext.hpp> + +#include <algorithm> +#include <list> +#include <set> +#include <unordered_set> +#include <unordered_map> +#include <memory> + +namespace +{ + OUString createFromUtf8(const char* data, size_t size) + { + OUString aTarget; + bool bSuccess = rtl_convertStringToUString(&aTarget.pData, + data, + size, + RTL_TEXTENCODING_UTF8, + RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR|RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR|RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR); + (void) bSuccess; + assert(bSuccess); + return aTarget; + } + + OString genKeyId(const OString& rGenerator) + { + sal_uInt32 nCRC = rtl_crc32(0, rGenerator.getStr(), rGenerator.getLength()); + // Use simple ASCII characters, exclude I, l, 1 and O, 0 to avoid confusing IDs + static const char sSymbols[] = + "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789"; + char sKeyId[6]; + for (short nKeyInd = 0; nKeyInd < 5; ++nKeyInd) + { + sKeyId[nKeyInd] = sSymbols[(nCRC & 63) % strlen(sSymbols)]; + nCRC >>= 6; + } + sKeyId[5] = '\0'; + return OString(sKeyId); + } +} + +namespace Translate +{ + std::locale Create(const sal_Char* pPrefixName, const LanguageTag& rLocale) + { + static std::unordered_map<OString, std::locale, OStringHash> aCache; + OString sIdentifier = rLocale.getGlibcLocaleString(".UTF-8").toUtf8(); + OString sUnique = sIdentifier + OString(pPrefixName); + auto aFind = aCache.find(sUnique); + if (aFind != aCache.end()) + return aFind->second; + boost::locale::generator gen; + gen.characters(boost::locale::char_facet); + gen.categories(boost::locale::message_facet | boost::locale::information_facet); + OUString uri("$BRAND_BASE_DIR/$BRAND_SHARE_RESOURCE_SUBDIR/"); + rtl::Bootstrap::expandMacros(uri); + OUString path; + osl::File::getSystemPathFromFileURL(uri, path); + gen.add_messages_path(OUStringToOString(path, osl_getThreadTextEncoding()).getStr()); + gen.add_messages_domain(pPrefixName); + std::locale aRet(gen(sIdentifier.getStr())); + aCache[sUnique] = aRet; + return aRet; + } + + OUString get(const char* pContextAndId, const std::locale &loc) + { + OString sContext; + const char *pId = strchr(pContextAndId, '\004'); + if (!pId) + pId = pContextAndId; + else + { + sContext = OString(pContextAndId, pId - pContextAndId); + ++pId; + } + + //if its a key id locale, generate it here + if (std::use_facet<boost::locale::info>(loc).language() == "qtz") + { + OString sKeyId(genKeyId(OString(pContextAndId).replace('\004', '|'))); + return OUString::fromUtf8(sKeyId) + OUStringLiteral1(0x2016) + createFromUtf8(pId, strlen(pId)); + } + + //otherwise translate it + const std::string ret = boost::locale::pgettext(sContext.getStr(), pId, loc); + return ExpandVariables(createFromUtf8(ret.data(), ret.size())); + } + + static ResHookProc pImplResHookProc = nullptr; + + OUString ExpandVariables(const OUString& rString) + { + if (pImplResHookProc) + return pImplResHookProc(rString); + return rString; + } + + void SetReadStringHook( ResHookProc pProc ) + { + pImplResHookProc = pProc; + } + + ResHookProc GetReadStringHook() + { + return pImplResHookProc; + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/unotools/source/misc/componentresmodule.cxx b/unotools/source/misc/componentresmodule.cxx deleted file mode 100644 index cb1f718fca44..000000000000 --- a/unotools/source/misc/componentresmodule.cxx +++ /dev/null @@ -1,87 +0,0 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* - * This file is part of the LibreOffice project. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * This file incorporates work covered by the following license notice: - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed - * with this work for additional information regarding copyright - * ownership. The ASF licenses this file to you under the Apache - * License, Version 2.0 (the "License"); you may not use this file - * except in compliance with the License. You may obtain a copy of - * the License at http://www.apache.org/licenses/LICENSE-2.0 . - */ - -#include <unotools/componentresmodule.hxx> -#include <tools/resmgr.hxx> -#include <osl/diagnose.h> -#include <rtl/strbuf.hxx> - -namespace utl -{ - - //= OComponentResModuleImpl - - /** PIMPL-class for OComponentResourceModule - - not threadsafe! - */ - class OComponentResModuleImpl - { - private: - std::locale m_aLocale; - bool m_bLocaleInitialized; - OString m_sResFilePrefix; - LanguageTag m_aLanguage; - - OComponentResModuleImpl(const OComponentResModuleImpl&) = delete; - OComponentResModuleImpl& operator=(const OComponentResModuleImpl&) = delete; - - public: - explicit OComponentResModuleImpl(const OString& _rResFilePrefix, const LanguageTag& rLanguage) - : m_bLocaleInitialized( false ) - , m_sResFilePrefix( _rResFilePrefix ) - , m_aLanguage( rLanguage ) - { - } - - /** retrieves our resource manager - */ - const std::locale& getResLocale(); - }; - - const std::locale& OComponentResModuleImpl::getResLocale() - { - if (!m_bLocaleInitialized) - { - m_aLocale = Translate::Create(m_sResFilePrefix.getStr(), m_aLanguage); - m_bLocaleInitialized = true; - } - return m_aLocale; - } - - //= OComponentResourceModule - OComponentResourceModule::OComponentResourceModule(const OString& _rResFilePrefix, const LanguageTag& rLanguage) - :BaseClass() - ,m_pImpl( new OComponentResModuleImpl( _rResFilePrefix, rLanguage ) ) - { - } - - OComponentResourceModule::~OComponentResourceModule() - { - } - - const std::locale& OComponentResourceModule::getResLocale() - { - ::osl::MutexGuard aGuard( m_aMutex ); - return m_pImpl->getResLocale(); - } - -} // namespace utl - -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |