1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
/*
* TextLocalizationContainer.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "TextLocalizationContainer.h"
#include "texts/CGeneralTextHandler.h"
#include "Languages.h"
#include "TextOperations.h"
#include "../VCMI_Lib.h"
#include "../json/JsonNode.h"
#include "../modding/CModHandler.h"
VCMI_LIB_NAMESPACE_BEGIN
std::recursive_mutex TextLocalizationContainer::globalTextMutex;
void TextLocalizationContainer::registerStringOverride(const std::string & modContext, const TextIdentifier & UID, const std::string & localized, const std::string & language)
{
std::lock_guard globalLock(globalTextMutex);
assert(!modContext.empty());
// NOTE: implicitly creates entry, intended - strings added by maps, campaigns, vcmi and potentially - UI mods are not registered anywhere at the moment
auto & entry = stringsLocalizations[UID.get()];
// load string override only in following cases:
// a) string was not modified in another mod (e.g. rebalance mod gave skill new description)
// b) this string override is defined in the same mod as one that provided modified version of this string
if (entry.identifierModContext == entry.baseStringModContext || modContext == entry.baseStringModContext)
{
entry.translatedText = localized;
if (entry.identifierModContext.empty())
{
entry.identifierModContext = modContext;
entry.baseStringModContext = modContext;
}
else
{
if (language == VLC->generaltexth->getPreferredLanguage())
entry.overriden = true;
}
}
else
{
logGlobal->debug("Skipping translation override for string %s: changed in a different mod", UID.get());
}
}
void TextLocalizationContainer::addSubContainer(const TextLocalizationContainer & container)
{
std::lock_guard globalLock(globalTextMutex);
assert(!vstd::contains(subContainers, &container));
subContainers.push_back(&container);
}
void TextLocalizationContainer::removeSubContainer(const TextLocalizationContainer & container)
{
std::lock_guard globalLock(globalTextMutex);
assert(vstd::contains(subContainers, &container));
subContainers.erase(std::remove(subContainers.begin(), subContainers.end(), &container), subContainers.end());
}
const std::string & TextLocalizationContainer::translateString(const TextIdentifier & identifier) const
{
std::lock_guard globalLock(globalTextMutex);
if(stringsLocalizations.count(identifier.get()) == 0)
{
for(auto containerIter = subContainers.rbegin(); containerIter != subContainers.rend(); ++containerIter)
if((*containerIter)->identifierExists(identifier))
return (*containerIter)->translateString(identifier);
logGlobal->error("Unable to find localization for string '%s'", identifier.get());
return identifier.get();
}
const auto & entry = stringsLocalizations.at(identifier.get());
return entry.translatedText;
}
void TextLocalizationContainer::registerString(const std::string & modContext, const TextIdentifier & inputUID, const JsonNode & localized)
{
assert(localized.isNull() || !localized.getModScope().empty());
assert(localized.isNull() || !getModLanguage(localized.getModScope()).empty());
if (localized.isNull())
registerString(modContext, modContext, inputUID, localized.String());
else
registerString(modContext, localized.getModScope(), inputUID, localized.String());
}
void TextLocalizationContainer::registerString(const std::string & modContext, const TextIdentifier & UID, const std::string & localized)
{
registerString(modContext, modContext, UID, localized);
}
void TextLocalizationContainer::registerString(const std::string & identifierModContext, const std::string & localizedStringModContext, const TextIdentifier & UID, const std::string & localized)
{
std::lock_guard globalLock(globalTextMutex);
assert(!identifierModContext.empty());
assert(!localizedStringModContext.empty());
assert(UID.get().find("..") == std::string::npos); // invalid identifier - there is section that was evaluated to empty string
assert(stringsLocalizations.count(UID.get()) == 0 || boost::algorithm::starts_with(UID.get(), "map") || boost::algorithm::starts_with(UID.get(), "header")); // registering already registered string? FIXME: "header" is a workaround. VMAP needs proper integration in translation system
if(stringsLocalizations.count(UID.get()) > 0)
{
auto & value = stringsLocalizations[UID.get()];
value.translatedText = localized;
value.identifierModContext = identifierModContext;
value.baseStringModContext = localizedStringModContext;
}
else
{
StringState value;
value.translatedText = localized;
value.identifierModContext = identifierModContext;
value.baseStringModContext = localizedStringModContext;
stringsLocalizations[UID.get()] = value;
}
}
void TextLocalizationContainer::loadTranslationOverrides(const std::string & modContext, const std::string & language, const JsonNode & config)
{
for(const auto & node : config.Struct())
registerStringOverride(modContext, node.first, node.second.String(), language);
}
bool TextLocalizationContainer::identifierExists(const TextIdentifier & UID) const
{
std::lock_guard globalLock(globalTextMutex);
return stringsLocalizations.count(UID.get());
}
void TextLocalizationContainer::exportAllTexts(std::map<std::string, std::map<std::string, std::string>> & storage, bool onlyMissing) const
{
std::lock_guard globalLock(globalTextMutex);
for (auto const & subContainer : subContainers)
subContainer->exportAllTexts(storage, onlyMissing);
for (auto const & entry : stringsLocalizations)
{
if (onlyMissing && entry.second.overriden)
continue;
std::string textToWrite;
std::string modName = entry.second.baseStringModContext;
if (entry.second.baseStringModContext == entry.second.identifierModContext && modName.find('.') != std::string::npos)
modName = modName.substr(0, modName.find('.'));
boost::range::replace(modName, '.', '_');
textToWrite = entry.second.translatedText;
if (!textToWrite.empty())
storage[modName][entry.first] = textToWrite;
}
}
std::string TextLocalizationContainer::getModLanguage(const std::string & modContext)
{
if (modContext == "core")
return CGeneralTextHandler::getInstalledLanguage();
return VLC->modh->getModLanguage(modContext);
}
void TextLocalizationContainer::jsonSerialize(JsonNode & dest) const
{
std::lock_guard globalLock(globalTextMutex);
for(auto & s : stringsLocalizations)
dest.Struct()[s.first].String() = s.second.translatedText;
}
TextContainerRegistrable::TextContainerRegistrable()
{
VLC->generaltexth->addSubContainer(*this);
}
TextContainerRegistrable::~TextContainerRegistrable()
{
VLC->generaltexth->removeSubContainer(*this);
}
TextContainerRegistrable::TextContainerRegistrable(const TextContainerRegistrable & other)
: TextLocalizationContainer(other)
{
VLC->generaltexth->addSubContainer(*this);
}
TextContainerRegistrable::TextContainerRegistrable(TextContainerRegistrable && other) noexcept
:TextLocalizationContainer(other)
{
VLC->generaltexth->addSubContainer(*this);
}
VCMI_LIB_NAMESPACE_END
|