TextLocalizationContainer.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * TextLocalizationContainer.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "TextIdentifier.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. class JsonNode;
  14. class DLL_LINKAGE TextLocalizationContainer
  15. {
  16. protected:
  17. static std::recursive_mutex globalTextMutex;
  18. struct StringState
  19. {
  20. /// Human-readable string that was added on registration
  21. std::string baseValue;
  22. /// Translated human-readable string
  23. std::string overrideValue;
  24. /// ID of mod that created this string
  25. std::string modContext;
  26. template <typename Handler>
  27. void serialize(Handler & h)
  28. {
  29. h & baseValue;
  30. //h & baseLanguage;
  31. h & modContext;
  32. }
  33. };
  34. /// map identifier -> localization
  35. std::unordered_map<std::string, StringState> stringsLocalizations;
  36. std::vector<const TextLocalizationContainer *> subContainers;
  37. /// add selected string to internal storage as high-priority strings
  38. void registerStringOverride(const std::string & modContext, const TextIdentifier & UID, const std::string & localized);
  39. std::string getModLanguage(const std::string & modContext);
  40. // returns true if identifier with such name was registered, even if not translated to current language
  41. bool identifierExists(const TextIdentifier & UID) const;
  42. public:
  43. /// Loads translation from provided json
  44. /// Any entries loaded by this will have priority over texts registered normally
  45. void loadTranslationOverrides(const std::string & modContext, JsonNode const & file);
  46. /// add selected string to internal storage
  47. void registerString(const std::string & modContext, const TextIdentifier & UID, const JsonNode & localized);
  48. void registerString(const std::string & modContext, const TextIdentifier & UID, const std::string & localized);
  49. void registerString(const std::string & modContext, const TextIdentifier & UID, const std::string & localized, const std::string & language);
  50. /// returns translated version of a string that can be displayed to user
  51. template<typename ... Args>
  52. std::string translate(std::string arg1, Args ... args) const
  53. {
  54. TextIdentifier id(arg1, args ...);
  55. return deserialize(id);
  56. }
  57. /// converts identifier into user-readable string
  58. const std::string & deserialize(const TextIdentifier & identifier) const;
  59. /// Debug method, returns all currently stored texts
  60. /// Format: [mod ID][string ID] -> human-readable text
  61. void exportAllTexts(std::map<std::string, std::map<std::string, std::string>> & storage) const;
  62. /// Add or override subcontainer which can store identifiers
  63. void addSubContainer(const TextLocalizationContainer & container);
  64. /// Remove subcontainer with give name
  65. void removeSubContainer(const TextLocalizationContainer & container);
  66. void jsonSerialize(JsonNode & dest) const;
  67. template <typename Handler>
  68. void serialize(Handler & h)
  69. {
  70. std::lock_guard globalLock(globalTextMutex);
  71. if (h.version >= Handler::Version::SIMPLE_TEXT_CONTAINER_SERIALIZATION)
  72. {
  73. h & stringsLocalizations;
  74. }
  75. else
  76. {
  77. std::string key;
  78. int64_t sz = stringsLocalizations.size();
  79. if (h.version >= Handler::Version::REMOVE_TEXT_CONTAINER_SIZE_T)
  80. {
  81. int64_t size = sz;
  82. h & size;
  83. sz = size;
  84. }
  85. else
  86. {
  87. h & sz;
  88. }
  89. if(h.saving)
  90. {
  91. for(auto & s : stringsLocalizations)
  92. {
  93. key = s.first;
  94. h & key;
  95. h & s.second;
  96. }
  97. }
  98. else
  99. {
  100. for(size_t i = 0; i < sz; ++i)
  101. {
  102. h & key;
  103. h & stringsLocalizations[key];
  104. }
  105. }
  106. }
  107. }
  108. };
  109. class DLL_LINKAGE TextContainerRegistrable : public TextLocalizationContainer
  110. {
  111. public:
  112. TextContainerRegistrable();
  113. ~TextContainerRegistrable();
  114. TextContainerRegistrable(const TextContainerRegistrable & other);
  115. TextContainerRegistrable(TextContainerRegistrable && other) noexcept;
  116. TextContainerRegistrable& operator=(const TextContainerRegistrable & b) = default;
  117. };
  118. VCMI_LIB_NAMESPACE_END