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