Imported Upstream version 16.3.2
[platform/upstream/libzypp.git] / zypp / Locale.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/Locale.h
10  *
11 */
12 #ifndef ZYPP_LOCALE_H
13 #define ZYPP_LOCALE_H
14
15 #include <iosfwd>
16 #include <string>
17
18 #include "zypp/base/Hash.h"
19
20 #include "zypp/IdStringType.h"
21 #include "zypp/LanguageCode.h"
22 #include "zypp/CountryCode.h"
23
24 ///////////////////////////////////////////////////////////////////
25 namespace zypp
26 {
27   class Locale;
28   typedef std::unordered_set<Locale> LocaleSet;
29
30   ///////////////////////////////////////////////////////////////////
31   /// \class Locale
32   /// \brief 'Language[_Country]' codes.
33   ///
34   /// In fact the class will not prevent to use a non iso code.
35   /// Just a warning will appear in the log. Construction from string
36   /// consider everything up to the first \c '.' or \c '@'.
37   /// \code
38   ///   Locale l( "de_DE.UTF-8" );
39   ///
40   ///   l.code()     == "de_DE";
41   ///   l.language() == "de";
42   ///   l.country()  == "DE";
43   ///
44   ///   l.fallback()                       == "de";
45   ///   l.fallback().fallback()            == Locale::enCode == "en";
46   ///   l.fallback().fallback().fallback() == Locale::noCode == "";
47   /// \endcode
48   ///////////////////////////////////////////////////////////////////
49   class Locale : public IdStringType<Locale>
50   {
51   public:
52     /** Default Ctor: \ref noCode */
53     Locale();
54
55     /** Ctor from string. */
56     explicit Locale( IdString str_r );
57
58     /** Ctor from string. */
59     explicit Locale( const std::string & str_r );
60
61     /** Ctor from string. */
62     explicit Locale( const char * str_r );
63
64     /** Ctor taking LanguageCode and optional CountryCode. */
65     Locale( LanguageCode language_r, CountryCode country_r = CountryCode() );
66
67     /** Dtor */
68     ~Locale();
69
70   public:
71     /** \name Locale constants. */
72     //@{
73     /** Empty code. */
74     static const Locale noCode;
75
76     /** Last resort "en". */
77     static const Locale enCode;
78     //@}
79
80   public:
81     /** The language part. */
82     LanguageCode language() const;
83
84     /** The county part.*/
85     CountryCode country() const;
86
87     /** Return the locale code asString. */
88     std::string code() const
89     { return std::string(_str); }
90
91     /** Return the translated locale name. */
92     std::string name() const;
93
94   public:
95     /** Return the fallback locale for this locale, if no fallback exists the empty Locale::noCode.
96      * The usual fallback sequence is "language_COUNTRY" -> "language" -> Locale::enCode ("en")
97      * ->Locale::noCode (""). Some exceptions like "pt_BR"->"en"->"" do exist.
98      */
99     Locale fallback() const;
100
101     /** Return the best match for \ref Locale \a requested_r within the available \a avLocales_r.
102      *
103      * If \a requested_r is not specified \ref ZConfig::textLocale is assumed.
104      *
105      * If neither \c requested_r nor any of it's \ref fallback locales are available
106      * in \a avLocales_r, \ref Locale::noCode is returned.
107      */
108     static Locale bestMatch( const LocaleSet & avLocales_r, Locale requested_r = Locale() );
109
110   private:
111     friend class IdStringType<Locale>;
112     IdString _str;
113   };
114 } // namespace zypp
115 ///////////////////////////////////////////////////////////////////
116
117 ZYPP_DEFINE_ID_HASHABLE( ::zypp::Locale );
118
119 #endif // ZYPP_LOCALE_H