lang.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. //-----------------------------------------------------------------------------
  23. /// \file lang.h
  24. /// \brief Header for language support
  25. //-----------------------------------------------------------------------------
  26. #include "console/simBase.h"
  27. #include "core/util/tVector.h"
  28. //lang_ localization
  29. #include "core/fileObject.h"
  30. #include "core/util/str.h"
  31. #include "core/strings/unicode.h"
  32. #ifndef _LANG_H_
  33. #define _LANG_H_
  34. #define LANG_INVALID_ID 0xffffffff ///!< Invalid ID. Used for returning failure
  35. //-----------------------------------------------------------------------------
  36. /// \brief Class for working with language files
  37. //-----------------------------------------------------------------------------
  38. class LangFile
  39. {
  40. protected:
  41. Vector<UTF8 *> mStringTable;
  42. UTF8 * mLangName;
  43. UTF8 * mLangFile;
  44. void freeTable();
  45. public:
  46. LangFile(const UTF8 *langName = NULL);
  47. virtual ~LangFile();
  48. bool load(const UTF8 *filename);
  49. bool save(const UTF8 *filename);
  50. bool load(Stream *s);
  51. bool save(Stream *s);
  52. const UTF8 * getString(U32 id);
  53. U32 addString(const UTF8 *str);
  54. // [tom, 4/22/2005] setString() added to help the language compiler a bit
  55. void setString(U32 id, const UTF8 *str);
  56. void setLangName(const UTF8 *newName);
  57. const UTF8 *getLangName(void) { return mLangName; }
  58. const UTF8 *getLangFile(void) { return mLangFile; }
  59. void setLangFile(const UTF8 *langFile);
  60. bool activateLanguage(void);
  61. void deactivateLanguage(void);
  62. bool isLoaded(void) { return mStringTable.size() > 0; }
  63. S32 getNumStrings(void) { return mStringTable.size(); }
  64. };
  65. //-----------------------------------------------------------------------------
  66. /// \brief Language file table
  67. //-----------------------------------------------------------------------------
  68. class LangTable : public SimObject
  69. {
  70. typedef SimObject Parent;
  71. protected:
  72. Vector<LangFile *> mLangTable;
  73. S32 mDefaultLang;
  74. S32 mCurrentLang;
  75. public:
  76. DECLARE_CONOBJECT(LangTable);
  77. LangTable();
  78. virtual ~LangTable();
  79. S32 addLanguage(LangFile *lang, const UTF8 *name = NULL);
  80. S32 addLanguage(const UTF8 *filename, const UTF8 *name = NULL);
  81. void setDefaultLanguage(S32 langid);
  82. void setCurrentLanguage(S32 langid);
  83. S32 getCurrentLanguage(void) { return mCurrentLang; }
  84. const UTF8 * getLangName(const S32 langid) const
  85. {
  86. if(langid < 0 || langid >= mLangTable.size())
  87. return NULL;
  88. return mLangTable[langid]->getLangName();
  89. }
  90. const S32 getNumLang(void) const { return mLangTable.size(); }
  91. const UTF8 * getString(const U32 id) const;
  92. const U32 getStringLength(const U32 id) const;
  93. };
  94. extern UTF8 *sanitiseVarName(const UTF8 *varName, UTF8 *buffer, U32 bufsize);
  95. extern UTF8 *getCurrentModVarName(UTF8 *buffer, U32 bufsize);
  96. extern const LangTable *getCurrentModLangTable();
  97. extern const LangTable *getModLangTable(const UTF8 *mod);
  98. #endif // _LANG_H_