TextLocalizationContainer.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. bool overriden = false;
  28. template <typename Handler>
  29. void serialize(Handler & h)
  30. {
  31. h & translatedText;
  32. h & identifierModContext;
  33. h & baseStringModContext;
  34. }
  35. };
  36. /// map identifier -> localization
  37. std::unordered_map<std::string, StringState> stringsLocalizations;
  38. std::vector<const TextLocalizationContainer *> subContainers;
  39. /// add selected string to internal storage as high-priority strings
  40. void registerStringOverride(const std::string & modContext, const TextIdentifier & UID, const std::string & localized, const std::string & language);
  41. std::string getModLanguage(const std::string & modContext);
  42. // returns true if identifier with such name was registered, even if not translated to current language
  43. bool identifierExists(const TextIdentifier & UID) const;
  44. public:
  45. /// Loads translation from provided json
  46. /// Any entries loaded by this will have priority over texts registered normally
  47. void loadTranslationOverrides(const std::string & modContext, const std::string & language, JsonNode const & file);
  48. /// add selected string to internal storage
  49. void registerString(const std::string & modContext, const TextIdentifier & UID, const JsonNode & localized);
  50. void registerString(const std::string & modContext, const TextIdentifier & UID, const std::string & localized);
  51. void registerString(const std::string & identifierModContext, const std::string & localizedStringModContext, const TextIdentifier & UID, const std::string & localized);
  52. /// returns translated version of a string that can be displayed to user
  53. template<typename ... Args>
  54. std::string translate(std::string arg1, Args ... args) const
  55. {
  56. TextIdentifier id(arg1, args ...);
  57. return translateString(id);
  58. }
  59. /// converts identifier into user-readable string
  60. const std::string & translateString(const TextIdentifier & identifier) const;
  61. /// Debug method, returns all currently stored texts
  62. /// Format: [mod ID][string ID] -> human-readable text
  63. void exportAllTexts(std::map<std::string, std::map<std::string, std::string>> & storage, bool onlyMissing) const;
  64. /// Add or override subcontainer which can store identifiers
  65. void addSubContainer(const TextLocalizationContainer & container);
  66. /// Remove subcontainer with give name
  67. void removeSubContainer(const TextLocalizationContainer & container);
  68. void jsonSerialize(JsonNode & dest) const;
  69. template <typename Handler>
  70. void serialize(Handler & h)
  71. {
  72. std::lock_guard globalLock(globalTextMutex);
  73. h & stringsLocalizations;
  74. }
  75. };
  76. class DLL_LINKAGE TextContainerRegistrable : public TextLocalizationContainer
  77. {
  78. public:
  79. TextContainerRegistrable();
  80. ~TextContainerRegistrable();
  81. TextContainerRegistrable(const TextContainerRegistrable & other);
  82. TextContainerRegistrable(TextContainerRegistrable && other) noexcept;
  83. TextContainerRegistrable& operator=(const TextContainerRegistrable & b) = default;
  84. };
  85. VCMI_LIB_NAMESPACE_END