TextLocalizationContainer.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 translatedText;
  22. /// ID of mod that created this string
  23. std::string identifierModContext;
  24. /// ID of mod that provides original, untranslated version of this string
  25. /// Different from identifierModContext if mod has modified object from another mod (e.g. rebalance mods)
  26. std::string baseStringModContext;
  27. template <typename Handler>
  28. void serialize(Handler & h)
  29. {
  30. h & translatedText;
  31. h & identifierModContext;
  32. h & baseStringModContext;
  33. }
  34. };
  35. /// map identifier -> localization
  36. std::unordered_map<std::string, StringState> stringsLocalizations;
  37. std::vector<const TextLocalizationContainer *> subContainers;
  38. /// add selected string to internal storage as high-priority strings
  39. void registerStringOverride(const std::string & modContext, const TextIdentifier & UID, const std::string & localized);
  40. std::string getModLanguage(const std::string & modContext);
  41. // returns true if identifier with such name was registered, even if not translated to current language
  42. bool identifierExists(const TextIdentifier & UID) const;
  43. public:
  44. /// Loads translation from provided json
  45. /// Any entries loaded by this will have priority over texts registered normally
  46. void loadTranslationOverrides(const std::string & modContext, JsonNode const & file);
  47. /// add selected string to internal storage
  48. void registerString(const std::string & modContext, const TextIdentifier & UID, const JsonNode & localized);
  49. void registerString(const std::string & modContext, const TextIdentifier & UID, const std::string & localized);
  50. void registerString(const std::string & identifierModContext, const std::string & localizedStringModContext, const TextIdentifier & UID, const std::string & localized);
  51. /// returns translated version of a string that can be displayed to user
  52. template<typename ... Args>
  53. std::string translate(std::string arg1, Args ... args) const
  54. {
  55. TextIdentifier id(arg1, args ...);
  56. return translateString(id);
  57. }
  58. /// converts identifier into user-readable string
  59. const std::string & translateString(const TextIdentifier & identifier) const;
  60. /// Debug method, returns all currently stored texts
  61. /// Format: [mod ID][string ID] -> human-readable text
  62. void exportAllTexts(std::map<std::string, std::map<std::string, std::string>> & storage) const;
  63. /// Add or override subcontainer which can store identifiers
  64. void addSubContainer(const TextLocalizationContainer & container);
  65. /// Remove subcontainer with give name
  66. void removeSubContainer(const TextLocalizationContainer & container);
  67. void jsonSerialize(JsonNode & dest) const;
  68. template <typename Handler>
  69. void serialize(Handler & h)
  70. {
  71. std::lock_guard globalLock(globalTextMutex);
  72. if (h.version >= Handler::Version::SIMPLE_TEXT_CONTAINER_SERIALIZATION)
  73. {
  74. h & stringsLocalizations;
  75. }
  76. else
  77. {
  78. std::string key;
  79. int64_t sz = stringsLocalizations.size();
  80. if (h.version >= Handler::Version::REMOVE_TEXT_CONTAINER_SIZE_T)
  81. {
  82. int64_t size = sz;
  83. h & size;
  84. sz = size;
  85. }
  86. else
  87. {
  88. h & sz;
  89. }
  90. if(h.saving)
  91. {
  92. for(auto & s : stringsLocalizations)
  93. {
  94. key = s.first;
  95. h & key;
  96. h & s.second;
  97. }
  98. }
  99. else
  100. {
  101. for(size_t i = 0; i < sz; ++i)
  102. {
  103. h & key;
  104. h & stringsLocalizations[key];
  105. }
  106. }
  107. }
  108. }
  109. };
  110. class DLL_LINKAGE TextContainerRegistrable : public TextLocalizationContainer
  111. {
  112. public:
  113. TextContainerRegistrable();
  114. ~TextContainerRegistrable();
  115. TextContainerRegistrable(const TextContainerRegistrable & other);
  116. TextContainerRegistrable(TextContainerRegistrable && other) noexcept;
  117. TextContainerRegistrable& operator=(const TextContainerRegistrable & b) = default;
  118. };
  119. VCMI_LIB_NAMESPACE_END