00001 /* 00002 * Copyright (C) 2003-2009 Olivier Boudeville 00003 * 00004 * This file is part of the Ceylan library. 00005 * 00006 * The Ceylan library is free software: you can redistribute it and/or modify 00007 * it under the terms of either the GNU Lesser General Public License or 00008 * the GNU General Public License, as they are published by the Free Software 00009 * Foundation, either version 3 of these Licenses, or (at your option) 00010 * any later version. 00011 * 00012 * The Ceylan library is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU Lesser General Public License and the GNU General Public License 00016 * for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License and the GNU General Public License along with the Ceylan library. 00020 * If not, see <http://www.gnu.org/licenses/>. 00021 * 00022 * Author: Olivier Boudeville (olivier.boudeville@esperide.com) 00023 * 00024 */ 00025 00026 00027 #include "CeylanLocale.h" 00028 00029 #include "CeylanStringUtils.h" // for formatStringList 00030 #include "CeylanOperators.h" // for toString 00031 00032 00033 using std::string ; 00034 using std::list ; 00035 00036 #include <algorithm> 00037 00038 using namespace Ceylan ; 00039 00040 00041 00042 LocalizationException::LocalizationException( const string & reason ) : 00043 Ceylan::Exception( reason ) 00044 { 00045 00046 } 00047 00048 00049 LocalizationException::~LocalizationException() throw() 00050 { 00051 00052 } 00053 00054 00055 00056 /* 00057 * Known locales listed here: 00058 * (IsAKnownLocale, GetLocaleCodeFromName and GetLocaleNameFromCode must be 00059 * updated if that list is updated) 00060 * 00061 */ 00062 00063 const Ceylan::LocaleCode LocalizationSettings::EnglishLocale = 1 ; 00064 const Ceylan::LocaleCode LocalizationSettings::FrenchLocale = 2 ; 00065 const Ceylan::LocaleCode LocalizationSettings::GermanLocale = 3 ; 00066 00067 const std::string EnglishLocaleName = "english" ; 00068 const std::string FrenchLocaleName = "french" ; 00069 const std::string GermanLocaleName = "german" ; 00070 00071 00072 00073 LocalizationSettings::LocalizationSettings() : 00074 _currentLocale( 0 ) 00075 { 00076 00077 } 00078 00079 00080 00081 LocalizationSettings::~LocalizationSettings() throw() 00082 { 00083 00084 } 00085 00086 00087 00088 void LocalizationSettings::addSupportedLocale( LocaleCode code ) 00089 { 00090 00091 // Adds only if not already there: 00092 if ( ! isSupported( code ) ) 00093 _supportedLocales.push_back( code ) ; 00094 00095 } 00096 00097 00098 00099 void LocalizationSettings::addSupportedLocale( const string & localeName ) 00100 { 00101 00102 if ( ! IsAKnownLocale( localeName ) ) 00103 throw LocalizationException( 00104 "LocalizationSettings::addSupportedLocale failed: locale '" 00105 + localeName + "' not known." ) ; 00106 00107 addSupportedLocale( GetLocaleCodeFromName( localeName ) ) ; 00108 00109 } 00110 00111 00112 00113 bool LocalizationSettings::isSupported( LocaleCode code ) 00114 { 00115 00116 return ( std::find( _supportedLocales.begin(), _supportedLocales.end(), 00117 code ) != _supportedLocales.end() ) ; 00118 00119 } 00120 00121 00122 00123 bool LocalizationSettings::isSupported( const std::string & localeName ) 00124 { 00125 00126 if ( ! IsAKnownLocale( localeName ) ) 00127 return false ; 00128 00129 // No exception should be raised: 00130 return isSupported( GetLocaleCodeFromName(localeName) ) ; 00131 00132 } 00133 00134 00135 00136 const std::list<LocaleCode> & LocalizationSettings::getSupportedLocales() const 00137 { 00138 00139 return _supportedLocales ; 00140 00141 } 00142 00143 00144 00145 bool LocalizationSettings::hasCurrentLocale() const 00146 { 00147 00148 return ( _currentLocale != 0 ) ; 00149 00150 } 00151 00152 00153 00154 LocaleCode LocalizationSettings::getCurrentLocaleCode() const 00155 { 00156 00157 if ( _currentLocale == 0 ) 00158 throw LocalizationException( 00159 "LocalizationSettings::getCurrentLocaleCode failed: " 00160 "no current locale set." ) ; 00161 00162 return _currentLocale ; 00163 } 00164 00165 00166 00167 const std::string & LocalizationSettings::getCurrentLocaleName() const 00168 { 00169 00170 return GetLocaleNameFromCode( getCurrentLocaleCode() ) ; 00171 00172 } 00173 00174 00175 00176 void LocalizationSettings::setCurrentLocale( LocaleCode code ) 00177 { 00178 00179 if ( ! isSupported( code ) ) 00180 throw LocalizationException( 00181 "LocalizationSettings::setCurrentLocale failed for locale code " 00182 + Ceylan::toString( code ) + ": locale not supported." ) ; 00183 00184 _currentLocale = code ; 00185 00186 } 00187 00188 00189 00190 void LocalizationSettings::setCurrentLocale( const std::string & localeName ) 00191 { 00192 00193 setCurrentLocale( GetLocaleCodeFromName( localeName ) ) ; 00194 00195 } 00196 00197 00198 00199 const std::string LocalizationSettings::toString( VerbosityLevels level ) const 00200 { 00201 00202 if ( _supportedLocales.empty() ) 00203 return "No locale supported" ; 00204 00205 string currentMessage ; 00206 00207 if ( hasCurrentLocale() ) 00208 currentMessage = "Current locale is " + getCurrentLocaleName() ; 00209 else 00210 currentMessage = "No current locale set" ; 00211 00212 list<string> res ; 00213 00214 for ( list<LocaleCode>::const_iterator it = _supportedLocales.begin(); 00215 it != _supportedLocales.end(); it++ ) 00216 res.push_back( GetLocaleNameFromCode( *it ) ) ; 00217 00218 return currentMessage + ". Supported locales are: " 00219 + Ceylan::formatStringList( res ) ; 00220 00221 } 00222 00223 00224 00225 bool LocalizationSettings::IsAKnownLocale( const string & localeName ) 00226 { 00227 00228 // Clumsy and stupid, as C++ is: (could also use GetLocaleCodeFromName) 00229 00230 if ( localeName == "english" || localeName == "french" 00231 || localeName == "german" ) 00232 return true ; 00233 00234 return false ; 00235 00236 } 00237 00238 00239 00240 LocaleCode LocalizationSettings::GetLocaleCodeFromName( 00241 const string & localeName ) 00242 { 00243 00244 if ( localeName == EnglishLocaleName ) 00245 return EnglishLocale ; 00246 00247 if ( localeName == FrenchLocaleName ) 00248 return FrenchLocale ; 00249 00250 if ( localeName == GermanLocaleName ) 00251 return GermanLocale ; 00252 00253 throw LocalizationException( 00254 "LocalizationSettings::GetLocaleCodeFromName failed: unknown locale: " 00255 + localeName ) ; 00256 00257 } 00258 00259 00260 00261 const std::string & LocalizationSettings::GetLocaleNameFromCode( 00262 LocaleCode code ) 00263 { 00264 00265 switch( code ) 00266 { 00267 00268 case EnglishLocale: 00269 return EnglishLocaleName ; 00270 break ; 00271 00272 case FrenchLocale: 00273 return FrenchLocaleName ; 00274 break ; 00275 00276 case GermanLocale: 00277 return GermanLocaleName ; 00278 break ; 00279 00280 default: 00281 throw LocalizationException( 00282 "LocalizationSettings::GetLocaleNameFromCode failed: " 00283 "unknown locale code " + Ceylan::toString( code ) ) ; 00284 break ; 00285 00286 } 00287 00288 } 00289