PixelLightAPI
.
|
00001 /*********************************************************\ 00002 * File: Localization.h * 00003 * 00004 * Copyright (C) 2002-2011 The PixelLight Team (http://www.pixellight.org/) 00005 * 00006 * This file is part of PixelLight. 00007 * 00008 * PixelLight is free software: you can redistribute it and/or modify 00009 * it under the terms of the GNU Lesser General Public License as published by 00010 * the Free Software Foundation, either version 3 of the License, or 00011 * (at your option) any later version. 00012 * 00013 * PixelLight is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public License 00019 * along with PixelLight. If not, see <http://www.gnu.org/licenses/>. 00020 \*********************************************************/ 00021 00022 00023 #ifndef __PLCORE_LOCALIZATION_H__ 00024 #define __PLCORE_LOCALIZATION_H__ 00025 #pragma once 00026 00027 00028 //[-------------------------------------------------------] 00029 //[ Includes ] 00030 //[-------------------------------------------------------] 00031 #include "PLCore/String/String.h" 00032 #include "PLCore/Core/Singleton.h" 00033 #include "PLCore/Container/Array.h" 00034 #include "PLCore/Container/HashMap.h" 00035 00036 00037 //[-------------------------------------------------------] 00038 //[ Macro definitions ] 00039 //[-------------------------------------------------------] 00040 #define PLT(Text) PLCore::Localization::GetInstance()->Get(Text) 00041 #define PL_TEXT(Text, Group) PLCore::Localization::GetInstance()->Get(Text, Group) 00042 00043 00044 //[-------------------------------------------------------] 00045 //[ Forward declarations ] 00046 //[-------------------------------------------------------] 00047 namespace PLCore { 00048 class Localization; 00049 class LocalizationGroup; 00050 } 00051 00052 00053 //[-------------------------------------------------------] 00054 //[ Namespace ] 00055 //[-------------------------------------------------------] 00056 namespace PLCore { 00057 00058 00059 //[-------------------------------------------------------] 00060 //[ Classes ] 00061 //[-------------------------------------------------------] 00062 /** 00063 * @brief 00064 * Localization manager 00065 * 00066 * @remarks 00067 * The localization manager is used to 'translate' a given text into the current active 00068 * language. This is done by using the given text to translate as 'key' within a certain 00069 * group and each project can have it's own localization group to avoid conflicts. If the given 00070 * text to translate was found within the given localization group, the translation result is returned, 00071 * else the given text is returned instead. As result, within your code you ALWAYS have to use a native 00072 * language (within PixelLight, this is english) which is used if a text is not translated. 00073 * The default localization group is called 'PixelLight' and should ONLY be used for engine texts! 00074 * 00075 * @verbatim 00076 * Usage example: 00077 * String sString = PLT("Translate this text"); 00078 * String sMyString = PL_TEXT("Translate this text", "MyProject"); 00079 * @endverbatim 00080 */ 00081 class Localization : public Singleton<Localization> { 00082 00083 00084 //[-------------------------------------------------------] 00085 //[ Friends ] 00086 //[-------------------------------------------------------] 00087 friend class Singleton<Localization>; 00088 00089 00090 //[-------------------------------------------------------] 00091 //[ Public static data ] 00092 //[-------------------------------------------------------] 00093 public: 00094 static PLCORE_API const String PixelLight; /**< "PixelLight" (default) */ 00095 00096 00097 //[-------------------------------------------------------] 00098 //[ Public static PLCore::Singleton functions ] 00099 //[-------------------------------------------------------] 00100 // This solution enhances the compatibility with legacy compilers like GCC 4.2.1 used on Mac OS X 10.6 00101 // -> The C++11 feature "extern template" (C++11, see e.g. http://www2.research.att.com/~bs/C++0xFAQ.html#extern-templates) can only be used on modern compilers like GCC 4.6 00102 // -> We can't break legacy compiler support, especially when only the singletons are responsible for the break 00103 // -> See PLCore::Singleton for more details about singletons 00104 public: 00105 static PLCORE_API Localization *GetInstance(); 00106 static PLCORE_API bool HasInstance(); 00107 00108 00109 //[-------------------------------------------------------] 00110 //[ Public functions ] 00111 //[-------------------------------------------------------] 00112 public: 00113 /** 00114 * @brief 00115 * Returns the current language 00116 * 00117 * @return 00118 * The current language 00119 */ 00120 inline String GetLanguage() const; 00121 00122 /** 00123 * @brief 00124 * Sets the current language 00125 * 00126 * @param[in] sLanguage 00127 * The new current language 00128 * 00129 * @return 00130 * 'true' if all went fine, else 'false' 00131 * 00132 * @note 00133 * - You can use the System::GetLocaleLanguage() function to get the current program locale language 00134 * - It's recommended to call this function ONLY on startup because it's possible that some 00135 * texts can't be updated on runtime if another language is set 00136 */ 00137 inline void SetLanguage(const String &sLanguage); 00138 00139 /** 00140 * @brief 00141 * Returns the translation of the given text 00142 * 00143 * @param[in] sText 00144 * Text to translate 00145 * @param[in] sGroup 00146 * Group the text to translate is in 00147 * 00148 * @return 00149 * The translation of the given text, if no translation is available for this text the 00150 * given text is returned instead by default 00151 */ 00152 PLCORE_API String Get(const String &sText, const String &sGroup = PixelLight) const; 00153 00154 /** 00155 * @brief 00156 * Returns the number of groups 00157 * 00158 * @return 00159 * The number of groups 00160 */ 00161 inline uint32 GetNumOfGroups() const; 00162 00163 /** 00164 * @brief 00165 * Returns a group by index 00166 * 00167 * @param[in] nIndex 00168 * Index of the group 00169 * 00170 * @return 00171 * The requested group, a null pointer on error 00172 */ 00173 inline LocalizationGroup *GetGroup(uint32 nIndex) const; 00174 00175 /** 00176 * @brief 00177 * Returns a group by name 00178 * 00179 * @param[in] sName 00180 * Name of the group 00181 * 00182 * @return 00183 * The requested group, a null pointer on error 00184 */ 00185 inline LocalizationGroup *GetGroup(const String &sName) const; 00186 00187 /** 00188 * @brief 00189 * Adds a new group 00190 * 00191 * @param[in] sName 00192 * Name of the new group 00193 * 00194 * @return 00195 * The new group, a null pointer on error (maybe there's already a group with the given name?) 00196 */ 00197 PLCORE_API LocalizationGroup *AddGroup(const String &sName); 00198 00199 /** 00200 * @brief 00201 * Removes a group by index 00202 * 00203 * @param[in] nIndex 00204 * Index of the group 00205 * 00206 * @return 00207 * 'true' if all went fine, else 'false' (maybe there's no group with the given name?) 00208 */ 00209 PLCORE_API bool RemoveGroup(uint32 nIndex); 00210 00211 /** 00212 * @brief 00213 * Removes a group by name 00214 * 00215 * @param[in] sName 00216 * Name of the group 00217 * 00218 * @return 00219 * 'true' if all went fine, else 'false' (maybe there's no group with the given name?) 00220 */ 00221 PLCORE_API bool RemoveGroup(const String &sName); 00222 00223 /** 00224 * @brief 00225 * Removes all groups 00226 */ 00227 PLCORE_API void RemoveAllGroups(); 00228 00229 00230 //[-------------------------------------------------------] 00231 //[ Private functions ] 00232 //[-------------------------------------------------------] 00233 private: 00234 /** 00235 * @brief 00236 * Constructor 00237 */ 00238 Localization(); 00239 00240 /** 00241 * @brief 00242 * Copy constructor 00243 * 00244 * @param[in] cSource 00245 * Source to copy from 00246 */ 00247 Localization(const Localization &cSource); 00248 00249 /** 00250 * @brief 00251 * Destructor 00252 */ 00253 virtual ~Localization(); 00254 00255 /** 00256 * @brief 00257 * Copy operator 00258 * 00259 * @param[in] cSource 00260 * Source to copy from 00261 * 00262 * @return 00263 * Reference to this instance 00264 */ 00265 Localization &operator =(const Localization &cSource); 00266 00267 00268 //[-------------------------------------------------------] 00269 //[ Private data ] 00270 //[-------------------------------------------------------] 00271 private: 00272 String m_sLanguage; /**< Current set language */ 00273 Array<LocalizationGroup*> m_lstGroups; /**< Localization groups list */ 00274 HashMap<String, LocalizationGroup*> m_mapGroups; /**< Localization groups map */ 00275 00276 00277 }; 00278 00279 00280 //[-------------------------------------------------------] 00281 //[ Namespace ] 00282 //[-------------------------------------------------------] 00283 } // PLCore 00284 00285 00286 //[-------------------------------------------------------] 00287 //[ Implementation ] 00288 //[-------------------------------------------------------] 00289 #include "PLCore/Tools/Localization.inl" 00290 00291 00292 #endif // __PLCORE_LOCALIZATION_H__
|