BsStringTable.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. /** @addtogroup Localization-Internal
  202. * @{
  203. */
  204. /**
  205. * Internal data used for representing a localized string instance. for example a specific instance of a localized
  206. * string using specific parameters.
  207. */
  208. struct LocalizedStringData
  209. {
  210. struct ParamOffset
  211. {
  212. ParamOffset()
  213. :paramIdx(0), location(0)
  214. { }
  215. ParamOffset(UINT32 _paramIdx, UINT32 _location)
  216. :paramIdx(_paramIdx), location(_location)
  217. { }
  218. UINT32 paramIdx;
  219. UINT32 location;
  220. };
  221. LocalizedStringData();
  222. ~LocalizedStringData();
  223. WString string;
  224. UINT32 numParameters;
  225. ParamOffset* parameterOffsets;
  226. void concatenateString(WString& outputString, WString* parameters, UINT32 numParameterValues) const;
  227. void updateString(const WString& string);
  228. };
  229. /** Data for a single language in the string table. */
  230. struct LanguageData
  231. {
  232. UnorderedMap<WString, SPtr<LocalizedStringData>> strings;
  233. };
  234. /** @} */
  235. /** @addtogroup Localization
  236. * @{
  237. */
  238. /** Used for string localization. Stores strings and their translations in various languages. */
  239. class BS_CORE_EXPORT StringTable : public Resource
  240. {
  241. // TODO - When editing string table I will need to ensure that all languages of the same string have the same number of parameters
  242. public:
  243. StringTable();
  244. ~StringTable();
  245. /** Returns all identifiers in the table. */
  246. const UnorderedSet<WString>& getIdentifiers() const { return mIdentifiers; }
  247. /** Adds or modifies string translation for the specified language. */
  248. void setString(const WString& identifier, Language language, const WString& string);
  249. /** Returns a string translation for the specified language. Returns the identifier itself if one doesn't exist. */
  250. WString getString(const WString& identifier, Language language);
  251. /** Removes the string described by identifier, from all languages. */
  252. void removeString(const WString& identifier);
  253. /**
  254. * Gets a string data for the specified string identifier and currently active language.
  255. *
  256. * @param[in] identifier Unique string identifier.
  257. * @param[in] insertIfNonExisting If true, a new string data for the specified identifier and language will be
  258. * added to the table if data doesn't already exist. The data will use the
  259. * identifier as the translation string.
  260. * @return The string data. Don't store reference to this data as it may get deleted.
  261. */
  262. SPtr<LocalizedStringData> getStringData(const WString& identifier, bool insertIfNonExisting = true);
  263. /**
  264. * Gets a string data for the specified string identifier and language.
  265. *
  266. * @param[in] identifier Unique string identifier.
  267. * @param[in] language Language.
  268. * @param[in] insertIfNonExisting If true, a new string data for the specified identifier and language will be
  269. * added to the table if data doesn't already exist. The data will use the
  270. * identifier as the translation string.
  271. * @return The string data. Don't store reference to this data as it may get deleted.
  272. */
  273. SPtr<LocalizedStringData> getStringData(const WString& identifier, Language language, bool insertIfNonExisting = true);
  274. /** Creates a new empty string table resource. */
  275. static HStringTable create();
  276. static const Language DEFAULT_LANGUAGE;
  277. public: // ***** INTERNAL ******
  278. /** @name Internal
  279. * @{
  280. */
  281. /**
  282. * Creates a new empty string table resource.
  283. *
  284. * @note Internal method. Use create() for normal use.
  285. */
  286. static SPtr<StringTable> _createPtr();
  287. /** @} */
  288. private:
  289. friend class HString;
  290. friend class StringTableManager;
  291. /** Gets the currently active language. */
  292. Language getActiveLanguage() const { return mActiveLanguage; }
  293. /** Changes the currently active language. Any newly created strings will use this value. */
  294. void setActiveLanguage(Language language);
  295. Language mActiveLanguage;
  296. LanguageData* mActiveLanguageData;
  297. LanguageData* mDefaultLanguageData;
  298. LanguageData* mAllLanguages;
  299. UnorderedSet<WString> mIdentifiers;
  300. /************************************************************************/
  301. /* SERIALIZATION */
  302. /************************************************************************/
  303. public:
  304. friend class StringTableRTTI;
  305. static RTTITypeBase* getRTTIStatic();
  306. virtual RTTITypeBase* getRTTI() const override;
  307. };
  308. /** @} */
  309. }