1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/Locale.cc
15 #include "zypp/Locale.h"
16 #include "zypp/ZConfig.h"
20 ///////////////////////////////////////////////////////////////////
22 { /////////////////////////////////////////////////////////////////
24 typedef std::map<std::string, std::string> OtherDefaultLanguage;
25 static OtherDefaultLanguage otherDefaultLanguage;
27 ///////////////////////////////////////////////////////////////////
29 // CLASS NAME : Locale::Impl
31 /** Locale implementation. */
37 Impl( const std::string & code_r )
40 std::string::size_type sep = code_r.find_first_of( "@." );
41 if ( sep == std::string::npos ) {
44 t = code_r.substr( 0, sep );
48 if ( sep == std::string::npos ) {
49 _language = LanguageCode( t );
51 _language = LanguageCode( t.substr( 0, sep ) );
52 _country = CountryCode( t.substr( sep+1 ) );
56 Impl( const LanguageCode & language_r,
57 const CountryCode & country_r )
58 : _language( language_r )
59 , _country( country_r )
62 const LanguageCode & language() const
65 const CountryCode & country() const
68 std::string code() const
70 std::string ret( _language.code() );
71 if ( _country.hasCode() )
72 ret += "_" + _country.code();
76 std::string name() const
78 std::string ret( _language.name() );
79 if ( _country.hasCode() )
80 ret += " (" + _country.name() + ")";
84 Locale fallback() const
86 if (otherDefaultLanguage.size() == 0) {
87 // initial inserting map
88 otherDefaultLanguage["pt_BR"] = "en";
91 if (otherDefaultLanguage.find(code()) != otherDefaultLanguage.end())
92 return LanguageCode(otherDefaultLanguage[code()]);
94 if ( _country.hasCode() )
97 if ( _language.hasCode() && _language != LanguageCode("en") )
98 return LanguageCode("en");
105 LanguageCode _language;
106 CountryCode _country;
109 /** Offer default Impl. */
110 static shared_ptr<Impl> nullimpl()
112 static shared_ptr<Impl> _nullimpl( new Impl );
116 ///////////////////////////////////////////////////////////////////
118 /** \relates Locale::Impl Stream output */
119 inline std::ostream & operator<<( std::ostream & str, const Locale::Impl & obj )
121 return str << "Locale::Impl";
124 ///////////////////////////////////////////////////////////////////
126 // CLASS NAME : Locale
128 ///////////////////////////////////////////////////////////////////
130 const Locale Locale::noCode;
132 ///////////////////////////////////////////////////////////////////
134 // METHOD NAME : Locale::Locale
135 // METHOD TYPE : Ctor
138 : _pimpl( Impl::nullimpl() )
141 ///////////////////////////////////////////////////////////////////
143 // METHOD NAME : Locale::Locale
144 // METHOD TYPE : Ctor
146 Locale::Locale( IdString code_r )
147 : _pimpl( new Impl( code_r.asString() ) )
150 ///////////////////////////////////////////////////////////////////
152 // METHOD NAME : Locale::Locale
153 // METHOD TYPE : Ctor
155 Locale::Locale( const std::string & code_r )
156 : _pimpl( new Impl( code_r ) )
159 ///////////////////////////////////////////////////////////////////
161 // METHOD NAME : Locale::Locale
162 // METHOD TYPE : Ctor
164 Locale::Locale( const char * code_r )
165 : _pimpl( new Impl( C_Str(code_r).c_str() ) )
168 ///////////////////////////////////////////////////////////////////
170 // METHOD NAME : Locale::Locale
171 // METHOD TYPE : Ctor
173 Locale::Locale( const LanguageCode & language_r,
174 const CountryCode & country_r )
175 : _pimpl( new Impl( language_r, country_r ) )
178 ///////////////////////////////////////////////////////////////////
180 // METHOD NAME : Locale::~Locale
181 // METHOD TYPE : Dtor
186 ///////////////////////////////////////////////////////////////////
188 // METHOD NAME : Locale::
191 const LanguageCode & Locale::language() const
192 { return _pimpl->language(); }
194 ///////////////////////////////////////////////////////////////////
196 // METHOD NAME : Locale::
199 const CountryCode & Locale::country() const
200 { return _pimpl->country(); }
202 ///////////////////////////////////////////////////////////////////
204 // METHOD NAME : Locale::
207 std::string Locale::code() const
208 { return _pimpl->code(); }
210 ///////////////////////////////////////////////////////////////////
212 // METHOD NAME : Locale::
215 std::string Locale::name() const
216 { return _pimpl->name(); }
218 ///////////////////////////////////////////////////////////////////
220 // METHOD NAME : Locale::
223 Locale Locale::fallback() const
224 { return _pimpl->fallback(); }
227 ///////////////////////////////////////////////////////////////////
229 Locale Locale::bestMatch( const LocaleSet & avLocales_r, const Locale & requested_r )
231 if ( ! avLocales_r.empty() )
233 for ( Locale check( requested_r == noCode ? ZConfig::instance().textLocale() : requested_r );
234 check != noCode; check = check.fallback() )
236 if ( avLocales_r.find( check ) != avLocales_r.end() )
244 /////////////////////////////////////////////////////////////////
246 ///////////////////////////////////////////////////////////////////