Added support for using language variables in the theme configuration

This commit is contained in:
Leon Styhre 2024-08-31 12:52:20 +02:00
parent 8997cf2209
commit d4a0f32dd0
2 changed files with 48 additions and 0 deletions

View file

@ -684,6 +684,9 @@ void ThemeData::loadFile(const std::map<std::string, std::string>& sysDataMap,
}
if (sCurrentTheme->second.capabilities.languages.size() > 0) {
for (auto& language : sCurrentTheme->second.capabilities.languages)
mLanguages.emplace_back(language);
std::string langSetting {Settings::getInstance()->getString("ThemeLanguage")};
if (langSetting == "automatic")
langSetting = Utils::Localization::sCurrentLocale;
@ -715,6 +718,7 @@ void ThemeData::loadFile(const std::map<std::string, std::string>& sysDataMap,
parseVariables(root);
parseColorSchemes(root);
parseFontSizes(root);
parseLanguages(root);
parseIncludes(root);
parseViews(root);
if (root.child("feature") != nullptr)
@ -1701,6 +1705,7 @@ void ThemeData::parseIncludes(const pugi::xml_node& root)
parseVariables(theme);
parseColorSchemes(theme);
parseFontSizes(theme);
parseLanguages(theme);
parseIncludes(theme);
parseViews(theme);
if (theme.child("feature") != nullptr)
@ -1751,6 +1756,7 @@ void ThemeData::parseVariants(const pugi::xml_node& root)
parseVariables(node);
parseColorSchemes(node);
parseFontSizes(node);
parseLanguages(node);
parseIncludes(node);
parseViews(node);
parseAspectRatios(node);
@ -1873,6 +1879,7 @@ void ThemeData::parseAspectRatios(const pugi::xml_node& root)
parseVariables(node);
parseColorSchemes(node);
parseFontSizes(node);
parseLanguages(node);
parseIncludes(node);
parseViews(node);
}
@ -1901,6 +1908,45 @@ void ThemeData::parseTransitions(const pugi::xml_node& root)
}
}
void ThemeData::parseLanguages(const pugi::xml_node& root)
{
if (sCurrentTheme == sThemes.end())
return;
if (sThemeLanguage == "")
return;
ThemeException error;
error << "ThemeData::parseLanguages(): ";
error.setFiles(mPaths);
for (pugi::xml_node node {root.child("language")}; node; node = node.next_sibling("language")) {
if (!node.attribute("name"))
throw error << ": <language> tag missing \"name\" attribute";
const std::string delim {" \t\r\n,"};
const std::string nameAttr {node.attribute("name").as_string()};
size_t prevOff {nameAttr.find_first_not_of(delim, 0)};
size_t off {nameAttr.find_first_of(delim, prevOff)};
std::string viewKey;
while (off != std::string::npos || prevOff != std::string::npos) {
viewKey = nameAttr.substr(prevOff, off - prevOff);
prevOff = nameAttr.find_first_not_of(delim, off);
off = nameAttr.find_first_of(delim, prevOff);
if (std::find(mLanguages.cbegin(), mLanguages.cend(), viewKey) == mLanguages.cend()) {
throw error << ": <language> value \"" << viewKey
<< "\" is not defined in capabilities.xml";
}
if (sThemeLanguage == viewKey) {
parseVariables(node);
parseIncludes(node);
}
}
}
}
void ThemeData::parseVariables(const pugi::xml_node& root)
{
ThemeException error;

View file

@ -256,6 +256,7 @@ private:
void parseVariants(const pugi::xml_node& root);
void parseColorSchemes(const pugi::xml_node& root);
void parseFontSizes(const pugi::xml_node& root);
void parseLanguages(const pugi::xml_node& root);
void parseAspectRatios(const pugi::xml_node& root);
void parseTransitions(const pugi::xml_node& root);
void parseVariables(const pugi::xml_node& root);
@ -292,6 +293,7 @@ private:
std::vector<std::string> mVariants;
std::vector<std::string> mColorSchemes;
std::vector<std::string> mFontSizes;
std::vector<std::string> mLanguages;
std::string mSelectedVariant;
std::string mOverrideVariant;
std::string mSelectedColorScheme;