aboutsummaryrefslogtreecommitdiff
path: root/src/corelib/i18n/mlocale_p.h
blob: 45ba2a09e68433685d9ad5214d6b3890c4c2326e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (directui@nokia.com)
**
** This file is part of libmeegotouch.
**
** If you have questions regarding the use of this file, please contact
** Nokia at directui@nokia.com.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/

#ifndef MLOCALE_P_H
#define MLOCALE_P_H

#include <QSet>
#include <QList>
#include <QString>
#include <QStringList>
#include <QExplicitlySharedDataPointer>
#include <QLocale>
#include <QCache>

#ifdef HAVE_ICU
#include <unicode/datefmt.h>
#include <unicode/numfmt.h>
#include <unicode/decimfmt.h>
#include <unicode/unistr.h>
#include <unicode/dtfmtsym.h>
#endif

#ifdef HAVE_GCONF
#include "mgconfitem.h"
#endif

#include "mlocale.h"

class QString;
class MTranslationCatalog;

class MLocalePrivate
{
    Q_DECLARE_PUBLIC(MLocale)

public:
    MLocalePrivate();
    MLocalePrivate(const MLocalePrivate &other);

    virtual ~MLocalePrivate();


    MLocalePrivate &operator=(const MLocalePrivate &other);

    typedef QList<QExplicitlySharedDataPointer<MTranslationCatalog> > CatalogList;

    // return string for a category, default if category is not set
    QString categoryName(MLocale::Category category) const;

    /*!
     * \brief loads the QTranslators for all current catalogs
     */
    void loadTrCatalogs();

    /*!
     * \brief Installs the translations into QCoreApplication
     */
    void insertTrToQCoreApp();

    /*!
     * \brief Removes the translations from QCoreApplication
     */
    void removeTrFromQCoreApp();

    /*!
     * \brief inserts a text direction translator into QCoreApplication
     *
     * This is a special translator used to make QCoreApplication
     * detect the correct layout direction used for the language
     * of the current locale. For example, in case of Arabic or
     * Hebrew right-to-left layout is used.
     */
    void insertDirectionTrToQCoreApp();

    QLocale createQLocale(MLocale::Category catetory) const;

    void setCategoryLocale(MLocale *mlocale, MLocale::Category category,
                           const QString &localeName);

#ifdef HAVE_ICU
    /*!
     * \brief tries to truncate a locale name to search for fallbacks
     * \param a pointer to a QString containing an ICU locale name
     *
     * When searching for ICU resource bundles or when trying to
     * get ICU resource strings by keys it is sometimes necessary
     * to use fallbacks, i.e. when nothing is found for the
     * locale name “en_US”, try “en” as well. This function
     * tries to truncate the locale name given as the parameter to
     * the next fallback to try. If truncating was possible and
     * the function returns true. If no further truncating is
     * possible it returns false and the locale name given
     * as the parameter is left unchanged. I.e. if false is returned
     * there are no fallbacks left to try.
     */
    bool truncateLocaleName(QString *localeName) const;

    // creates an icu::Locale for specific category
    icu::Locale getCategoryLocale(MLocale::Category category) const;

    static icu::DateFormatSymbols *createDateFormatSymbols(const icu::Locale &locale);

    // checks if an ICU format string is a twelve hour format string or not
    bool isTwelveHours(const QString &icuFormatQString) const;
    // converts an ICU date format to 24 hour clock
    void dateFormatTo24h(icu::DateFormat *df) const;
    // converts an ICU date format to 12 hour clock
    void dateFormatTo12h(icu::DateFormat *df) const;

    /*!
     * \brief returns ICU date and time format string of the current locale
     * \param dateType style of date formatting
     * \param timeType style of time formatting
     * \param calendarType calendar to use for formatting
     * \param timeFormat24h enum to choose 12 hour or 24 hour or default time format
     */
    QString icuFormatString(MLocale::DateType dateType,
                            MLocale::TimeType timeType,
                            MLocale::CalendarType calendarType,
                            MLocale::TimeFormat24h timeFormat24h) const;

    // creates a dateformat object for datetime formatting/parsing
    // the caller is responsible for deleting the dateformat object
    icu::DateFormat *createDateFormat(MLocale::DateType dateType,
                                      MLocale::TimeType timeType,
                                      MLocale::CalendarType calendarType,
                                      MLocale::TimeFormat24h timeFormat24h) const;
#endif

    // these return the requested part of a locale string,
    // e.g. parseLanguage("fi_FI") -> "fi"
    static QString parseLanguage(const QString &localeString);
    static QString parseCountry(const QString &localeString);
    static QString parseScript(const QString &localeString);
    static QString parseVariant(const QString &localeString);

    QString formatPhoneNumber( const QString& phoneNumber,
                               MLocale::PhoneNumberGrouping grouping ) const;

    bool isValidCountryCode( const QString& code ) const;

    bool _valid;

    // the default locale is used for messages and other categories if not
    // overridden
    QString _defaultLocale;
    QString _messageLocale;
    QString _numericLocale;
    QString _collationLocale;
    QString _calendarLocale;
    QString _monetaryLocale;
    QString _nameLocale;
    QString _telephoneLocale;

    // the list of valid country codes for the formatPhoneNumber function
    QSet<QString> _validCountryCodes;

    // the used calendar and collation may be overridden
    MLocale::CalendarType _calendarType;
    MLocale::Collation _collation;
    MLocale::TimeFormat24h _timeFormat24h;
    // add currency?

    MLocale::PhoneNumberGrouping _phoneNumberGrouping;

#ifdef HAVE_ICU
    // number format caching for better performance.
    icu::NumberFormat *_numberFormat;
    icu::NumberFormat *_numberFormatLcTime;
#endif

    // translations for two supported translation categories
    CatalogList _messageTranslations;
    CatalogList _timeTranslations;

    // tr translations are kept separate because they have to be loaded when inserted
    // into QCoreApplication
    CatalogList _trTranslations;

    static QStringList dataPaths;
    static QStringList translationPaths;

#ifdef HAVE_GCONF
    MGConfItem currentLanguageItem;
    MGConfItem currentLcTimeItem;
    MGConfItem currentLcTimeFormat24hItem;
    MGConfItem currentLcCollateItem;
    MGConfItem currentLcNumericItem;
    MGConfItem currentLcMonetaryItem;
    MGConfItem currentLcTelephoneItem;
#endif

    MLocale *q_ptr;
};

#endif