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
diff --git a/gui/gui.cpp b/gui/gui.cpp
index 31b61be..78e8a7e 100644
--- a/gui/gui.cpp
+++ b/gui/gui.cpp
@@ -517,13 +517,19 @@
 			fprintf(orsout, "%s", result);
 			LOGINFO("Command cannot be performed, operation in progress.\n");
 		} else {
-			if (gui_console_only() == 0) {
+			if (strlen(command) == 11 && strncmp(command, "dumpstrings", 11) == 0) {
+				// This cannot be done safely with gui_console_only because gui_console_only updates mCurrentSet
+				// which makes the resources that we are trying to read unreachable.
+				gui_set_FILE(orsout);
+				PageManager::GetResources()->DumpStrings();
+				gui_set_FILE(NULL);
+			} else if (gui_console_only() == 0) {
 				LOGINFO("Console started successfully\n");
 				gui_set_FILE(orsout);
 				if (strlen(command) > 11 && strncmp(command, "runscript", 9) == 0) {
 					char* filename = command + 11;
 					if (OpenRecoveryScript::copy_script_file(filename) == 0) {
-						LOGERR("Unable to copy script file\n");
+						LOGINFO("Unable to copy script file\n");
 					} else {
 						OpenRecoveryScript::run_script_file();
 					}
@@ -534,7 +540,7 @@
 					gui_print("%s = %s\n", varname, temp.c_str());
 				} else if (strlen(command) > 9 && strncmp(command, "decrypt", 7) == 0) {
 					char* pass = command + 8;
-					gui_print("Attempting to decrypt data partition via command line.\n");
+					gui_msg("decrypt_cmd=Attempting to decrypt data partition via command line.");
 					if (PartitionManager.Decrypt_Device(pass) == 0) {
 						set_page_done = 1;
 					}
@@ -753,15 +759,39 @@
 {
 	// This function parses text for DataManager values encompassed by %value% in the XML
 	// and string resources (%@resource_name%)
-	size_t pos = 0;
+	size_t pos = 0, next, end;
 
 	while (1)
 	{
-		size_t next = str.find('%', pos);
+		next = str.find("{@", pos);
+		if (next == std::string::npos)
+			break;
+
+		end = str.find('}', next + 1);
+		if (end == std::string::npos)
+			break;
+
+		std::string var = str.substr(next + 2, (end - next) - 2);
+		str.erase(next, (end - next) + 1);
+
+		size_t default_loc = var.find('=', 0);
+		std::string lookup;
+		if (default_loc == std::string::npos) {
+			str.insert(next, PageManager::GetResources()->FindString(var));
+		} else {
+			lookup = var.substr(0, default_loc);
+			std::string default_string = var.substr(default_loc + 1, var.size() - default_loc - 1);
+			str.insert(next, PageManager::GetResources()->FindString(lookup, default_string));
+		}
+	}
+	pos = 0;
+	while (1)
+	{
+		next = str.find('%', pos);
 		if (next == std::string::npos)
 			return str;
 
-		size_t end = str.find('%', next + 1);
+		end = str.find('%', next + 1);
 		if (end == std::string::npos)
 			return str;
 
@@ -787,6 +817,10 @@
 	}
 }
 
+std::string gui_lookup(const std::string& resource_name, const std::string& default_value) {
+	return PageManager::GetResources()->FindString(resource_name, default_value);
+}
+
 extern "C" int gui_init(void)
 {
 	gr_init();
@@ -828,7 +862,7 @@
 	{
 		if (PageManager::LoadPackage("TWRP", TWRES "ui.xml", "decrypt"))
 		{
-			LOGERR("Failed to load base packages.\n");
+			gui_err("base_pkg_err=Failed to load base packages.");
 			goto error;
 		}
 		else
@@ -849,10 +883,9 @@
 				retry_count--;
 			}
 
-			if (!PartitionManager.Mount_Settings_Storage(false))
+			if (!PartitionManager.Mount_Settings_Storage(true))
 			{
-				LOGERR("Unable to mount %s during GUI startup.\n",
-					   theme_path.c_str());
+				LOGINFO("Unable to mount %s during GUI startup.\n", theme_path.c_str());
 				check = 1;
 			}
 		}
@@ -863,7 +896,7 @@
 #endif // ifndef TW_OEM_BUILD
 			if (PageManager::LoadPackage("TWRP", TWRES "ui.xml", "main"))
 			{
-				LOGERR("Failed to load base packages.\n");
+				gui_err("base_pkg_err=Failed to load base packages.");
 				goto error;
 			}
 #ifndef TW_OEM_BUILD
@@ -886,7 +919,7 @@
 {
 #ifndef TW_OEM_BUILD
 	if (!PartitionManager.Mount_Settings_Storage(false)) {
-		LOGERR("Unable to mount settings storage during GUI startup.\n");
+		LOGINFO("Unable to mount settings storage during GUI startup.\n");
 		return -1;
 	}
 
@@ -898,7 +931,7 @@
 		if (PageManager::ReloadPackage("TWRP", theme_path)) {
 			// Custom theme failed to load, try to load stock theme
 			if (PageManager::ReloadPackage("TWRP", TWRES "ui.xml")) {
-				LOGERR("Failed to load base packages.\n");
+				gui_err("base_pkg_err=Failed to load base packages.");
 				goto error;
 			}
 		}