1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
22 #include <cellkeytranslator.hxx>
23 #include <comphelper/processfactory.hxx>
24 #include <i18nlangtag/lang.h>
25 #include <i18nutil/transliteration.hxx>
26 #include <rtl/ustring.hxx>
27 #include <unotools/syslocale.hxx>
28 #include <com/sun/star/uno/Sequence.hxx>
30 using ::com::sun::star::uno::Sequence
;
32 using namespace ::com::sun::star
;
38 LOCALE_MATCH_NONE
= 0,
40 LOCALE_MATCH_LANG_SCRIPT
,
41 LOCALE_MATCH_LANG_SCRIPT_COUNTRY
,
47 static LocaleMatch
lclLocaleCompare(const lang::Locale
& rLocale1
, const LanguageTag
& rLanguageTag2
)
49 LocaleMatch eMatchLevel
= LOCALE_MATCH_NONE
;
50 LanguageTag
aLanguageTag1( rLocale1
);
52 if ( aLanguageTag1
.getLanguage() == rLanguageTag2
.getLanguage() )
53 eMatchLevel
= LOCALE_MATCH_LANG
;
57 if ( aLanguageTag1
.getScript() == rLanguageTag2
.getScript() )
58 eMatchLevel
= LOCALE_MATCH_LANG_SCRIPT
;
62 if ( aLanguageTag1
.getCountry() == rLanguageTag2
.getCountry() )
63 eMatchLevel
= LOCALE_MATCH_LANG_SCRIPT_COUNTRY
;
67 if (aLanguageTag1
== rLanguageTag2
)
68 return LOCALE_MATCH_ALL
;
73 ScCellKeyword::ScCellKeyword(const char* pName
, OpCode eOpCode
, const lang::Locale
& rLocale
) :
80 ::std::unique_ptr
<ScCellKeywordTranslator
> ScCellKeywordTranslator::spInstance
;
82 static void lclMatchKeyword(OUString
& rName
, const ScCellKeywordHashMap
& aMap
,
83 OpCode eOpCode
, const lang::Locale
* pLocale
)
85 ScCellKeywordHashMap::const_iterator itrEnd
= aMap
.end();
86 ScCellKeywordHashMap::const_iterator itr
= aMap
.find(rName
);
88 if ( itr
== itrEnd
|| itr
->second
.empty() )
89 // No candidate strings exist. Bail out.
92 if ( eOpCode
== ocNone
&& !pLocale
)
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
);
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;
105 for (auto const& elem
: itr
->second
)
107 if ( eOpCode
!= ocNone
&& pLocale
)
109 if (elem
.meOpCode
== eOpCode
)
111 LocaleMatch eLevel
= lclLocaleCompare(elem
.mrLocale
, aLanguageTag
);
112 if ( eLevel
== LOCALE_MATCH_ALL
)
114 // Name with matching opcode and locale found.
115 rName
= OUString::createFromAscii( elem
.mpName
);
118 else if ( eLevel
> eLocaleMatchLevel
)
120 // Name with a better matching locale.
121 eLocaleMatchLevel
= eLevel
;
122 aBestMatchName
= elem
.mpName
;
124 else if ( !bOpCodeMatched
)
125 // At least the opcode matches.
126 aBestMatchName
= elem
.mpName
;
128 bOpCodeMatched
= true;
131 else if ( eOpCode
!= ocNone
&& !pLocale
)
133 if ( elem
.meOpCode
== eOpCode
)
135 // Name with a matching opcode preferred.
136 rName
= OUString::createFromAscii( elem
.mpName
);
142 LocaleMatch eLevel
= lclLocaleCompare(elem
.mrLocale
, aLanguageTag
);
143 if ( eLevel
== LOCALE_MATCH_ALL
)
145 // Name with matching locale preferred.
146 rName
= OUString::createFromAscii( elem
.mpName
);
149 else if ( eLevel
> eLocaleMatchLevel
)
151 // Name with a better matching locale.
152 eLocaleMatchLevel
= eLevel
;
153 aBestMatchName
= elem
.mpName
;
158 // No preferred strings found. Return the best matching name.
159 rName
= OUString::createFromAscii(aBestMatchName
);
162 void ScCellKeywordTranslator::transKeyword(OUString
& rName
, const lang::Locale
* pLocale
, OpCode eOpCode
)
165 spInstance
.reset( new ScCellKeywordTranslator
);
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
);
176 const sal_Unicode
* from
;
181 ScCellKeywordTranslator::ScCellKeywordTranslator() :
182 maTransWrapper( ::comphelper::getProcessComponentContext(),
183 TransliterationFlags::LOWERCASE_UPPERCASE
)
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.
189 // All keywords must be uppercase, and the mapping must be from the
190 // localized keyword to the English keyword.
192 // Make sure that the original keyword file (keywords_utf16.txt) is
193 // encoded in UCS-2/UTF-16!
195 #include "cellkeywords.inl"
198 ScCellKeywordTranslator::~ScCellKeywordTranslator()
202 void ScCellKeywordTranslator::addToMap(const OUString
& rKey
, const char* pName
, const lang::Locale
& rLocale
, OpCode eOpCode
)
204 ScCellKeyword
aKeyItem( pName
, eOpCode
, rLocale
);
206 ScCellKeywordHashMap::iterator itrEnd
= maStringNameMap
.end();
207 ScCellKeywordHashMap::iterator itr
= maStringNameMap
.find(rKey
);
212 std::vector
<ScCellKeyword
> aVector
{ aKeyItem
};
213 maStringNameMap
.emplace(rKey
, aVector
);
216 itr
->second
.push_back(aKeyItem
);
219 void ScCellKeywordTranslator::addToMap(const TransItem
* pItems
, const lang::Locale
& rLocale
)
221 for (sal_uInt16 i
= 0; pItems
[i
].from
!= nullptr; ++i
)
222 addToMap(OUString(pItems
[i
].from
), pItems
[i
].to
, rLocale
, pItems
[i
].func
);
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */