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/console.cpp | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 103 insertions(+), 10 deletions(-) (limited to 'gui/console.cpp') diff --git a/gui/console.cpp b/gui/console.cpp index 6b38d6137..b5204fb7c 100644 --- a/gui/console.cpp +++ b/gui/console.cpp @@ -1,3 +1,21 @@ +/* + Copyright 2015 bigbiff/Dees_Troy 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 . +*/ + // console.cpp - GUIConsole object #include @@ -24,10 +42,16 @@ extern "C" { #include "rapidxml.hpp" #include "objects.hpp" +#include "gui.hpp" +#include "twmsg.h" + +#define GUI_CONSOLE_BUFFER_SIZE 512 +size_t last_message_count = 0; +std::vector gMessages; -static std::vector gConsole; -static std::vector gConsoleColor; +std::vector gConsole; +std::vector gConsoleColor; static FILE* ors_file; extern "C" void __gui_print(const char *color, char *buf) @@ -58,22 +82,22 @@ extern "C" void __gui_print(const char *color, char *buf) gConsole.push_back(start); gConsoleColor.push_back(color); } - if (ors_file) { - fprintf(ors_file, "%s\n", buf); - fflush(ors_file); - } } extern "C" void gui_print(const char *fmt, ...) { - char buf[512]; // We're going to limit a single request to 512 bytes + char buf[GUI_CONSOLE_BUFFER_SIZE]; // We're going to limit a single request to 512 bytes va_list ap; va_start(ap, fmt); - vsnprintf(buf, 512, fmt, ap); + vsnprintf(buf, GUI_CONSOLE_BUFFER_SIZE, fmt, ap); va_end(ap); fputs(buf, stdout); + if (ors_file) { + fprintf(ors_file, "%s", buf); + fflush(ors_file); + } __gui_print("normal", buf); return; @@ -81,14 +105,18 @@ extern "C" void gui_print(const char *fmt, ...) extern "C" void gui_print_color(const char *color, const char *fmt, ...) { - char buf[512]; // We're going to limit a single request to 512 bytes + char buf[GUI_CONSOLE_BUFFER_SIZE]; // We're going to limit a single request to 512 bytes va_list ap; va_start(ap, fmt); - vsnprintf(buf, 512, fmt, ap); + vsnprintf(buf, GUI_CONSOLE_BUFFER_SIZE, fmt, ap); va_end(ap); fputs(buf, stdout); + if (ors_file) { + fprintf(ors_file, "%s", buf); + fflush(ors_file); + } __gui_print(color, buf); return; @@ -99,6 +127,70 @@ extern "C" void gui_set_FILE(FILE* f) ors_file = f; } +void gui_msg(const char* text) +{ + if (text) { + Message msg = Msg(text); + gui_msg(msg); + } +} + +void gui_warn(const char* text) +{ + if (text) { + Message msg = Msg(msg::kWarning, text); + gui_msg(msg); + } +} + +void gui_err(const char* text) +{ + if (text) { + Message msg = Msg(msg::kError, text); + gui_msg(msg); + } +} + +void gui_highlight(const char* text) +{ + if (text) { + Message msg = Msg(msg::kHighlight, text); + gui_msg(msg); + } +} + +void gui_msg(Message msg) +{ + std::string output = msg; + output += "\n"; + fputs(output.c_str(), stdout); + if (ors_file) { + fprintf(ors_file, "%s", output.c_str()); + fflush(ors_file); + } + gMessages.push_back(msg); +} + +void GUIConsole::Translate_Now() { + size_t message_count = gMessages.size(); + if (message_count <= last_message_count) + return; + + for (size_t m = last_message_count; m < message_count; m++) { + std::string message = gMessages[m]; + std::string color = "normal"; + if (gMessages[m].GetKind() == msg::kError) + color = "error"; + else if (gMessages[m].GetKind() == msg::kHighlight) + color = "highlight"; + else if (gMessages[m].GetKind() == msg::kWarning) + color = "warning"; + gConsole.push_back(message); + gConsoleColor.push_back(color); + } + last_message_count = message_count; +} + GUIConsole::GUIConsole(xml_node<>* node) : GUIScrollList(node) { xml_node<>* child; @@ -160,6 +252,7 @@ int GUIConsole::RenderSlideout(void) int GUIConsole::RenderConsole(void) { + Translate_Now(); AddLines(&gConsole, &gConsoleColor, &mLastCount, &rConsole, &rConsoleColor); GUIScrollList::Render(); -- cgit v1.2.3