From 74db157b9406594a549a70415668dd6cbe17d1d3 Mon Sep 17 00:00:00 2001 From: Ethan Yonker Date: Wed, 28 Oct 2015 12:44:49 -0500 Subject: Multiple Language Support This is similar to https://gerrit.omnirom.org/#/c/14014 A lot of the features built in the older patch set have been split out into separate patches, most of which have already been merged. The remaining functionality here should all be directly related to language selection and loading. We always load English as a base before loading other languages over the top of the base. The idea is that if another language is missing a translation, then we will still display the English. Maybe still to do: read the /cache/recovery/last_locale file and load a language based on that. For me, this file contains just: en_US We probably won't bother with region specific translations so we would have to look at either trimming off the _US or using some other method like perhaps a symlink or a combination of the two. Thanks to _that for twmsg.cpp class Change-Id: I9647a22e47883a3ddd2de1da51f64aab7c328f74 --- gui/twmsg.cpp | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 gui/twmsg.cpp (limited to 'gui/twmsg.cpp') diff --git a/gui/twmsg.cpp b/gui/twmsg.cpp new file mode 100644 index 000000000..00c693150 --- /dev/null +++ b/gui/twmsg.cpp @@ -0,0 +1,129 @@ +/* + Copyright 2015 _that/TeamWin + This file is part of TWRP/TeamWin Recovery Project. + + TWRP is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + TWRP is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with TWRP. If not, see . +*/ + +#include "../data.hpp" +#include "pages.hpp" +#include "resources.hpp" + +#include "twmsg.h" + +std::string Message::GetFormatString(const std::string& name) const +{ + std::string resname; + size_t pos = name.find('='); + if (pos == std::string::npos) + resname = name; + else + resname = name.substr(0, pos); + + std::string formatstr = resourceLookup(resname); + bool notfound = formatstr.empty() || formatstr[0] == '['; // HACK: TODO: integrate this with resource-not-found logic + if (notfound && pos != std::string::npos) + // resource not found - use the default format string specified after "=" + formatstr = name.substr(pos + 1); + return formatstr; +} + +// Look up in local replacement vars first, if not found then use outer lookup object +class LocalLookup : public StringLookup +{ + const std::vector& vars; + const StringLookup& next; + public: + LocalLookup(const std::vector& vars, const StringLookup& next) : vars(vars), next(next) {} + virtual std::string operator()(const std::string& name) const + { + if (!name.empty() && isdigit(name[0])) { // {1}..{9} + int i = atoi(name.c_str()); + if (i > 0 && i <= (int)vars.size()) + return vars[i - 1]; + } + return next(name); + } +}; + +// conversion to final string +Message::operator std::string() const +{ + // do resource lookup + std::string str = GetFormatString(name); + + LocalLookup lookup(variables, varLookup); + + // now insert stuff into curly braces + + size_t pos = 0; + while ((pos = str.find('{', pos)) < std::string::npos) { + size_t end = str.find('}', pos); + if (end == std::string::npos) + break; + + std::string varname = str.substr(pos + 1, end - pos - 1); + std::string vartext = lookup(varname); + + str.replace(pos, end - pos + 1, vartext); + } + // TODO: complain about too many or too few numbered replacement variables + return str; +} + +/* +Resource manager lookup +*/ +class ResourceLookup : public StringLookup +{ +public: + virtual std::string operator()(const std::string& name) const + { + const ResourceManager* res = PageManager::GetResources(); + if (res) + return res->FindString(name); + return name; + } +}; +ResourceLookup resourceLookup; + + +/* +DataManager lookup +*/ +class DataLookup : public StringLookup +{ +public: + virtual std::string operator()(const std::string& name) const + { + std::string value; + if (DataManager::GetValue(name, value) == 0) + return value; + else + return ""; + } +}; +DataLookup dataLookup; + + +// Utility functions to create messages. Short names to make usage convenient. +Message Msg(const char* name) +{ + return Message(msg::kNormal, name, resourceLookup, dataLookup); +} + +Message Msg(msg::Kind kind, const char* name) +{ + return Message(kind, name, resourceLookup, dataLookup); +} -- cgit v1.2.3