xref: /trunk/main/vcl/source/app/i18nhelp.cxx (revision 9f62ea84)
1*9f62ea84SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9f62ea84SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9f62ea84SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9f62ea84SAndrew Rist  * distributed with this work for additional information
6*9f62ea84SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9f62ea84SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9f62ea84SAndrew Rist  * "License"); you may not use this file except in compliance
9*9f62ea84SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*9f62ea84SAndrew Rist  *
11*9f62ea84SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*9f62ea84SAndrew Rist  *
13*9f62ea84SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9f62ea84SAndrew Rist  * software distributed under the License is distributed on an
15*9f62ea84SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9f62ea84SAndrew Rist  * KIND, either express or implied.  See the License for the
17*9f62ea84SAndrew Rist  * specific language governing permissions and limitations
18*9f62ea84SAndrew Rist  * under the License.
19*9f62ea84SAndrew Rist  *
20*9f62ea84SAndrew Rist  *************************************************************/
21*9f62ea84SAndrew Rist 
22*9f62ea84SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_vcl.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "unotools/localedatawrapper.hxx"
28cdf0e10cSrcweir #include "unotools/transliterationwrapper.hxx"
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include "i18npool/mslangid.hxx"
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include "rtl/ustrbuf.hxx"
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include "vcl/i18nhelp.hxx"
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include "com/sun/star/lang/XMultiServiceFactory.hpp"
37cdf0e10cSrcweir #include "com/sun/star/i18n/TransliterationModules.hpp"
38cdf0e10cSrcweir 
39cdf0e10cSrcweir using namespace ::com::sun::star;
40cdf0e10cSrcweir 
I18nHelper(::com::sun::star::uno::Reference<::com::sun::star::lang::XMultiServiceFactory> & rxMSF,const::com::sun::star::lang::Locale & rLocale)41cdf0e10cSrcweir vcl::I18nHelper::I18nHelper(  ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& rxMSF, const ::com::sun::star::lang::Locale& rLocale )
42cdf0e10cSrcweir {
43cdf0e10cSrcweir     mxMSF = rxMSF;
44cdf0e10cSrcweir     maLocale = rLocale;
45cdf0e10cSrcweir     mpLocaleDataWrapper = NULL;
46cdf0e10cSrcweir     mpTransliterationWrapper= NULL;
47cdf0e10cSrcweir     mbTransliterateIgnoreCase = sal_False;
48cdf0e10cSrcweir }
49cdf0e10cSrcweir 
~I18nHelper()50cdf0e10cSrcweir vcl::I18nHelper::~I18nHelper()
51cdf0e10cSrcweir {
52cdf0e10cSrcweir     ImplDestroyWrappers();
53cdf0e10cSrcweir }
54cdf0e10cSrcweir 
ImplDestroyWrappers()55cdf0e10cSrcweir void vcl::I18nHelper::ImplDestroyWrappers()
56cdf0e10cSrcweir {
57cdf0e10cSrcweir     delete mpLocaleDataWrapper;
58cdf0e10cSrcweir     mpLocaleDataWrapper = NULL;
59cdf0e10cSrcweir 
60cdf0e10cSrcweir     delete mpTransliterationWrapper;
61cdf0e10cSrcweir     mpTransliterationWrapper= NULL;
62cdf0e10cSrcweir }
63cdf0e10cSrcweir 
ImplGetTransliterationWrapper() const64cdf0e10cSrcweir utl::TransliterationWrapper& vcl::I18nHelper::ImplGetTransliterationWrapper() const
65cdf0e10cSrcweir {
66cdf0e10cSrcweir     if ( !mpTransliterationWrapper )
67cdf0e10cSrcweir     {
68cdf0e10cSrcweir         sal_Int32 nModules = i18n::TransliterationModules_IGNORE_WIDTH;
69cdf0e10cSrcweir         if ( mbTransliterateIgnoreCase )
70cdf0e10cSrcweir             nModules |= i18n::TransliterationModules_IGNORE_CASE;
71cdf0e10cSrcweir 
72cdf0e10cSrcweir         ((vcl::I18nHelper*)this)->mpTransliterationWrapper = new utl::TransliterationWrapper( mxMSF, (i18n::TransliterationModules)nModules );
73cdf0e10cSrcweir         ((vcl::I18nHelper*)this)->mpTransliterationWrapper->loadModuleIfNeeded( MsLangId::convertLocaleToLanguage( maLocale ) );
74cdf0e10cSrcweir     }
75cdf0e10cSrcweir     return *mpTransliterationWrapper;
76cdf0e10cSrcweir }
77cdf0e10cSrcweir 
ImplGetLocaleDataWrapper() const78cdf0e10cSrcweir LocaleDataWrapper& vcl::I18nHelper::ImplGetLocaleDataWrapper() const
79cdf0e10cSrcweir {
80cdf0e10cSrcweir     if ( !mpLocaleDataWrapper )
81cdf0e10cSrcweir     {
82cdf0e10cSrcweir         ((vcl::I18nHelper*)this)->mpLocaleDataWrapper = new LocaleDataWrapper( mxMSF, maLocale );
83cdf0e10cSrcweir     }
84cdf0e10cSrcweir     return *mpLocaleDataWrapper;
85cdf0e10cSrcweir }
86cdf0e10cSrcweir 
getLocale() const87cdf0e10cSrcweir const ::com::sun::star::lang::Locale& vcl::I18nHelper::getLocale() const
88cdf0e10cSrcweir {
89cdf0e10cSrcweir     return maLocale;
90cdf0e10cSrcweir }
91cdf0e10cSrcweir 
is_formatting_mark(sal_Unicode c)92cdf0e10cSrcweir inline bool is_formatting_mark( sal_Unicode c )
93cdf0e10cSrcweir {
94cdf0e10cSrcweir 	if( (c >= 0x200B) && (c <= 0x200F) )	// BiDi and zero-width-markers
95cdf0e10cSrcweir 		return true;
96cdf0e10cSrcweir 	if( (c >= 0x2028) && (c <= 0x202E) )	// BiDi and paragraph-markers
97cdf0e10cSrcweir 		return true;
98cdf0e10cSrcweir 	return false;
99cdf0e10cSrcweir }
100cdf0e10cSrcweir 
101cdf0e10cSrcweir /* #i100057# filter formatting marks out of strings before passing them to
102cdf0e10cSrcweir    the transliteration. The real solution would have been an additional TransliterationModule
103cdf0e10cSrcweir    to ignore these marks during transliteration; however changin the code in i18npool that actually
104cdf0e10cSrcweir    implements this could produce unwanted side effects.
105cdf0e10cSrcweir 
106cdf0e10cSrcweir    Of course this copying around is not really good, but looking at i18npool, one more time
107cdf0e10cSrcweir    will not hurt.
108cdf0e10cSrcweir */
filterFormattingChars(const String & rStr)109cdf0e10cSrcweir String vcl::I18nHelper::filterFormattingChars( const String& rStr )
110cdf0e10cSrcweir {
111cdf0e10cSrcweir     sal_Int32 nUnicodes = rStr.Len();
112cdf0e10cSrcweir     rtl::OUStringBuffer aBuf( nUnicodes );
113cdf0e10cSrcweir     const sal_Unicode* pStr = rStr.GetBuffer();
114cdf0e10cSrcweir     while( nUnicodes-- )
115cdf0e10cSrcweir     {
116cdf0e10cSrcweir         if( ! is_formatting_mark( *pStr ) )
117cdf0e10cSrcweir             aBuf.append( *pStr );
118cdf0e10cSrcweir         pStr++;
119cdf0e10cSrcweir     }
120cdf0e10cSrcweir     return aBuf.makeStringAndClear();
121cdf0e10cSrcweir }
122cdf0e10cSrcweir 
CompareString(const String & rStr1,const String & rStr2) const123cdf0e10cSrcweir sal_Int32 vcl::I18nHelper::CompareString( const String& rStr1, const String& rStr2 ) const
124cdf0e10cSrcweir {
125cdf0e10cSrcweir 	::osl::Guard< ::osl::Mutex > aGuard( ((vcl::I18nHelper*)this)->maMutex );
126cdf0e10cSrcweir 
127cdf0e10cSrcweir     if ( mbTransliterateIgnoreCase )
128cdf0e10cSrcweir     {
129cdf0e10cSrcweir         // Change mbTransliterateIgnoreCase and destroy the warpper, next call to
130cdf0e10cSrcweir         // ImplGetTransliterationWrapper() will create a wrapper with the correct bIgnoreCase
131cdf0e10cSrcweir         ((vcl::I18nHelper*)this)->mbTransliterateIgnoreCase = sal_False;
132cdf0e10cSrcweir         delete ((vcl::I18nHelper*)this)->mpTransliterationWrapper;
133cdf0e10cSrcweir         ((vcl::I18nHelper*)this)->mpTransliterationWrapper = NULL;
134cdf0e10cSrcweir     }
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 
137cdf0e10cSrcweir     String aStr1( filterFormattingChars(rStr1) );
138cdf0e10cSrcweir     String aStr2( filterFormattingChars(rStr2) );
139cdf0e10cSrcweir     return ImplGetTransliterationWrapper().compareString( aStr1, aStr2 );
140cdf0e10cSrcweir }
141cdf0e10cSrcweir 
MatchString(const String & rStr1,const String & rStr2) const142cdf0e10cSrcweir sal_Bool vcl::I18nHelper::MatchString( const String& rStr1, const String& rStr2 ) const
143cdf0e10cSrcweir {
144cdf0e10cSrcweir 	::osl::Guard< ::osl::Mutex > aGuard( ((vcl::I18nHelper*)this)->maMutex );
145cdf0e10cSrcweir 
146cdf0e10cSrcweir     if ( !mbTransliterateIgnoreCase )
147cdf0e10cSrcweir     {
148cdf0e10cSrcweir         // Change mbTransliterateIgnoreCase and destroy the warpper, next call to
149cdf0e10cSrcweir         // ImplGetTransliterationWrapper() will create a wrapper with the correct bIgnoreCase
150cdf0e10cSrcweir         ((vcl::I18nHelper*)this)->mbTransliterateIgnoreCase = sal_True;
151cdf0e10cSrcweir         delete ((vcl::I18nHelper*)this)->mpTransliterationWrapper;
152cdf0e10cSrcweir         ((vcl::I18nHelper*)this)->mpTransliterationWrapper = NULL;
153cdf0e10cSrcweir     }
154cdf0e10cSrcweir 
155cdf0e10cSrcweir     String aStr1( filterFormattingChars(rStr1) );
156cdf0e10cSrcweir     String aStr2( filterFormattingChars(rStr2) );
157cdf0e10cSrcweir     return ImplGetTransliterationWrapper().isMatch( aStr1, aStr2 );
158cdf0e10cSrcweir }
159cdf0e10cSrcweir 
MatchMnemonic(const String & rString,sal_Unicode cMnemonicChar) const160cdf0e10cSrcweir sal_Bool vcl::I18nHelper::MatchMnemonic( const String& rString, sal_Unicode cMnemonicChar ) const
161cdf0e10cSrcweir {
162cdf0e10cSrcweir 	::osl::Guard< ::osl::Mutex > aGuard( ((vcl::I18nHelper*)this)->maMutex );
163cdf0e10cSrcweir 
164cdf0e10cSrcweir     sal_Bool bEqual = sal_False;
165cdf0e10cSrcweir     sal_uInt16 n = rString.Search( '~' );
166cdf0e10cSrcweir     if ( n != STRING_NOTFOUND )
167cdf0e10cSrcweir     {
168cdf0e10cSrcweir         String aMatchStr( rString, n+1, STRING_LEN );   // not only one char, because of transliteration...
169cdf0e10cSrcweir         bEqual = MatchString( cMnemonicChar, aMatchStr );
170cdf0e10cSrcweir     }
171cdf0e10cSrcweir     return bEqual;
172cdf0e10cSrcweir }
173cdf0e10cSrcweir 
174cdf0e10cSrcweir 
GetDate(const Date & rDate) const175cdf0e10cSrcweir String vcl::I18nHelper::GetDate( const Date& rDate ) const
176cdf0e10cSrcweir {
177cdf0e10cSrcweir 	::osl::Guard< ::osl::Mutex > aGuard( ((vcl::I18nHelper*)this)->maMutex );
178cdf0e10cSrcweir 
179cdf0e10cSrcweir     return ImplGetLocaleDataWrapper().getDate( rDate );
180cdf0e10cSrcweir }
181cdf0e10cSrcweir 
GetNum(long nNumber,sal_uInt16 nDecimals,sal_Bool bUseThousandSep,sal_Bool bTrailingZeros) const182cdf0e10cSrcweir String vcl::I18nHelper::GetNum( long nNumber, sal_uInt16 nDecimals, sal_Bool bUseThousandSep, sal_Bool bTrailingZeros ) const
183cdf0e10cSrcweir {
184cdf0e10cSrcweir     return ImplGetLocaleDataWrapper().getNum( nNumber, nDecimals, bUseThousandSep, bTrailingZeros );
185cdf0e10cSrcweir }
186