BsStringTable.h 7.7 KB

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