BsStringTable.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "BsResource.h"
  6. namespace BansheeEngine
  7. {
  8. /** @addtogroup Localization
  9. * @{
  10. */
  11. /** Loosely based on ISO 639-1 two letter language codes */
  12. enum class Language
  13. {
  14. Afar,
  15. Abkhazian,
  16. Avestan,
  17. Afrikaans,
  18. Akan,
  19. Amharic,
  20. Aragonese,
  21. Arabic,
  22. Assamese,
  23. Avaric,
  24. Aymara,
  25. Azerbaijani,
  26. Bashkir,
  27. Belarusian,
  28. Bulgarian,
  29. Bihari,
  30. Bislama,
  31. Bambara,
  32. Bengali,
  33. Tibetan,
  34. Breton,
  35. Bosnian,
  36. Catalan,
  37. Chechen,
  38. Chamorro,
  39. Corsican,
  40. Cree,
  41. Czech,
  42. ChurchSlavic,
  43. Chuvash,
  44. Welsh,
  45. Danish,
  46. German,
  47. Maldivian,
  48. Bhutani,
  49. Ewe,
  50. Greek,
  51. EnglishUK,
  52. EnglishUS,
  53. Esperanto,
  54. Spanish,
  55. Estonian,
  56. Basque,
  57. Persian,
  58. Fulah,
  59. Finnish,
  60. Fijian,
  61. Faroese,
  62. French,
  63. WesternFrisian,
  64. Irish,
  65. ScottishGaelic,
  66. Galician,
  67. Guarani,
  68. Gujarati,
  69. Manx,
  70. Hausa,
  71. Hebrew,
  72. Hindi,
  73. HiriMotu,
  74. Croatian,
  75. Haitian,
  76. Hungarian,
  77. Armenian,
  78. Herero,
  79. Interlingua,
  80. Indonesian,
  81. Interlingue,
  82. Igbo,
  83. SichuanYi,
  84. Inupiak,
  85. Ido,
  86. Icelandic,
  87. Italian,
  88. Inuktitut,
  89. Japanese,
  90. Javanese,
  91. Georgian,
  92. Kongo,
  93. Kikuyu,
  94. Kuanyama,
  95. Kazakh,
  96. Kalaallisut,
  97. Cambodian,
  98. Kannada,
  99. Korean,
  100. Kanuri,
  101. Kashmiri,
  102. Kurdish,
  103. Komi,
  104. Cornish,
  105. Kirghiz,
  106. Latin,
  107. Luxembourgish,
  108. Ganda,
  109. Limburgish,
  110. Lingala,
  111. Laotian,
  112. Lithuanian,
  113. LubaKatanga,
  114. Latvian,
  115. Malagasy,
  116. Marshallese,
  117. Maori,
  118. Macedonian,
  119. Malayalam,
  120. Mongolian,
  121. Moldavian,
  122. Marathi,
  123. Malay,
  124. Maltese,
  125. Burmese,
  126. Nauru,
  127. NorwegianBokmal,
  128. Ndebele,
  129. Nepali,
  130. Ndonga,
  131. Dutch,
  132. NorwegianNynorsk,
  133. Norwegian,
  134. Navaho,
  135. Nyanja,
  136. Provençal,
  137. Ojibwa,
  138. Oromo,
  139. Oriya,
  140. Ossetic,
  141. Punjabi,
  142. Pali,
  143. Polish,
  144. Pushto,
  145. Portuguese,
  146. Quechua,
  147. Romansh,
  148. Kirundi,
  149. Romanian,
  150. Russian,
  151. Kinyarwanda,
  152. Sanskrit,
  153. Sardinian,
  154. Sindhi,
  155. NorthernSami,
  156. Sangro,
  157. Sinhalese,
  158. Slovak,
  159. Slovenian,
  160. Samoan,
  161. Shona,
  162. Somali,
  163. Albanian,
  164. Serbian,
  165. Swati,
  166. Sesotho,
  167. Sundanese,
  168. Swedish,
  169. Swahili,
  170. Tamil,
  171. Telugu,
  172. Tajik,
  173. Thai,
  174. Tigrinya,
  175. Turkmen,
  176. Tagalog,
  177. Setswana,
  178. Tonga,
  179. Turkish,
  180. Tsonga,
  181. Tatar,
  182. Twi,
  183. Tahitian,
  184. Uighur,
  185. Ukrainian,
  186. Urdu,
  187. Uzbek,
  188. Venda,
  189. Vietnamese,
  190. Volapuk,
  191. Walloon,
  192. Wolof,
  193. Xhosa,
  194. Yiddish,
  195. Yoruba,
  196. Zhuang,
  197. Chinese,
  198. Zulu,
  199. Count // Number of entries
  200. };
  201. /** @cond INTERNAL */
  202. /**
  203. * Internal data used for representing a localized string instance. for example a specific instance of a localized
  204. * string using specific parameters.
  205. */
  206. struct LocalizedStringData
  207. {
  208. struct ParamOffset
  209. {
  210. ParamOffset()
  211. :paramIdx(0), location(0)
  212. { }
  213. ParamOffset(UINT32 _paramIdx, UINT32 _location)
  214. :paramIdx(_paramIdx), location(_location)
  215. { }
  216. UINT32 paramIdx;
  217. UINT32 location;
  218. };
  219. LocalizedStringData();
  220. ~LocalizedStringData();
  221. WString string;
  222. UINT32 numParameters;
  223. ParamOffset* parameterOffsets;
  224. void concatenateString(WString& outputString, WString* parameters, UINT32 numParameterValues) const;
  225. void updateString(const WString& string);
  226. };
  227. /** Data for a single language in the string table. */
  228. struct LanguageData
  229. {
  230. UnorderedMap<WString, SPtr<LocalizedStringData>> strings;
  231. };
  232. /** @endcond */
  233. /** Used for string localization. Stores strings and their translations in various languages. */
  234. class BS_CORE_EXPORT StringTable : public Resource
  235. {
  236. // TODO - When editing string table I will need to ensure that all languages of the same string have the same number of parameters
  237. public:
  238. StringTable();
  239. ~StringTable();
  240. /** Returns all identifiers in the table. */
  241. const UnorderedSet<WString>& getIdentifiers() const { return mIdentifiers; }
  242. /** Adds or modifies string translation for the specified language. */
  243. void setString(const WString& identifier, Language language, const WString& string);
  244. /** Returns a string translation for the specified language. Returns the identifier itself if one doesn't exist. */
  245. WString getString(const WString& identifier, Language language);
  246. /** Removes the string described by identifier, from all languages. */
  247. void removeString(const WString& identifier);
  248. /**
  249. * Gets a string data for the specified string identifier and currently active language.
  250. *
  251. * @param[in] identifier Unique string identifier.
  252. * @param[in] insertIfNonExisting If true, a new string data for the specified identifier and language will be
  253. * added to the table if data doesn't already exist. The data will use the
  254. * identifier as the translation string.
  255. * @return The string data. Don't store reference to this data as it may get deleted.
  256. */
  257. SPtr<LocalizedStringData> getStringData(const WString& identifier, bool insertIfNonExisting = true);
  258. /**
  259. * Gets a string data for the specified string identifier and language.
  260. *
  261. * @param[in] identifier Unique string identifier.
  262. * @param[in] language Language.
  263. * @param[in] insertIfNonExisting If true, a new string data for the specified identifier and language will be
  264. * added to the table if data doesn't already exist. The data will use the
  265. * identifier as the translation string.
  266. * @return The string data. Don't store reference to this data as it may get deleted.
  267. */
  268. SPtr<LocalizedStringData> getStringData(const WString& identifier, Language language, bool insertIfNonExisting = true);
  269. /** Creates a new empty string table resource. */
  270. static HStringTable create();
  271. static const Language DEFAULT_LANGUAGE;
  272. public: // ***** INTERNAL ******
  273. /** @name Internal
  274. * @{
  275. */
  276. /**
  277. * Creates a new empty string table resource.
  278. *
  279. * @note Internal method. Use create() for normal use.
  280. */
  281. static SPtr<StringTable> _createPtr();
  282. /** @} */
  283. private:
  284. friend class HString;
  285. friend class StringTableManager;
  286. /** Gets the currently active language. */
  287. Language getActiveLanguage() const { return mActiveLanguage; }
  288. /** Changes the currently active language. Any newly created strings will use this value. */
  289. void setActiveLanguage(Language language);
  290. Language mActiveLanguage;
  291. LanguageData* mActiveLanguageData;
  292. LanguageData* mDefaultLanguageData;
  293. LanguageData* mAllLanguages;
  294. UnorderedSet<WString> mIdentifiers;
  295. /************************************************************************/
  296. /* SERIALIZATION */
  297. /************************************************************************/
  298. public:
  299. friend class StringTableRTTI;
  300. static RTTITypeBase* getRTTIStatic();
  301. virtual RTTITypeBase* getRTTI() const override;
  302. };
  303. /** @} */
  304. }