1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/Locale.h
18 #include "zypp/base/Hash.h"
20 #include "zypp/IdStringType.h"
21 #include "zypp/LanguageCode.h"
22 #include "zypp/CountryCode.h"
24 ///////////////////////////////////////////////////////////////////
28 typedef std::unordered_set<Locale> LocaleSet;
30 ///////////////////////////////////////////////////////////////////
32 /// \brief 'Language[_Country]' codes.
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 '@'.
38 /// Locale l( "de_DE.UTF-8" );
40 /// l.code() == "de_DE";
41 /// l.language() == "de";
42 /// l.country() == "DE";
44 /// l.fallback() == "de";
45 /// l.fallback().fallback() == Locale::enCode == "en";
46 /// l.fallback().fallback().fallback() == Locale::noCode == "";
48 ///////////////////////////////////////////////////////////////////
49 class Locale : public IdStringType<Locale>
52 /** Default Ctor: \ref noCode */
55 /** Ctor from string. */
56 explicit Locale( IdString str_r );
58 /** Ctor from string. */
59 explicit Locale( const std::string & str_r );
61 /** Ctor from string. */
62 explicit Locale( const char * str_r );
64 /** Ctor taking LanguageCode and optional CountryCode. */
65 Locale( LanguageCode language_r, CountryCode country_r = CountryCode() );
71 /** \name Locale constants. */
74 static const Locale noCode;
76 /** Last resort "en". */
77 static const Locale enCode;
81 /** The language part. */
82 LanguageCode language() const;
84 /** The county part.*/
85 CountryCode country() const;
87 /** Return the locale code asString. */
88 std::string code() const
89 { return std::string(_str); }
91 /** Return the translated locale name. */
92 std::string name() const;
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.
99 Locale fallback() const;
101 /** Return the best match for \ref Locale \a requested_r within the available \a avLocales_r.
103 * If \a requested_r is not specified \ref ZConfig::textLocale is assumed.
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.
108 static Locale bestMatch( const LocaleSet & avLocales_r, Locale requested_r = Locale() );
111 friend class IdStringType<Locale>;
115 ///////////////////////////////////////////////////////////////////
117 ZYPP_DEFINE_ID_HASHABLE( ::zypp::Locale );
119 #endif // ZYPP_LOCALE_H