1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/Locale.h
17 #include "zypp/base/PtrTypes.h"
18 #include "zypp/base/Tr1hash.h"
20 #include "zypp/IdString.h"
21 #include "zypp/LanguageCode.h"
22 #include "zypp/CountryCode.h"
24 ///////////////////////////////////////////////////////////////////
26 { /////////////////////////////////////////////////////////////////
29 typedef std::tr1::unordered_set<Locale> LocaleSet;
31 ///////////////////////////////////////////////////////////////////
33 // CLASS NAME : Locale
36 * \todo migrate to IdString
40 friend std::ostream & operator<<( std::ostream & str, const Locale & obj );
50 /** Ctor taking a string. */
52 Locale( IdString code_r );
55 Locale( const std::string & code_r );
58 Locale( const char * code_r );
60 /** Ctor taking LanguageCode and optional CountryCode. */
61 Locale( const LanguageCode & language_r,
62 const CountryCode & country_r = CountryCode() );
68 /** \name Locale constants. */
70 /** No or empty code. */
71 static const Locale noCode;
76 const LanguageCode & language() const;
78 const CountryCode & country() const;
80 /** Return the locale code. */
81 std::string code() const;
83 /** Return the name made of language and country name. */
84 std::string name() const;
86 /** Return a fallback locale for this locale, when giving up, returns empty Locale() */
87 Locale fallback() const;
91 /** Return the best match for \ref Locale \c requested_r within the available \c avLocales_r.
93 * If \c requested_r is nor specified or equals \ref Locale::noCode,
94 * \ref ZConfig::textLocale is assumed.
96 * If neither \c requested_r nor any of it's \ref fallback locales
97 * are available, \ref Locale::noCode is returned.
99 static Locale bestMatch( const LocaleSet & avLocales_r, const Locale & requested_r = Locale() );
102 /** Pointer to implementation */
103 RW_pointer<Impl> _pimpl;
105 ///////////////////////////////////////////////////////////////////
107 /** \relates Locale Stream output */
108 inline std::ostream & operator<<( std::ostream & str, const Locale & obj )
109 { return str << obj.code(); }
111 /** Comparison based on string value. */
113 /** \relates Locale */
114 inline bool operator==( const Locale & lhs, const Locale & rhs ) {
115 return( lhs.code() == rhs.code() );
117 /** \relates Locale */
118 inline bool operator==( const std::string & lhs, const Locale & rhs ) {
119 return( lhs == rhs.code() );
121 /** \relates Locale */
122 inline bool operator==( const Locale & lhs, const std::string & rhs ) {
123 return( lhs.code() == rhs );
126 /** \relates Locale */
127 inline bool operator!=( const Locale & lhs, const Locale & rhs ) {
128 return( ! operator==( lhs, rhs ) );
130 /** \relates Locale */
131 inline bool operator!=( const std::string & lhs, const Locale & rhs ) {
132 return( ! operator==( lhs, rhs ) );
134 /** \relates Locale */
135 inline bool operator!=( const Locale & lhs, const std::string & rhs ) {
136 return( ! operator==( lhs, rhs ) );
140 /////////////////////////////////////////////////////////////////
142 ///////////////////////////////////////////////////////////////////
144 namespace std { namespace tr1 {
145 /** \relates ::zypp::Locale hash function */
146 template<> struct hash< ::zypp::Locale>
148 size_t operator()( const ::zypp::Locale & __s ) const
149 { return hash<std::string>()(__s.code()); }
153 ///////////////////////////////////////////////////////////////////
155 { /////////////////////////////////////////////////////////////////
156 /** \relates zypp::Locale Default order for std::container based on code string value.*/
158 inline bool less<zypp::Locale>::operator()( const zypp::Locale & lhs, const zypp::Locale & rhs ) const
159 { return lhs.code() < rhs.code(); }
160 /////////////////////////////////////////////////////////////////
162 ///////////////////////////////////////////////////////////////////
163 #endif // ZYPP_LOCALE_H