LocalizationManager.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #ifndef __LOCALLIZATION_MANAGER_H__
  2. #define __LOCALLIZATION_MANAGER_H__
  3. #include <string>
  4. #include <unordered_map>
  5. #include "json/document-wrapper.h"
  6. #include "editor-support/cocostudio/CocosStudioExport.h"
  7. namespace cocostudio {
  8. /**
  9. *@brief Localization string manager interface template.
  10. */
  11. class CC_STUDIO_DLL ILocalizationManager
  12. {
  13. public:
  14. virtual ~ILocalizationManager() = default;
  15. virtual bool initLanguageData(std::string file) = 0;
  16. virtual std::string getLocalizationString(std::string key) = 0;
  17. };
  18. /**
  19. *@brief Localization string manager for output Json data file by cocostudio localization editor.
  20. * Note: If changed localization data file manually, please make sure the data file save as
  21. * text file format with encoding as 'UTF8 no BOM', otherwise the localization data may
  22. * not been parse successfully.
  23. */
  24. class CC_STUDIO_DLL JsonLocalizationManager : ILocalizationManager
  25. {
  26. public:
  27. static ILocalizationManager* getInstance();
  28. static void destroyInstance();
  29. public:
  30. /* Init manager with special localize json data file.
  31. * @param file Name of localize file.
  32. * @return If manager initialize success return true.
  33. */
  34. virtual bool initLanguageData(std::string file);
  35. /* Get localization string for special key.
  36. * @param key Special key to search in localization data.
  37. * @return If manager find the key in localization data, return value
  38. * set to key, otherwise return key itself.
  39. */
  40. virtual std::string getLocalizationString(std::string key);
  41. protected:
  42. JsonLocalizationManager();
  43. ~JsonLocalizationManager();
  44. protected:
  45. rapidjson::Document * languageData;
  46. };
  47. class CC_STUDIO_DLL BinLocalizationManager : ILocalizationManager
  48. {
  49. public:
  50. static ILocalizationManager* getInstance();
  51. static void destroyInstance();
  52. /* Init manager with special localize binary data file.
  53. * @param file Name of localize file.
  54. * @return If manager initialize success return true.
  55. */
  56. virtual bool initLanguageData(std::string file);
  57. /* Get localization string for special key.
  58. * @param key Special key to search in localization data.
  59. * @return If manager find the key in localization data, return value
  60. * set to key, otherwise return key itself.
  61. */
  62. virtual std::string getLocalizationString(std::string key);
  63. protected:
  64. BinLocalizationManager();
  65. ~BinLocalizationManager();
  66. protected:
  67. std::unordered_map<std::string, std::string> languageData;
  68. };
  69. class CC_STUDIO_DLL LocalizationHelper
  70. {
  71. public:
  72. /* Get current localization manager.
  73. * @return The instance of current localization manager.
  74. * If the manager hasn't been set, it will return the singleton instance of BinLocalizationManager.
  75. */
  76. static ILocalizationManager* getCurrentManager();
  77. /* Set current localization manager.
  78. * @param manager The instance of current manager.
  79. * @param isBinary Wether the manager is binary localization manager.
  80. * If the param is false, current manager will be set to JsonLocalizationManager.
  81. */
  82. static void setCurrentManager(ILocalizationManager* manager, bool isBinary);
  83. /* Get the type of current localization manager.
  84. * @return If current manager is BinLocalizationManager, return true.
  85. * Otherwise return false, that means current manager is JsonLocalizationManager.
  86. */
  87. static bool isBinManager();
  88. };
  89. }
  90. #endif //__LOCALLIZATION_MANAGER_H__