BsStringTable.h 6.9 KB

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