LibreOffice Module sc (master) 1
cellkeytranslator.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <memory>
21#include <global.hxx>
22#include <cellkeytranslator.hxx>
24#include <i18nlangtag/lang.h>
26#include <rtl/ustring.hxx>
28#include <com/sun/star/uno/Sequence.hxx>
29
30using ::com::sun::star::uno::Sequence;
31
32using namespace ::com::sun::star;
33
34namespace {
35
36enum LocaleMatch
37{
38 LOCALE_MATCH_NONE = 0,
39 LOCALE_MATCH_LANG,
40 LOCALE_MATCH_LANG_SCRIPT,
41 LOCALE_MATCH_LANG_SCRIPT_COUNTRY,
42 LOCALE_MATCH_ALL
43};
44
45}
46
47static LocaleMatch lclLocaleCompare(const lang::Locale& rLocale1, const LanguageTag& rLanguageTag2)
48{
49 LocaleMatch eMatchLevel = LOCALE_MATCH_NONE;
50 LanguageTag aLanguageTag1( rLocale1);
51
52 if ( aLanguageTag1.getLanguage() == rLanguageTag2.getLanguage() )
53 eMatchLevel = LOCALE_MATCH_LANG;
54 else
55 return eMatchLevel;
56
57 if ( aLanguageTag1.getScript() == rLanguageTag2.getScript() )
58 eMatchLevel = LOCALE_MATCH_LANG_SCRIPT;
59 else
60 return eMatchLevel;
61
62 if ( aLanguageTag1.getCountry() == rLanguageTag2.getCountry() )
63 eMatchLevel = LOCALE_MATCH_LANG_SCRIPT_COUNTRY;
64 else
65 return eMatchLevel;
66
67 if (aLanguageTag1 == rLanguageTag2)
68 return LOCALE_MATCH_ALL;
69
70 return eMatchLevel;
71}
72
73ScCellKeyword::ScCellKeyword(const char* pName, OpCode eOpCode, const lang::Locale& rLocale) :
74 mpName(pName),
75 meOpCode(eOpCode),
76 mrLocale(rLocale)
77{
78}
79
80::std::unique_ptr<ScCellKeywordTranslator> ScCellKeywordTranslator::spInstance;
81
82static void lclMatchKeyword(OUString& rName, const ScCellKeywordHashMap& aMap,
83 OpCode eOpCode, const lang::Locale* pLocale)
84{
85 ScCellKeywordHashMap::const_iterator itrEnd = aMap.end();
86 ScCellKeywordHashMap::const_iterator itr = aMap.find(rName);
87
88 if ( itr == itrEnd || itr->second.empty() )
89 // No candidate strings exist. Bail out.
90 return;
91
92 if ( eOpCode == ocNone && !pLocale )
93 {
94 // Since no locale nor opcode matching is needed, simply return
95 // the first item on the list.
96 rName = OUString::createFromAscii( itr->second.front().mpName );
97 return;
98 }
99
100 LanguageTag aLanguageTag( pLocale ? *pLocale : lang::Locale("","",""));
101 const char* aBestMatchName = itr->second.front().mpName;
102 LocaleMatch eLocaleMatchLevel = LOCALE_MATCH_NONE;
103 bool bOpCodeMatched = false;
104
105 for (auto const& elem : itr->second)
106 {
107 if ( eOpCode != ocNone && pLocale )
108 {
109 if (elem.meOpCode == eOpCode)
110 {
111 LocaleMatch eLevel = lclLocaleCompare(elem.mrLocale, aLanguageTag);
112 if ( eLevel == LOCALE_MATCH_ALL )
113 {
114 // Name with matching opcode and locale found.
115 rName = OUString::createFromAscii( elem.mpName );
116 return;
117 }
118 else if ( eLevel > eLocaleMatchLevel )
119 {
120 // Name with a better matching locale.
121 eLocaleMatchLevel = eLevel;
122 aBestMatchName = elem.mpName;
123 }
124 else if ( !bOpCodeMatched )
125 // At least the opcode matches.
126 aBestMatchName = elem.mpName;
127
128 bOpCodeMatched = true;
129 }
130 }
131 else if ( eOpCode != ocNone && !pLocale )
132 {
133 if ( elem.meOpCode == eOpCode )
134 {
135 // Name with a matching opcode preferred.
136 rName = OUString::createFromAscii( elem.mpName );
137 return;
138 }
139 }
140 else if ( pLocale )
141 {
142 LocaleMatch eLevel = lclLocaleCompare(elem.mrLocale, aLanguageTag);
143 if ( eLevel == LOCALE_MATCH_ALL )
144 {
145 // Name with matching locale preferred.
146 rName = OUString::createFromAscii( elem.mpName );
147 return;
148 }
149 else if ( eLevel > eLocaleMatchLevel )
150 {
151 // Name with a better matching locale.
152 eLocaleMatchLevel = eLevel;
153 aBestMatchName = elem.mpName;
154 }
155 }
156 }
157
158 // No preferred strings found. Return the best matching name.
159 rName = OUString::createFromAscii(aBestMatchName);
160}
161
162void ScCellKeywordTranslator::transKeyword(OUString& rName, const lang::Locale* pLocale, OpCode eOpCode)
163{
164 if (!spInstance)
166
167 LanguageType nLang = pLocale ?
168 LanguageTag(*pLocale).makeFallback().getLanguageType() : ScGlobal::oSysLocale->GetLanguageTag().getLanguageType();
169 Sequence<sal_Int32> aOffsets;
170 rName = spInstance->maTransWrapper.transliterate(rName, nLang, 0, rName.getLength(), &aOffsets);
171 lclMatchKeyword(rName, spInstance->maStringNameMap, eOpCode, pLocale);
172}
173
175{
177 const char* to;
179};
180
182 maTransWrapper( ::comphelper::getProcessComponentContext(),
184{
185 // The file below has been autogenerated by sc/workben/celltrans/parse.py.
186 // To add new locale keywords, edit sc/workben/celltrans/keywords_utf16.txt
187 // and re-run the parse.py script.
188 //
189 // All keywords must be uppercase, and the mapping must be from the
190 // localized keyword to the English keyword.
191 //
192 // Make sure that the original keyword file (keywords_utf16.txt) is
193 // encoded in UCS-2/UTF-16!
194
195 #include "cellkeywords.inl"
196}
197
199{
200}
201
202void ScCellKeywordTranslator::addToMap(const OUString& rKey, const char* pName, const lang::Locale& rLocale, OpCode eOpCode)
203{
204 ScCellKeyword aKeyItem( pName, eOpCode, rLocale );
205
206 ScCellKeywordHashMap::iterator itrEnd = maStringNameMap.end();
207 ScCellKeywordHashMap::iterator itr = maStringNameMap.find(rKey);
208
209 if ( itr == itrEnd )
210 {
211 // New keyword.
212 std::vector<ScCellKeyword> aVector { aKeyItem };
213 maStringNameMap.emplace(rKey, aVector);
214 }
215 else
216 itr->second.push_back(aKeyItem);
217}
218
219void ScCellKeywordTranslator::addToMap(const TransItem* pItems, const lang::Locale& rLocale)
220{
221 for (sal_uInt16 i = 0; pItems[i].from != nullptr; ++i)
222 addToMap(OUString(pItems[i].from), pItems[i].to, rLocale, pItems[i].func);
223}
224
225/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const char * pName
static LocaleMatch lclLocaleCompare(const lang::Locale &rLocale1, const LanguageTag &rLanguageTag2)
static void lclMatchKeyword(OUString &rName, const ScCellKeywordHashMap &aMap, OpCode eOpCode, const lang::Locale *pLocale)
std::unordered_map< OUString, ::std::vector< ScCellKeyword > > ScCellKeywordHashMap
LanguageType getLanguageType(bool bResolveSystem=true) const
OUString getLanguage() const
OUString getScript() const
LanguageTag & makeFallback()
OUString getCountry() const
Translate cell function keywords.
void addToMap(const OUString &rKey, const char *pName, const css::lang::Locale &rLocale, OpCode eOpCode)
static void transKeyword(OUString &rName, const css::lang::Locale *pLocale, OpCode eOpCode)
static ::std::unique_ptr< ScCellKeywordTranslator > spInstance
ScCellKeywordHashMap maStringNameMap
static std::optional< SvtSysLocale > oSysLocale
Definition: global.hxx:544
const char * pLocale
Reference< XComponentContext > getProcessComponentContext()
int i
HashMap_OWString_Interface aMap
OpCode
ocNone
ScCellKeyword(const char *pName, OpCode eOpCode, const css::lang::Locale &rLocale)
const char * to
const sal_Unicode * from
TransliterationFlags
sal_uInt16 sal_Unicode
static N to(double f)
Definition: xltools.cxx:275