BsStringTable.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #pragma once
  2. #include "BsPrerequisitesUtil.h"
  3. #include "BsModule.h"
  4. #include "BsEvent.h"
  5. namespace BansheeEngine
  6. {
  7. /**
  8. * @brief Loosely based on ISO 639-1 two letter language codes
  9. */
  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. /**
  200. * @brief Internal data used for representing a localized string instance.
  201. * e.g. a specific instance of a localized string using specific parameters.
  202. */
  203. struct LocalizedStringData
  204. {
  205. struct Common
  206. {
  207. WString identifier;
  208. Event<void()> onStringDataModified;
  209. };
  210. struct ParamOffset
  211. {
  212. ParamOffset()
  213. :paramIdx(0), location(0)
  214. { }
  215. ParamOffset(UINT32 _paramIdx, UINT32 _location)
  216. :paramIdx(_paramIdx), location(_location)
  217. { }
  218. UINT32 paramIdx;
  219. UINT32 location;
  220. };
  221. LocalizedStringData();
  222. ~LocalizedStringData();
  223. WString string;
  224. UINT32 numParameters;
  225. ParamOffset* parameterOffsets;
  226. Common* commonData;
  227. void concatenateString(WString& outputString, WString* parameters, UINT32 numParameterValues) const;
  228. void updateString(const WString& string);
  229. };
  230. /**
  231. * @brief Class that handles string localization. Stores strings and their translations
  232. * in various languages, along with the ability to switch currently active language.
  233. */
  234. class BS_UTILITY_EXPORT StringTable : public Module<StringTable>
  235. {
  236. // TODO - When editing string table I will need to ensure that all languages of the same string have the same number of parameters
  237. struct LanguageData
  238. {
  239. UnorderedMap<WString, LocalizedStringData*> strings;
  240. };
  241. public:
  242. StringTable();
  243. ~StringTable();
  244. /**
  245. * @brief Gets the currently active language.
  246. */
  247. Language getActiveLanguage() const { return mActiveLanguage; }
  248. /**
  249. * @brief Changes the currently active language.
  250. * This will update any localized string instances.
  251. */
  252. void setActiveLanguage(Language language);
  253. /**
  254. * @brief Adds or modifies string translation for the specified language.
  255. */
  256. void setString(const WString& identifier, Language language, const WString& string);
  257. /**
  258. * @brief Removes the string described by identifier, from all languages.
  259. */
  260. void removeString(const WString& identifier);
  261. /**
  262. * @brief Gets a string data for the specified string identifier and currently active language.
  263. *
  264. * @param identifier Unique string identifier.
  265. * @param insertIfNonExisting If true, a new string data for the specified identifier and language will be
  266. * added to the table if data doesn't already exist. The data will use the identifier as
  267. * the translation string.
  268. *
  269. * @return The string data. Don't store reference to this data as it may get deleted.
  270. */
  271. LocalizedStringData& getStringData(const WString& identifier, bool insertIfNonExisting = true);
  272. /**
  273. * @brief Gets a string data for the specified string identifier and language.
  274. *
  275. * @param identifier Unique string identifier.
  276. * @param language Language.
  277. * @param insertIfNonExisting If true, a new string data for the specified identifier and language will be
  278. * added to the table if data doesn't already exist. The data will use the identifier as
  279. * the translation string.
  280. *
  281. * @return The string data. Don't store reference to this data as it may get deleted.
  282. */
  283. LocalizedStringData& getStringData(const WString& identifier, Language language, bool insertIfNonExisting = true);
  284. private:
  285. friend class HString;
  286. static const Language DEFAULT_LANGUAGE;
  287. Language mActiveLanguage;
  288. LanguageData* mActiveLanguageData;
  289. LanguageData* mDefaultLanguageData;
  290. LanguageData* mAllLanguages;
  291. UnorderedMap<WString, LocalizedStringData::Common*> mCommonData;
  292. void notifyAllStringsChanged();
  293. };
  294. }