00001 /*---------------------------------------------------------------------\ 00002 | ____ _ __ __ ___ | 00003 | |__ / \ / / . \ . \ | 00004 | / / \ V /| _/ _/ | 00005 | / /__ | | | | | | | 00006 | /_____||_| |_| |_| | 00007 | | 00008 \---------------------------------------------------------------------*/ 00012 #include <iostream> 00013 #include <map> 00014 00015 #include "zypp/Locale.h" 00016 00017 using std::endl; 00018 00020 namespace zypp 00021 { 00022 00023 typedef std::map<std::string, std::string> OtherDefaultLanguage; 00024 static OtherDefaultLanguage otherDefaultLanguage; 00025 00027 // 00028 // CLASS NAME : Locale::Impl 00029 // 00031 struct Locale::Impl 00032 { 00033 Impl() 00034 {} 00035 00036 Impl( const std::string & code_r ) 00037 { 00038 std::string t; 00039 std::string::size_type sep = code_r.find_first_of( "@." ); 00040 if ( sep == std::string::npos ) { 00041 t = code_r; 00042 } else { 00043 t = code_r.substr( 0, sep ); 00044 } 00045 00046 sep = t.find( '_' ); 00047 if ( sep == std::string::npos ) { 00048 _language = LanguageCode( t ); 00049 } else { 00050 _language = LanguageCode( t.substr( 0, sep ) ); 00051 _country = CountryCode( t.substr( sep+1 ) ); 00052 } 00053 } 00054 00055 Impl( const LanguageCode & language_r, 00056 const CountryCode & country_r ) 00057 : _language( language_r ) 00058 , _country( country_r ) 00059 {} 00060 00061 const LanguageCode & language() const 00062 { return _language; } 00063 00064 const CountryCode & country() const 00065 { return _country; } 00066 00067 std::string code() const 00068 { 00069 std::string ret( _language.code() ); 00070 if ( _country.hasCode() ) 00071 ret += "_" + _country.code(); 00072 return ret; 00073 } 00074 00075 std::string name() const 00076 { 00077 std::string ret( _language.name() ); 00078 if ( _country.hasCode() ) 00079 ret += " (" + _country.name() + ")"; 00080 return ret; 00081 } 00082 00083 Locale fallback() const 00084 { 00085 if (otherDefaultLanguage.size() == 0) { 00086 // initial inserting map 00087 otherDefaultLanguage["pt_BR"] = "en"; 00088 } 00089 00090 if (otherDefaultLanguage.find(code()) != otherDefaultLanguage.end()) 00091 return LanguageCode(otherDefaultLanguage[code()]); 00092 00093 if ( _country.hasCode() ) 00094 return _language; 00095 00096 if ( _language.hasCode() && _language != LanguageCode("en") ) 00097 return LanguageCode("en"); 00098 00099 return Locale(); 00100 } 00101 00102 private: 00103 00104 LanguageCode _language; 00105 CountryCode _country; 00106 00107 public: 00109 static shared_ptr<Impl> nullimpl() 00110 { 00111 static shared_ptr<Impl> _nullimpl( new Impl ); 00112 return _nullimpl; 00113 } 00114 }; 00116 00118 inline std::ostream & operator<<( std::ostream & str, const Locale::Impl & obj ) 00119 { 00120 return str << "Locale::Impl"; 00121 } 00122 00124 // 00125 // CLASS NAME : Locale 00126 // 00128 00129 const Locale Locale::noCode; 00130 00132 // 00133 // METHOD NAME : Locale::Locale 00134 // METHOD TYPE : Ctor 00135 // 00136 Locale::Locale() 00137 : _pimpl( Impl::nullimpl() ) 00138 {} 00139 00141 // 00142 // METHOD NAME : Locale::Locale 00143 // METHOD TYPE : Ctor 00144 // 00145 Locale::Locale( const std::string & code_r ) 00146 : _pimpl( new Impl( code_r ) ) 00147 {} 00148 00150 // 00151 // METHOD NAME : Locale::Locale 00152 // METHOD TYPE : Ctor 00153 // 00154 Locale::Locale( const LanguageCode & language_r, 00155 const CountryCode & country_r ) 00156 : _pimpl( new Impl( language_r, country_r ) ) 00157 {} 00158 00160 // 00161 // METHOD NAME : Locale::~Locale 00162 // METHOD TYPE : Dtor 00163 // 00164 Locale::~Locale() 00165 {} 00166 00168 // 00169 // METHOD NAME : Locale:: 00170 // METHOD TYPE : 00171 // 00172 const LanguageCode & Locale::language() const 00173 { return _pimpl->language(); } 00174 00176 // 00177 // METHOD NAME : Locale:: 00178 // METHOD TYPE : 00179 // 00180 const CountryCode & Locale::country() const 00181 { return _pimpl->country(); } 00182 00184 // 00185 // METHOD NAME : Locale:: 00186 // METHOD TYPE : 00187 // 00188 std::string Locale::code() const 00189 { return _pimpl->code(); } 00190 00192 // 00193 // METHOD NAME : Locale:: 00194 // METHOD TYPE : 00195 // 00196 std::string Locale::name() const 00197 { return _pimpl->name(); } 00198 00200 // 00201 // METHOD NAME : Locale:: 00202 // METHOD TYPE : 00203 // 00204 Locale Locale::fallback() const 00205 { return _pimpl->fallback(); } 00207 } // namespace zypp