BsStringTable.h 6.6 KB

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