2
0

BsStringTable.h 7.0 KB

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