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/unotoolsdllapi.h>
35 #include <string_view>
37 namespace com::sun::star::uno
{ class XComponentContext
; }
38 namespace com::sun::star::i18n
{ class XLocaleData5
; }
39 namespace com::sun::star::i18n
{ struct Calendar2
; }
40 namespace com::sun::star::i18n
{ struct Currency2
; }
41 namespace com::sun::star::i18n
{ struct FormatElement
; }
42 namespace com::sun::star::i18n
{ struct CalendarItem2
; }
50 class CalendarWrapper
;
52 enum class DateOrder
{
59 enum class LongDateOrder
{
67 enum class MeasurementSystem
{
73 * This class can be accessed without locking because we load
74 * all of the data in the constructor.
76 class UNOTOOLS_DLLPUBLIC LocaleDataWrapper
78 static sal_uInt8 nLocaleDataChecking
; // 0:=dontknow, 1:=yes, 2:=no
80 css::uno::Reference
< css::uno::XComponentContext
> m_xContext
;
81 css::uno::Reference
< css::i18n::XLocaleData5
> xLD
;
82 LanguageTag maLanguageTag
;
83 std::shared_ptr
< css::i18n::Calendar2
> xDefaultCalendar
;
84 std::shared_ptr
< css::i18n::Calendar2
> xSecondaryCalendar
;
85 css::i18n::LocaleDataItem2 aLocaleDataItem
;
86 css::uno::Sequence
< OUString
> aDateAcceptancePatterns
;
87 css::uno::Sequence
< sal_Int32
> aGrouping
;
89 OUString aLocaleItem
[css::i18n::LocaleItem::COUNT2
];
90 std::vector
<OUString
> aReservedWords
;
92 OUString aCurrBankSymbol
;
94 LongDateOrder nLongDateOrder
;
95 sal_uInt16 nCurrPositiveFormat
;
96 sal_uInt16 nCurrNegativeFormat
;
97 sal_uInt16 nCurrDigits
;
99 SAL_DLLPRIVATE
void loadData();
100 SAL_DLLPRIVATE
void loadDateAcceptancePatterns(const std::vector
<OUString
> & rOverrideDateAcceptancePatterns
);
102 const OUString
& getOneLocaleItem( sal_Int16 nItem
) const;
104 const OUString
& getOneReservedWord( sal_Int16 nWord
) const;
106 SAL_DLLPRIVATE
void loadCurrencyFormats();
108 SAL_DLLPRIVATE
void scanCurrFormatImpl( std::u16string_view rCode
,
109 sal_Int32 nStart
, sal_Int32
& nSign
,
110 sal_Int32
& nPar
, sal_Int32
& nNum
,
111 sal_Int32
& nBlank
, sal_Int32
& nSym
) const;
113 SAL_DLLPRIVATE
void loadDateOrders();
114 SAL_DLLPRIVATE LongDateOrder
scanDateOrderImpl( std::u16string_view rCode
) const;
116 SAL_DLLPRIVATE
void ImplAddFormatNum( rtl::OUStringBuffer
& rBuf
,
117 sal_Int64 nNumber
, sal_uInt16 nDecimals
,
118 bool bUseThousandSep
, bool bTrailingZeros
) const;
120 SAL_DLLPRIVATE
void loadDigitGrouping();
124 const css::uno::Reference
< css::uno::XComponentContext
> & rxContext
,
125 LanguageTag aLanguageTag
128 @param rOverrideDateAcceptancePatterns Override locale's date acceptance patterns.
129 An empty sequence resets the patterns to the locale's pattern sequence.
132 LanguageTag aLanguageTag
,
133 const std::vector
<OUString
> & rOverrideDateAcceptancePatterns
= {}
135 ~LocaleDataWrapper();
137 /** Get the service factory, meant to be able to create a CalendarWrapper
138 from a LocaleDataWrapper. Note that the service factory may be
139 non-existent if this LocaleDataWrapper was created without one and
140 lives "on the grassland". The CalendarWrapper ctor can handle that
142 const css::uno::Reference
<
143 css::uno::XComponentContext
> & getComponentContext()
144 const { return m_xContext
; }
146 /// get current requested Locale
147 const LanguageTag
& getLanguageTag() const;
149 /// get current loaded Locale, which might differ from the requested Locale
150 LanguageTag
getLoadedLanguageTag() const;
152 // Wrapper implementations of service LocaleData
154 SAL_DLLPRIVATE
css::i18n::LanguageCountryInfo
getLanguageCountryInfo() const;
155 /// NOTE: this wraps XLocaleData5::getLocaleItem2() in fact.
156 const css::i18n::LocaleDataItem2
& getLocaleItem() const;
157 /// NOTE: this wraps XLocaleData3::getAllCalendars2() in fact.
158 css::uno::Sequence
< css::i18n::Calendar2
> getAllCalendars() const;
159 /// NOTE: this wraps XLocaleData2::getAllCurrencies2() in fact.
160 css::uno::Sequence
< css::i18n::Currency2
> getAllCurrencies() const;
161 css::uno::Sequence
< css::i18n::FormatElement
> getAllFormats() const;
162 css::i18n::ForbiddenCharacters
getForbiddenCharacters() const;
163 SAL_DLLPRIVATE
const css::uno::Sequence
< css::lang::Locale
> & getAllInstalledLocaleNames() const;
164 const css::uno::Sequence
< OUString
> & getDateAcceptancePatterns() const;
167 /// same as the wrapper implementation but static
168 static const css::uno::Sequence
< css::lang::Locale
> & getInstalledLocaleNames();
170 /** Get LanguageTypes for all installed locales which are unambiguous
171 convertible back and forth between locale ISO strings and MS-LCID
172 LanguageType. Upon the first time the function is called when
173 locale data checking is enabled, messages are shown for locales not
174 matching, excluding already known problems.
175 (e.g. used in number formatter dialog init)
177 static const std::vector
< LanguageType
> & getInstalledLanguageTypes();
179 /// maps the LocaleData string to the International enum
180 static MeasurementSystem
mapMeasurementStringToEnum( std::u16string_view rMS
);
182 /// Convenience method to obtain the default calendar.
183 const std::shared_ptr
< css::i18n::Calendar2
>& getDefaultCalendar() const;
185 /// Convenience method to obtain the day names of the default calendar.
186 css::uno::Sequence
< css::i18n::CalendarItem2
> const & getDefaultCalendarDays() const;
188 /// Convenience method to obtain the month names of the default calendar.
189 css::uno::Sequence
< css::i18n::CalendarItem2
> const & getDefaultCalendarMonths() const;
191 /** If the secondary calendar, if any, is of the name passed AND number
192 formats using it usually use the E or EE keyword (EC|EEC). */
193 bool doesSecondaryCalendarUseEC( std::u16string_view rName
) const;
195 /** Obtain digit grouping. The usually known grouping by thousands (#,###)
196 is actually only one of possible groupings. Another one, for example,
197 used in India is group by 3 and then by 2 indefinitely (#,##,###). The
198 integer sequence returned here specifies grouping from right to left
199 (!), with a 0 entry designating the end of rules and the previous value
200 to be repeated indefinitely. Hence the sequence {3,0} specifies the
201 usual grouping by thousands, whereas the sequence {3,2,0} specifies
202 Indian grouping. The sal_Int32* getConstArray() can be passed directly
203 to the ::rtl::math::doubleToString() methods as argument for the
204 pGroups parameter. */
205 const css::uno::Sequence
< sal_Int32
>& getDigitGrouping() const;
207 // Functionality of class International methods, LocaleItem
209 const OUString
& getDateSep() const
210 { return getOneLocaleItem( css::i18n::LocaleItem::DATE_SEPARATOR
); }
211 const OUString
& getNumThousandSep() const
212 { return getOneLocaleItem( css::i18n::LocaleItem::THOUSAND_SEPARATOR
); }
213 const OUString
& getNumDecimalSep() const
214 { return getOneLocaleItem( css::i18n::LocaleItem::DECIMAL_SEPARATOR
); }
215 const OUString
& getNumDecimalSepAlt() const
216 { return getOneLocaleItem( css::i18n::LocaleItem::DECIMAL_SEPARATOR_ALTERNATIVE
); }
217 const OUString
& getTimeSep() const
218 { return getOneLocaleItem( css::i18n::LocaleItem::TIME_SEPARATOR
); }
219 const OUString
& getTime100SecSep() const
220 { return getOneLocaleItem( css::i18n::LocaleItem::TIME_100SEC_SEPARATOR
); }
221 const OUString
& getListSep() const
222 { return getOneLocaleItem( css::i18n::LocaleItem::LIST_SEPARATOR
); }
223 const OUString
& getQuotationMarkStart() const
224 { return getOneLocaleItem( css::i18n::LocaleItem::SINGLE_QUOTATION_START
); }
225 const OUString
& getQuotationMarkEnd() const
226 { return getOneLocaleItem( css::i18n::LocaleItem::SINGLE_QUOTATION_END
); }
227 const OUString
& getDoubleQuotationMarkStart() const
228 { return getOneLocaleItem( css::i18n::LocaleItem::DOUBLE_QUOTATION_START
); }
229 const OUString
& getDoubleQuotationMarkEnd() const
230 { return getOneLocaleItem( css::i18n::LocaleItem::DOUBLE_QUOTATION_END
); }
231 MeasurementSystem
getMeasurementSystemEnum() const
232 { return mapMeasurementStringToEnum( getOneLocaleItem( css::i18n::LocaleItem::MEASUREMENT_SYSTEM
) ); }
233 const OUString
& getTimeAM() const
234 { return getOneLocaleItem( css::i18n::LocaleItem::TIME_AM
); }
235 const OUString
& getTimePM() const
236 { return getOneLocaleItem( css::i18n::LocaleItem::TIME_PM
); }
237 const OUString
& getLongDateDayOfWeekSep() const
238 { return getOneLocaleItem( css::i18n::LocaleItem::LONG_DATE_DAY_OF_WEEK_SEPARATOR
); }
239 const OUString
& getLongDateDaySep() const
240 { return getOneLocaleItem( css::i18n::LocaleItem::LONG_DATE_DAY_SEPARATOR
); }
241 const OUString
& getLongDateMonthSep() const
242 { return getOneLocaleItem( css::i18n::LocaleItem::LONG_DATE_MONTH_SEPARATOR
); }
243 const OUString
& getLongDateYearSep() const
244 { return getOneLocaleItem( css::i18n::LocaleItem::LONG_DATE_YEAR_SEPARATOR
); }
246 /** A wrapper around rtl::math::stringToDouble() using the locale dependent
247 decimal separator, group separator, and if needed decimal separator
250 The decimal separator is tried first, if the conversion does not match
251 the entire string then the decimal separator alternative is tried if it
252 occurs in the string and was the reason to stop.
254 Leading blanks are skipped, trailing blanks are not skipped. The number
255 is parsed up to the first non-floating point number character, same as
256 rtl::math::stringToDouble() does. The caller is responsible for proper
257 error checking and end comparison.
260 The string to parse as floating point number.
262 Whether group separator is used/accepted during parsing.
264 Pointer to receive the conversion status as in
265 rtl::math::stringToDouble().
267 Pointer to receive the parse end (exclusive) as in
268 rtl::math::stringToDouble().
269 @return The floating point number as parsed.
271 double stringToDouble( std::u16string_view aString
, bool bUseGroupSep
,
272 rtl_math_ConversionStatus
* pStatus
, sal_Int32
* pParseEnd
) const;
274 /** A wrapper around rtl_math_uStringToDouble() using the locale dependent
275 decimal separator, group separator, and if needed decimal separator
278 The decimal separator is tried first, if the conversion does not match
279 the entire string then the decimal separator alternative is tried if it
280 occurs in the string and was the reason to stop.
282 Leading blanks are skipped, trailing blanks are not skipped. The number
283 is parsed up to the first non-floating point number character, same as
284 rtl_math_uStringToDouble() does. The caller is responsible for proper
285 error checking and end comparison.
288 The string position to start parsing a floating point number.
290 The string position to stop parsing, exclusive.
292 Whether group separator is used/accepted during parsing.
294 Pointer to receive the conversion status as in
295 rtl_math_uStringToDouble().
297 Pointer to receive the parse end (exclusive) as in
298 rtl_math_uStringToDouble().
299 @return The floating point number as parsed.
301 double stringToDouble( const sal_Unicode
* pBegin
, const sal_Unicode
* pEnd
, bool bUseGroupSep
,
302 rtl_math_ConversionStatus
* pStatus
, const sal_Unicode
** ppParseEnd
) const;
305 const OUString
& getCurrSymbol() const;
306 const OUString
& getCurrBankSymbol() const;
307 sal_uInt16
getCurrPositiveFormat() const;
308 sal_uInt16
getCurrNegativeFormat() const;
309 sal_uInt16
getCurrDigits() const;
311 // simple date and time formatting
312 DateOrder
getDateOrder() const;
313 LongDateOrder
getLongDateOrder() const;
314 /// only numerical values of Gregorian calendar
315 OUString
getDate( const Date
& rDate
) const;
316 OUString
getTime( const tools::Time
& rTime
, bool bSec
= true,
317 bool b100Sec
= false ) const;
318 OUString
getDuration( const tools::Duration
& rDuration
,
319 bool bSec
= true, bool b100Sec
= false ) const;
321 /** Simple number formatting
323 value * 10**nDecimals
324 @param bTrailingZeros
325 </sal_True> := always display trailing zeros in
326 decimal places, even if integer value.
327 </sal_False> := trailing zeros are only displayed
328 if the value is not an integer value.
330 OUString
getNum( sal_Int64 nNumber
, sal_uInt16 nDecimals
,
331 bool bUseThousandSep
= true,
332 bool bTrailingZeros
= true ) const;
334 /// "Secure" currency formatted string.
335 OUString
getCurr( sal_Int64 nNumber
, sal_uInt16 nDecimals
,
336 std::u16string_view rCurrencySymbol
,
337 bool bUseThousandSep
= true ) const;
339 // dummy returns, to be implemented
340 sal_Unicode
getCurrZeroChar() const
342 static bool isNumLeadingZero()
344 /// standard decimal places
345 static sal_uInt16
getNumDigits()
347 static bool isNumTrailingZeros()
352 const OUString
& getTrueWord() const
353 { return getOneReservedWord( css::i18n::reservedWords::TRUE_WORD
); }
354 const OUString
& getFalseWord() const
355 { return getOneReservedWord( css::i18n::reservedWords::FALSE_WORD
); }
356 const OUString
& getAboveWord() const
357 { return getOneReservedWord( css::i18n::reservedWords::ABOVE_WORD
); }
358 const OUString
& getBelowWord() const
359 { return getOneReservedWord( css::i18n::reservedWords::BELOW_WORD
); }
360 /// return a quarter abbreviation string matching nQuarter (0..3) => "Q1" .. "Q2"
361 const OUString
& getQuarterAbbreviation( sal_Int16 nQuarter
) const
362 { return getOneReservedWord( css::i18n::reservedWords::QUARTER1_ABBREVIATION
+ nQuarter
); }
364 /** Return whether locale data checks are enabled.
365 Checks are enabled if the environment variable
366 OOO_ENABLE_LOCALE_DATA_CHECKS is set to 'Y' or 'Yes' (or any other
367 string starting with 'Y') or '1'.
368 Also used in conjunction with the number formatter. */
369 static bool areChecksEnabled()
371 if (nLocaleDataChecking
== 0)
372 evaluateLocaleDataChecking();
373 return nLocaleDataChecking
== 1;
376 /** Append locale info to string, used with locale data checking.
377 A string similar to "de_DE requested\n en_US loaded" is appended. */
378 OUString
appendLocaleInfo(std::u16string_view rDebugMsg
) const;
380 /** Output a message during locale data checking. The (UTF-8) string is
381 written to stderr and in a non-product build or if DBG_UTIL is enabled
382 also raised as an assertion message box. */
383 static void outputCheckMessage( std::u16string_view rMsg
);
384 static void outputCheckMessage( const char* pStr
);
386 LocaleDataWrapper(const LocaleDataWrapper
&) = delete;
387 LocaleDataWrapper
& operator=(const LocaleDataWrapper
&) = delete;
391 SAL_DLLPRIVATE
const css::lang::Locale
& getMyLocale() const;
393 static void evaluateLocaleDataChecking();
396 #endif // INCLUDED_UNOTOOLS_LOCALEDATAWRAPPER_HXX
398 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */