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_UNOTOOLS_LOCALEDATAWRAPPER_HXX
21 #define INCLUDED_UNOTOOLS_LOCALEDATAWRAPPER_HXX
23 #include <com/sun/star/uno/Reference.hxx>
24 #include <com/sun/star/uno/Sequence.hxx>
25 #include <com/sun/star/i18n/LocaleItem.hpp>
26 #include <com/sun/star/i18n/LocaleDataItem2.hpp>
27 #include <com/sun/star/i18n/LanguageCountryInfo.hpp>
28 #include <com/sun/star/i18n/ForbiddenCharacters.hpp>
29 #include <com/sun/star/i18n/reservedWords.hpp>
30 #include <rtl/ustring.hxx>
32 #include <i18nlangtag/languagetag.hxx>
33 #include <unotools/readwritemutexguard.hxx>
34 #include <unotools/unotoolsdllapi.h>
38 namespace com::sun::star::uno
{ class XComponentContext
; }
39 namespace com::sun::star::i18n
{ class XLocaleData5
; }
40 namespace com::sun::star::i18n
{ struct Calendar2
; }
41 namespace com::sun::star::i18n
{ struct Currency2
; }
42 namespace com::sun::star::i18n
{ struct FormatElement
; }
43 namespace com::sun::star::i18n
{ struct CalendarItem2
; }
46 namespace tools
{ class Time
; }
47 class CalendarWrapper
;
49 enum class DateOrder
{
56 enum class MeasurementSystem
{
61 class UNOTOOLS_DLLPUBLIC LocaleDataWrapper
63 static sal_uInt8 nLocaleDataChecking
; // 0:=dontknow, 1:=yes, 2:=no
65 css::uno::Reference
< css::uno::XComponentContext
> m_xContext
;
66 css::uno::Reference
< css::i18n::XLocaleData5
> xLD
;
67 LanguageTag maLanguageTag
;
68 std::shared_ptr
< css::i18n::Calendar2
> xDefaultCalendar
;
69 std::shared_ptr
< css::i18n::Calendar2
> xSecondaryCalendar
;
70 css::i18n::LocaleDataItem2 aLocaleDataItem
;
71 css::uno::Sequence
< OUString
> aReservedWordSeq
;
72 css::uno::Sequence
< OUString
> aDateAcceptancePatterns
;
73 css::uno::Sequence
< sal_Int32
> aGrouping
;
75 OUString aLocaleItem
[css::i18n::LocaleItem::COUNT2
];
76 OUString aReservedWord
[css::i18n::reservedWords::COUNT
];
78 OUString aCurrBankSymbol
;
80 DateOrder nLongDateOrder
;
81 sal_uInt16 nCurrPositiveFormat
;
82 sal_uInt16 nCurrNegativeFormat
;
83 sal_uInt16 nCurrDigits
;
84 bool bLocaleDataItemValid
;
85 bool bReservedWordValid
;
86 bool bSecondaryCalendarValid
;
87 mutable ::utl::ReadWriteMutex aMutex
;
88 struct SAL_DLLPRIVATE Locale_Compare
90 bool operator()(const css::lang::Locale
& rLocale1
, const css::lang::Locale
& rLocale2
) const;
92 mutable std::map
<css::lang::Locale
, css::i18n::LocaleDataItem2
, Locale_Compare
> maDataItemCache
;
94 // whenever Locale changes
95 void invalidateData();
97 void getOneLocaleItemImpl( sal_Int16 nItem
);
98 const OUString
& getOneLocaleItem( sal_Int16 nItem
) const;
100 void getOneReservedWordImpl( sal_Int16 nWord
);
101 const OUString
& getOneReservedWord( sal_Int16 nWord
) const;
103 void getCurrSymbolsImpl();
104 void getCurrFormatsImpl();
106 void scanCurrFormatImpl( const OUString
& rCode
,
107 sal_Int32 nStart
, sal_Int32
& nSign
,
108 sal_Int32
& nPar
, sal_Int32
& nNum
,
109 sal_Int32
& nBlank
, sal_Int32
& nSym
) const;
111 void getDateOrdersImpl();
112 DateOrder
scanDateOrderImpl( const OUString
& rCode
) const;
114 void getDefaultCalendarImpl();
115 void getSecondaryCalendarImpl();
117 void ImplAddFormatNum( rtl::OUStringBuffer
& rBuf
,
118 sal_Int64 nNumber
, sal_uInt16 nDecimals
,
119 bool bUseThousandSep
, bool bTrailingZeros
) const;
121 void getDigitGroupingImpl();
125 const css::uno::Reference
< css::uno::XComponentContext
> & rxContext
,
126 const LanguageTag
& rLanguageTag
129 const LanguageTag
& rLanguageTag
131 ~LocaleDataWrapper();
133 /** Get the service factory, meant to be able to create a CalendarWrapper
134 from a LocaleDataWrapper. Note that the service factory may be
135 non-existent if this LocaleDataWrapper was created without one and
136 lives "on the grassland". The CalendarWrapper ctor can handle that
138 const css::uno::Reference
<
139 css::uno::XComponentContext
> & getComponentContext()
140 const { return m_xContext
; }
142 /// set a new Locale to request
143 void setLanguageTag( const LanguageTag
& rLanguageTag
);
145 /// get current requested Locale
146 const LanguageTag
& getLanguageTag() const;
148 /// get current loaded Locale, which might differ from the requested Locale
149 LanguageTag
getLoadedLanguageTag() const;
151 // Wrapper implementations of service LocaleData
153 css::i18n::LanguageCountryInfo
getLanguageCountryInfo() const;
154 /// NOTE: this wraps XLocaleData5::getLocaleItem2() in fact.
155 const css::i18n::LocaleDataItem2
& getLocaleItem() const;
156 /// NOTE: this wraps XLocaleData3::getAllCalendars2() in fact.
157 css::uno::Sequence
< css::i18n::Calendar2
> getAllCalendars() const;
158 /// NOTE: this wraps XLocaleData2::getAllCurrencies2() in fact.
159 css::uno::Sequence
< css::i18n::Currency2
> getAllCurrencies() const;
160 css::uno::Sequence
< css::i18n::FormatElement
> getAllFormats() const;
161 css::i18n::ForbiddenCharacters
getForbiddenCharacters() const;
162 css::uno::Sequence
< OUString
> getReservedWord() const;
163 css::uno::Sequence
< css::lang::Locale
> getAllInstalledLocaleNames() const;
164 css::uno::Sequence
< OUString
> getDateAcceptancePatterns() const;
166 /** Override locale's date acceptance patterns.
167 An empty sequence resets the patterns to the locale's pattern sequence.
169 void setDateAcceptancePatterns( const css::uno::Sequence
< OUString
> & rPatterns
);
171 /// same as the wrapper implementation but static
172 static css::uno::Sequence
< css::lang::Locale
> getInstalledLocaleNames();
174 /** Get LanguageTypes for all installed locales which are unambiguous
175 convertible back and forth between locale ISO strings and MS-LCID
176 LanguageType. Upon the first time the function is called when
177 locale data checking is enabled, messages are shown for locales not
178 matching, excluding already known problems.
179 (e.g. used in number formatter dialog init)
181 static std::vector
< LanguageType
> getInstalledLanguageTypes();
183 /// maps the LocaleData string to the International enum
184 MeasurementSystem
mapMeasurementStringToEnum( const OUString
& rMS
) const;
186 /// Convenience method to obtain the default calendar.
187 const std::shared_ptr
< css::i18n::Calendar2
>& getDefaultCalendar() const;
189 /// Convenience method to obtain the day names of the default calendar.
190 css::uno::Sequence
< css::i18n::CalendarItem2
> const & getDefaultCalendarDays() const;
192 /// Convenience method to obtain the month names of the default calendar.
193 css::uno::Sequence
< css::i18n::CalendarItem2
> const & getDefaultCalendarMonths() const;
195 /** If the secondary calendar, if any, is of the name passed AND number
196 formats using it usually use the E or EE keyword (EC|EEC). */
197 bool doesSecondaryCalendarUseEC( std::u16string_view rName
) const;
199 /** Obtain digit grouping. The usually known grouping by thousands (#,###)
200 is actually only one of possible groupings. Another one, for example,
201 used in India is group by 3 and then by 2 indefinitely (#,##,###). The
202 integer sequence returned here specifies grouping from right to left
203 (!), with a 0 entry designating the end of rules and the previous value
204 to be repeated indefinitely. Hence the sequence {3,0} specifies the
205 usual grouping by thousands, whereas the sequence {3,2,0} specifies
206 Indian grouping. The sal_Int32* getConstArray() can be passed directly
207 to the ::rtl::math::doubleToString() methods as argument for the
208 pGroups parameter. */
209 css::uno::Sequence
< sal_Int32
> getDigitGrouping() const;
211 // Functionality of class International methods, LocaleItem
213 const OUString
& getDateSep() const
214 { return getOneLocaleItem( css::i18n::LocaleItem::DATE_SEPARATOR
); }
215 const OUString
& getNumThousandSep() const
216 { return getOneLocaleItem( css::i18n::LocaleItem::THOUSAND_SEPARATOR
); }
217 const OUString
& getNumDecimalSep() const
218 { return getOneLocaleItem( css::i18n::LocaleItem::DECIMAL_SEPARATOR
); }
219 const OUString
& getNumDecimalSepAlt() const
220 { return getOneLocaleItem( css::i18n::LocaleItem::DECIMAL_SEPARATOR_ALTERNATIVE
); }
221 const OUString
& getTimeSep() const
222 { return getOneLocaleItem( css::i18n::LocaleItem::TIME_SEPARATOR
); }
223 const OUString
& getTime100SecSep() const
224 { return getOneLocaleItem( css::i18n::LocaleItem::TIME_100SEC_SEPARATOR
); }
225 const OUString
& getListSep() const
226 { return getOneLocaleItem( css::i18n::LocaleItem::LIST_SEPARATOR
); }
227 const OUString
& getQuotationMarkStart() const
228 { return getOneLocaleItem( css::i18n::LocaleItem::SINGLE_QUOTATION_START
); }
229 const OUString
& getQuotationMarkEnd() const
230 { return getOneLocaleItem( css::i18n::LocaleItem::SINGLE_QUOTATION_END
); }
231 const OUString
& getDoubleQuotationMarkStart() const
232 { return getOneLocaleItem( css::i18n::LocaleItem::DOUBLE_QUOTATION_START
); }
233 const OUString
& getDoubleQuotationMarkEnd() const
234 { return getOneLocaleItem( css::i18n::LocaleItem::DOUBLE_QUOTATION_END
); }
235 MeasurementSystem
getMeasurementSystemEnum() const
236 { return mapMeasurementStringToEnum( getOneLocaleItem( css::i18n::LocaleItem::MEASUREMENT_SYSTEM
) ); }
237 const OUString
& getTimeAM() const
238 { return getOneLocaleItem( css::i18n::LocaleItem::TIME_AM
); }
239 const OUString
& getTimePM() const
240 { return getOneLocaleItem( css::i18n::LocaleItem::TIME_PM
); }
241 const OUString
& getLongDateDayOfWeekSep() const
242 { return getOneLocaleItem( css::i18n::LocaleItem::LONG_DATE_DAY_OF_WEEK_SEPARATOR
); }
243 const OUString
& getLongDateDaySep() const
244 { return getOneLocaleItem( css::i18n::LocaleItem::LONG_DATE_DAY_SEPARATOR
); }
245 const OUString
& getLongDateMonthSep() const
246 { return getOneLocaleItem( css::i18n::LocaleItem::LONG_DATE_MONTH_SEPARATOR
); }
247 const OUString
& getLongDateYearSep() const
248 { return getOneLocaleItem( css::i18n::LocaleItem::LONG_DATE_YEAR_SEPARATOR
); }
250 /** A wrapper around rtl::math::stringToDouble() using the locale dependent
251 decimal separator, group separator, and if needed decimal separator
254 The decimal separator is tried first, if the conversion does not match
255 the entire string then the decimal separator alternative is tried if it
256 occurs in the string and was the reason to stop.
258 Leading blanks are skipped, trailing blanks are not skipped. The number
259 is parsed up to the first non-floating point number character, same as
260 rtl::math::stringToDouble() does. The caller is responsible for proper
261 error checking and end comparison.
264 The string to parse as floating point number.
266 Whether group separator is used/accepted during parsing.
268 Pointer to receive the conversion status as in
269 rtl::math::stringToDouble().
271 Pointer to receive the parse end (exclusive) as in
272 rtl::math::stringToDouble().
273 @return The floating point number as parsed.
275 double stringToDouble( const OUString
& rString
, bool bUseGroupSep
,
276 rtl_math_ConversionStatus
* pStatus
, sal_Int32
* pParseEnd
) const;
278 /** A wrapper around rtl_math_uStringToDouble() using the locale dependent
279 decimal separator, group separator, and if needed decimal separator
282 The decimal separator is tried first, if the conversion does not match
283 the entire string then the decimal separator alternative is tried if it
284 occurs in the string and was the reason to stop.
286 Leading blanks are skipped, trailing blanks are not skipped. The number
287 is parsed up to the first non-floating point number character, same as
288 rtl_math_uStringToDouble() does. The caller is responsible for proper
289 error checking and end comparison.
292 The string position to start parsing a floating point number.
294 The string position to stop parsing, exclusive.
296 Whether group separator is used/accepted during parsing.
298 Pointer to receive the conversion status as in
299 rtl_math_uStringToDouble().
301 Pointer to receive the parse end (exclusive) as in
302 rtl_math_uStringToDouble().
303 @return The floating point number as parsed.
305 double stringToDouble( const sal_Unicode
* pBegin
, const sal_Unicode
* pEnd
, bool bUseGroupSep
,
306 rtl_math_ConversionStatus
* pStatus
, const sal_Unicode
** ppParseEnd
) const;
309 const OUString
& getCurrSymbol() const;
310 const OUString
& getCurrBankSymbol() const;
311 sal_uInt16
getCurrPositiveFormat() const;
312 sal_uInt16
getCurrNegativeFormat() const;
313 sal_uInt16
getCurrDigits() const;
315 // simple date and time formatting
316 DateOrder
getDateOrder() const;
317 DateOrder
getLongDateOrder() const;
318 /// only numerical values of Gregorian calendar
319 OUString
getDate( const Date
& rDate
) const;
320 OUString
getTime( const tools::Time
& rTime
, bool bSec
= true,
321 bool b100Sec
= false ) const;
322 OUString
getDuration( const tools::Time
& rTime
,
323 bool bSec
= true, bool b100Sec
= false ) const;
325 /** The CalendarWrapper already <b>MUST</b>
326 have loaded a calendar.
328 <FALSE/> := full year
329 <TRUE/> := year % 100
331 OUString
getLongDate( const Date
& rDate
,
332 CalendarWrapper
& rCal
,
336 /** Simple number formatting
338 value * 10**nDecimals
339 @param bTrailingZeros
340 </sal_True> := always display trailing zeros in
341 decimal places, even if integer value.
342 </sal_False> := trailing zeros are only displayed
343 if the value is not an integer value.
345 OUString
getNum( sal_Int64 nNumber
, sal_uInt16 nDecimals
,
346 bool bUseThousandSep
= true,
347 bool bTrailingZeros
= true ) const;
349 /// "Secure" currency formatted string.
350 OUString
getCurr( sal_Int64 nNumber
, sal_uInt16 nDecimals
,
351 const OUString
& rCurrencySymbol
,
352 bool bUseThousandSep
= true ) const;
354 // dummy returns, to be implemented
355 sal_Unicode
getCurrZeroChar() const
357 static bool isNumLeadingZero()
359 /// standard decimal places
360 static sal_uInt16
getNumDigits()
362 static bool isNumTrailingZeros()
367 const OUString
& getTrueWord() const
368 { return getOneReservedWord( css::i18n::reservedWords::TRUE_WORD
); }
369 const OUString
& getFalseWord() const
370 { return getOneReservedWord( css::i18n::reservedWords::FALSE_WORD
); }
371 const OUString
& getAboveWord() const
372 { return getOneReservedWord( css::i18n::reservedWords::ABOVE_WORD
); }
373 const OUString
& getBelowWord() const
374 { return getOneReservedWord( css::i18n::reservedWords::BELOW_WORD
); }
375 /// return a quarter abbreviation string matching nQuarter (0..3) => "Q1" .. "Q2"
376 const OUString
& getQuarterAbbreviation( sal_Int16 nQuarter
) const
377 { return getOneReservedWord( css::i18n::reservedWords::QUARTER1_ABBREVIATION
+ nQuarter
); }
379 /** Return whether locale data checks are enabled.
380 Checks are enabled if the environment variable
381 OOO_ENABLE_LOCALE_DATA_CHECKS is set to 'Y' or 'Yes' (or any other
382 string starting with 'Y') or '1'.
383 Also used in conjunction with the number formatter. */
384 static bool areChecksEnabled()
386 if (nLocaleDataChecking
== 0)
387 evaluateLocaleDataChecking();
388 return nLocaleDataChecking
== 1;
391 /** Append locale info to string, used with locale data checking.
392 A string similar to "de_DE requested\n en_US loaded" is appended. */
393 OUString
appendLocaleInfo(const OUString
& rDebugMsg
) const;
395 /** Output a message during locale data checking. The (UTF-8) string is
396 written to stderr and in a non-product build or if DBG_UTIL is enabled
397 also raised as an assertion message box. */
398 static void outputCheckMessage( const OUString
& rMsg
);
399 static void outputCheckMessage( const char* pStr
);
401 LocaleDataWrapper(const LocaleDataWrapper
&) = delete;
402 LocaleDataWrapper
& operator=(const LocaleDataWrapper
&) = delete;
406 const css::lang::Locale
& getMyLocale() const;
408 static void evaluateLocaleDataChecking();
411 #endif // INCLUDED_UNOTOOLS_LOCALEDATAWRAPPER_HXX
413 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */