~thopiekar/zypper/libzypp-manual-import

« back to all changes in this revision

Viewing changes to zypp/Locale.h

  • Committer: Thomas-Karl Pietrowski
  • Date: 2015-08-15 15:59:50 UTC
  • Revision ID: thopiekar@googlemail.com-20150815155950-j66qn38efmvn289t
syncing with "changes 15.13.0 (11)"  #9a0aca7e3a21d768491b141a8ae86ef0c3fbc227
* https://github.com/openSUSE/libzypp/commit/9a0aca7e3a21d768491b141a8ae86ef0c3fbc227

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
#define ZYPP_LOCALE_H
14
14
 
15
15
#include <iosfwd>
16
 
 
17
 
#include "zypp/base/PtrTypes.h"
18
 
#include "zypp/base/Tr1hash.h"
19
 
 
20
 
#include "zypp/IdString.h"
 
16
#include <string>
 
17
 
 
18
#include "zypp/base/Hash.h"
 
19
 
 
20
#include "zypp/IdStringType.h"
21
21
#include "zypp/LanguageCode.h"
22
22
#include "zypp/CountryCode.h"
23
23
 
24
24
///////////////////////////////////////////////////////////////////
25
25
namespace zypp
26
 
{ /////////////////////////////////////////////////////////////////
27
 
 
 
26
{
28
27
  class Locale;
29
 
  typedef std::tr1::unordered_set<Locale> LocaleSet;
 
28
  typedef std::unordered_set<Locale> LocaleSet;
30
29
 
31
30
  ///////////////////////////////////////////////////////////////////
32
 
  //
33
 
  //    CLASS NAME : Locale
34
 
  //
35
 
  /**
36
 
   * \todo migrate to IdString
37
 
  */
38
 
  class Locale
 
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>
39
50
  {
40
 
    friend std::ostream & operator<<( std::ostream & str, const Locale & obj );
41
 
 
42
 
  public:
43
 
    /** Implementation  */
44
 
    class Impl;
45
 
 
46
 
  public:
47
 
    /** Default ctor */
 
51
  public:
 
52
    /** Default Ctor: \ref noCode */
48
53
    Locale();
49
54
 
50
 
    /** Ctor taking a string. */
51
 
    explicit
52
 
    Locale( IdString code_r );
53
 
 
54
 
    explicit
55
 
    Locale( const std::string & code_r );
56
 
 
57
 
    explicit
58
 
    Locale( const char * code_r );
 
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 );
59
63
 
60
64
    /** Ctor taking LanguageCode and optional CountryCode. */
61
 
    Locale( const LanguageCode & language_r,
62
 
            const CountryCode & country_r = CountryCode() );
 
65
    Locale( LanguageCode language_r, CountryCode country_r = CountryCode() );
63
66
 
64
67
    /** Dtor */
65
68
    ~Locale();
67
70
  public:
68
71
    /** \name Locale constants. */
69
72
    //@{
70
 
    /** No or empty code. */
 
73
    /** Empty code. */
71
74
    static const Locale noCode;
 
75
 
 
76
    /** Last resort "en". */
 
77
    static const Locale enCode;
72
78
    //@}
73
79
 
74
80
  public:
75
 
    /** */
76
 
    const LanguageCode & language() const;
77
 
    /** */
78
 
    const CountryCode & country() const;
79
 
 
80
 
    /** Return the locale code. */
81
 
    std::string code() const;
82
 
 
83
 
    /** Return the name made of language and country name. */
 
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. */
84
92
    std::string name() const;
85
93
 
86
 
    /** Return a fallback locale for this locale, when giving up, returns empty Locale() */
 
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
     */
87
99
    Locale fallback() const;
88
100
 
89
 
  public:
90
 
 
91
 
    /** Return the best match for \ref Locale \c requested_r within the available \c avLocales_r.
92
 
     *
93
 
     * If \c requested_r is nor specified or equals \ref Locale::noCode,
94
 
     * \ref ZConfig::textLocale is assumed.
95
 
     *
96
 
     * If neither \c requested_r nor any of it's \ref fallback locales
97
 
     * are available, \ref Locale::noCode is returned.
98
 
    */
99
 
    static Locale bestMatch( const LocaleSet & avLocales_r, const Locale & requested_r = Locale() );
 
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() );
100
109
 
101
110
  private:
102
 
    /** Pointer to implementation */
103
 
    RW_pointer<Impl> _pimpl;
 
111
    friend class IdStringType<Locale>;
 
112
    IdString _str;
104
113
  };
105
 
  ///////////////////////////////////////////////////////////////////
106
 
 
107
 
  /** \relates Locale Stream output */
108
 
  inline std::ostream & operator<<( std::ostream & str, const Locale & obj )
109
 
  { return str << obj.code(); }
110
 
 
111
 
  /** Comparison based on string value. */
112
 
  //@{
113
 
  /** \relates Locale */
114
 
  inline bool operator==( const Locale & lhs, const Locale & rhs ) {
115
 
    return( lhs.code() == rhs.code() );
116
 
  }
117
 
  /** \relates Locale */
118
 
  inline bool operator==( const std::string & lhs, const Locale & rhs ) {
119
 
    return( lhs == rhs.code() );
120
 
  }
121
 
  /** \relates Locale */
122
 
  inline bool operator==( const Locale & lhs, const std::string & rhs ) {
123
 
    return( lhs.code() == rhs );
124
 
  }
125
 
 
126
 
  /** \relates Locale */
127
 
  inline bool operator!=( const Locale & lhs, const Locale & rhs ) {
128
 
    return( ! operator==( lhs, rhs ) );
129
 
  }
130
 
  /** \relates Locale */
131
 
  inline bool operator!=( const std::string & lhs, const Locale & rhs ) {
132
 
    return( ! operator==( lhs, rhs ) );
133
 
  }
134
 
  /** \relates Locale */
135
 
  inline bool operator!=( const Locale & lhs, const std::string & rhs ) {
136
 
    return( ! operator==( lhs, rhs ) );
137
 
  }
138
 
  //@}
139
 
 
140
 
  /////////////////////////////////////////////////////////////////
141
114
} // namespace zypp
142
115
///////////////////////////////////////////////////////////////////
143
116
 
144
 
namespace std { namespace tr1 {
145
 
  /** \relates ::zypp::Locale hash function */
146
 
  template<> struct hash< ::zypp::Locale>
147
 
  {
148
 
    size_t operator()( const ::zypp::Locale & __s ) const
149
 
    { return hash<std::string>()(__s.code()); }
150
 
  };
151
 
}}
 
117
ZYPP_DEFINE_ID_HASHABLE( ::zypp::Locale );
152
118
 
153
 
///////////////////////////////////////////////////////////////////
154
 
namespace std
155
 
{ /////////////////////////////////////////////////////////////////
156
 
  /** \relates zypp::Locale Default order for std::container based on code string value.*/
157
 
  template<>
158
 
    inline bool less<zypp::Locale>::operator()( const zypp::Locale & lhs, const zypp::Locale & rhs ) const
159
 
    { return lhs.code() < rhs.code(); }
160
 
  /////////////////////////////////////////////////////////////////
161
 
} // namespace std
162
 
///////////////////////////////////////////////////////////////////
163
119
#endif // ZYPP_LOCALE_H