LoadLocalizationFile appends loca instead of erasing
This commit is contained in:
parent
44ab06efbe
commit
55eee90abe
|
|
@ -28,6 +28,9 @@ namespace Nz
|
||||||
const std::string& GetStringAtIndex(size_t index) const;
|
const std::string& GetStringAtIndex(size_t index) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
size_t GetOrCreateLocale(std::string_view name);
|
||||||
|
size_t GetOrCreateLookupIndex(std::string_view name);
|
||||||
|
|
||||||
struct Locale
|
struct Locale
|
||||||
{
|
{
|
||||||
std::string name;
|
std::string name;
|
||||||
|
|
@ -38,6 +41,7 @@ namespace Nz
|
||||||
std::unordered_map<std::string, size_t> m_lookupTable;
|
std::unordered_map<std::string, size_t> m_lookupTable;
|
||||||
|
|
||||||
Locale* m_currentLocale;
|
Locale* m_currentLocale;
|
||||||
|
size_t m_currentLocaleIndex;
|
||||||
|
|
||||||
static Localization* s_instance;
|
static Localization* s_instance;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ namespace Nz
|
||||||
Localization::Localization(Config /*config*/)
|
Localization::Localization(Config /*config*/)
|
||||||
: ModuleBase("Localization", this)
|
: ModuleBase("Localization", this)
|
||||||
, m_currentLocale(nullptr)
|
, m_currentLocale(nullptr)
|
||||||
|
, m_currentLocaleIndex(0)
|
||||||
{ }
|
{ }
|
||||||
|
|
||||||
Localization::~Localization()
|
Localization::~Localization()
|
||||||
|
|
@ -26,14 +27,13 @@ namespace Nz
|
||||||
std::string line;
|
std::string line;
|
||||||
std::getline(file, line);
|
std::getline(file, line);
|
||||||
|
|
||||||
m_locales.clear();
|
std::vector<size_t> locales;
|
||||||
SplitString(line, ";", [this](std::string_view str) {
|
SplitString(line, ";", [&](std::string_view str) {
|
||||||
if (!str.empty())
|
if (!str.empty())
|
||||||
m_locales.push_back({ std::string(str) });
|
locales.push_back(GetOrCreateLocale(str));
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
size_t index = 0;
|
|
||||||
while (std::getline(file, line))
|
while (std::getline(file, line))
|
||||||
{
|
{
|
||||||
std::vector<std::string_view> values;
|
std::vector<std::string_view> values;
|
||||||
|
|
@ -42,24 +42,37 @@ namespace Nz
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
|
||||||
m_lookupTable[std::string(values[0])] = index++;
|
size_t lookup = GetOrCreateLookupIndex(values[0]);
|
||||||
for (size_t i = 0; i < m_locales.size() && i < values.size() - 1; ++i)
|
|
||||||
|
for (size_t i = 0; i < values.size() - 1; ++i)
|
||||||
{
|
{
|
||||||
m_locales[i].localizedStrings.push_back(std::string(values[i + 1]));
|
if (m_locales[locales[i]].localizedStrings.size() <= lookup)
|
||||||
|
m_locales[locales[i]].localizedStrings.resize(lookup + 1);
|
||||||
|
|
||||||
|
m_locales[locales[i]].localizedStrings[lookup] = std::string(values[i + 1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ensure all loaded locales have values (even if empty) for all lookup keys
|
||||||
|
for (auto&& locale : m_locales)
|
||||||
|
locale.localizedStrings.resize(m_lookupTable.size());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Localization::SetLocale(const std::string& locale)
|
bool Localization::SetLocale(const std::string& locale)
|
||||||
{
|
{
|
||||||
auto it = std::find_if(m_locales.begin(), m_locales.end(), [locale](auto&& loc) { return loc.name == locale; });
|
for (size_t i = 0; i < m_locales.size(); ++i)
|
||||||
if (it == m_locales.end())
|
|
||||||
{
|
{
|
||||||
m_currentLocale = nullptr;
|
if (m_locales[i].name == locale)
|
||||||
return false;
|
{
|
||||||
|
m_currentLocale = &m_locales[i];
|
||||||
|
m_currentLocaleIndex = i;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
m_currentLocale = &(*it);
|
m_currentLocale = &(*it);
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -84,4 +97,32 @@ namespace Nz
|
||||||
return m_currentLocale->localizedStrings.at(index);
|
return m_currentLocale->localizedStrings.at(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t Localization::GetOrCreateLocale(std::string_view locale)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < m_locales.size(); ++i)
|
||||||
|
if (m_locales[i].name == locale)
|
||||||
|
return i;
|
||||||
|
|
||||||
|
m_locales.emplace_back(std::string(locale));
|
||||||
|
|
||||||
|
if (nullptr != m_currentLocale)
|
||||||
|
{
|
||||||
|
// make sure pointer is still valid (this will invalidate all LocalizedText)
|
||||||
|
m_currentLocale = &m_locales[m_currentLocaleIndex];
|
||||||
|
}
|
||||||
|
|
||||||
|
return m_locales.size() - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Localization::GetOrCreateLookupIndex(std::string_view name)
|
||||||
|
{
|
||||||
|
auto it = m_lookupTable.find(std::string(name));
|
||||||
|
if (it != m_lookupTable.end())
|
||||||
|
return it->second;
|
||||||
|
|
||||||
|
size_t index = m_lookupTable.size();
|
||||||
|
m_lookupTable[std::string(name)] = index;
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
} // end of namespace Nz
|
} // end of namespace Nz
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue