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 .
20 #ifndef INCLUDED_SVL_ONDEMAND_HXX
21 #define INCLUDED_SVL_ONDEMAND_HXX
24 #include <unotools/syslocale.hxx>
25 #include <i18nlangtag/lang.h>
26 #include <unotools/localedatawrapper.hxx>
27 #include <unotools/calendarwrapper.hxx>
28 #include <unotools/transliterationwrapper.hxx>
29 #include <unotools/nativenumberwrapper.hxx>
30 #include <com/sun/star/uno/Reference.hxx>
31 #include <i18nutil/transliteration.hxx>
34 On demand instantiation and initialization of several i18n wrappers,
35 helping the number formatter to not perform worse than it already does.
39 Switch between LANGUAGE_SYSTEM and LANGUAGE_ENGLISH_US and any other
41 SvNumberformatter uses it upon switching locales.
44 Avoids reloading and analysing of locale data again and again.
47 If the default ctor is used the init() method MUST be called before
48 accessing any locale data. The passed parameters Locale and LanguageType
49 must match each other.
52 class OnDemandLocaleDataWrapper
54 css::uno::Reference
< css::uno::XComponentContext
> m_xContext
;
55 SvtSysLocale aSysLocale
;
56 LanguageType eCurrentLanguage
;
57 LanguageType eLastAnyLanguage
;
58 const LocaleDataWrapper
* pSystem
;
59 std::unique_ptr
<const LocaleDataWrapper
> pEnglish
;
60 std::unique_ptr
< LocaleDataWrapper
> pAny
;
61 const LocaleDataWrapper
* pCurrent
;
65 OnDemandLocaleDataWrapper()
66 : eLastAnyLanguage( LANGUAGE_DONTKNOW
)
69 pCurrent
= pSystem
= aSysLocale
.GetLocaleDataPtr();
70 eCurrentLanguage
= LANGUAGE_SYSTEM
;
73 bool isInitialized() const { return bInitialized
; }
76 const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
,
77 const LanguageTag
& rLanguageTag
80 m_xContext
= rxContext
;
81 changeLocale( rLanguageTag
);
85 void changeLocale( const LanguageTag
& rLanguageTag
)
87 LanguageType eLang
= rLanguageTag
.getLanguageType( false);
88 if ( eLang
== LANGUAGE_SYSTEM
)
90 else if ( eLang
== LANGUAGE_ENGLISH_US
)
93 pEnglish
.reset( new LocaleDataWrapper( m_xContext
, rLanguageTag
) );
94 pCurrent
= pEnglish
.get();
100 pAny
.reset( new LocaleDataWrapper( m_xContext
, rLanguageTag
) );
101 eLastAnyLanguage
= eLang
;
103 else if ( eLastAnyLanguage
!= eLang
)
105 pAny
->setLanguageTag( rLanguageTag
);
106 eLastAnyLanguage
= eLang
;
108 pCurrent
= pAny
.get();
110 eCurrentLanguage
= eLang
;
113 LanguageType
getCurrentLanguage() const
114 { return eCurrentLanguage
; }
116 const LocaleDataWrapper
* get() const { return pCurrent
; }
117 const LocaleDataWrapper
* operator->() const { return get(); }
118 const LocaleDataWrapper
& operator*() const { return *get(); }
121 /** Load a calendar only if it's needed. Keep calendar for "en-US" locale
122 separately, as there can be alternation between locale dependent and
123 locale independent formats.
124 SvNumberformatter uses it upon switching locales.
126 @ATTENTION If the default ctor is used the init() method MUST be called
127 before accessing the calendar.
129 class OnDemandCalendarWrapper
131 css::uno::Reference
< css::uno::XComponentContext
> m_xContext
;
132 css::lang::Locale aEnglishLocale
;
133 css::lang::Locale aLocale
;
134 mutable css::lang::Locale aLastAnyLocale
;
135 mutable std::unique_ptr
<CalendarWrapper
> pEnglishPtr
;
136 mutable std::unique_ptr
<CalendarWrapper
> pAnyPtr
;
139 OnDemandCalendarWrapper()
141 LanguageTag
aEnglishLanguageTag(LANGUAGE_ENGLISH_US
);
142 aEnglishLocale
= aEnglishLanguageTag
.getLocale();
143 aLastAnyLocale
= aEnglishLocale
;
147 const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
,
148 const css::lang::Locale
& rLocale
151 m_xContext
= rxContext
;
152 changeLocale( rLocale
);
157 void changeLocale( const css::lang::Locale
& rLocale
)
162 CalendarWrapper
* get() const
164 CalendarWrapper
* pPtr
;
165 if ( aLocale
== aEnglishLocale
)
169 pEnglishPtr
.reset( new CalendarWrapper( m_xContext
));
170 pEnglishPtr
->loadDefaultCalendar( aEnglishLocale
);
172 pPtr
= pEnglishPtr
.get();
178 pAnyPtr
.reset(new CalendarWrapper( m_xContext
));
179 pAnyPtr
->loadDefaultCalendar(aLocale
);
180 aLastAnyLocale
= aLocale
;
182 else if ( aLocale
!= aLastAnyLocale
)
184 pAnyPtr
->loadDefaultCalendar( aLocale
);
185 aLastAnyLocale
= aLocale
;
187 pPtr
= pAnyPtr
.get();
194 /** Load a transliteration only if it's needed.
195 SvNumberformatter uses it upon switching locales.
196 @ATTENTION If the default ctor is used the init() method MUST be called
197 before accessing the transliteration.
199 class OnDemandTransliterationWrapper
201 css::uno::Reference
< css::uno::XComponentContext
> m_xContext
;
202 LanguageType eLanguage
;
203 TransliterationFlags nType
;
204 mutable std::unique_ptr
<::utl::TransliterationWrapper
>
210 OnDemandTransliterationWrapper()
211 : eLanguage( LANGUAGE_SYSTEM
)
212 , nType(TransliterationFlags::NONE
)
214 , bInitialized(false)
217 bool isInitialized() const { return bInitialized
; }
220 const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
,
224 m_xContext
= rxContext
;
225 nType
= TransliterationFlags::IGNORE_CASE
;
226 changeLocale( eLang
);
231 void changeLocale( LanguageType eLang
)
237 const ::utl::TransliterationWrapper
* get() const
242 pPtr
.reset( new ::utl::TransliterationWrapper( m_xContext
, nType
) );
243 pPtr
->loadModuleIfNeeded( eLanguage
);
249 const ::utl::TransliterationWrapper
* operator->() const { return get(); }
252 /** Load a native number service wrapper only if it's needed.
253 SvNumberformatter uses it.
256 If the default ctor is used the init() method MUST be called
257 before accessing the native number supplier.
259 class OnDemandNativeNumberWrapper
261 css::uno::Reference
< css::uno::XComponentContext
> m_xContext
;
262 mutable std::unique_ptr
<NativeNumberWrapper
>
266 OnDemandNativeNumberWrapper()
270 const css::uno::Reference
< css::uno::XComponentContext
>& rxContext
273 m_xContext
= rxContext
;
277 NativeNumberWrapper
* get() const
280 pPtr
.reset(new NativeNumberWrapper( m_xContext
));
286 #endif // INCLUDED_SVL_ONDEMAND_HXX
288 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */