BsStringTable.h 6.2 KB

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