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/twrp-functions.cpp b/twrp-functions.cpp
index 644d565..a7a8e8d 100644
--- a/twrp-functions.cpp
+++ b/twrp-functions.cpp
@@ -45,6 +45,7 @@
 #include "bootloader.h"
 #include "cutils/properties.h"
 #include "cutils/android_reboot.h"
+#include "gui/gui.hpp"
 #include <sys/reboot.h>
 #endif // ndef BUILD_TWRPTAR_MAIN
 #ifndef TW_EXCLUDE_ENCRYPTED_BACKUPS
@@ -122,12 +123,12 @@
 	rc_pid = waitpid(pid, status, 0);
 	if (rc_pid > 0) {
 		if (WIFSIGNALED(*status)) {
-			LOGERR("%s process ended with signal: %d\n", Child_Name.c_str(), WTERMSIG(*status)); // Seg fault or some other non-graceful termination
+			gui_msg(Msg(msg::kError, "pid_signal={1} process ended with signal: {2}")(Child_Name)(WTERMSIG(*status))); // Seg fault or some other non-graceful termination
 			return -1;
 		} else if (WEXITSTATUS(*status) == 0) {
 			LOGINFO("%s process ended with RC=%d\n", Child_Name.c_str(), WEXITSTATUS(*status)); // Success
 		} else {
-			LOGERR("%s process ended with ERROR=%d\n", Child_Name.c_str(), WEXITSTATUS(*status)); // Graceful exit, but there was an error
+			gui_msg(Msg(msg::kError, "pid_error={1} process ended with ERROR: {2}")(Child_Name)(WEXITSTATUS(*status))); // Graceful exit, but there was an error
 			return -1;
 		}
 	} else { // no PID returned
@@ -207,13 +208,13 @@
 
 	f = fopen(fn.c_str(), "rb");
 	if (f == NULL) {
-		LOGERR("Failed to open '%s' to try decrypt\n", fn.c_str());
+		LOGERR("Failed to open '%s' to try decrypt: %s\n", fn.c_str(), strerror(errno));
 		oaes_free(&ctx);
 		return -1;
 	}
 	read_len = fread(buffer, sizeof(uint8_t), 4096, f);
 	if (read_len <= 0) {
-		LOGERR("Read size during try decrypt failed\n");
+		LOGERR("Read size during try decrypt failed: %s\n", strerror(errno));
 		fclose(f);
 		oaes_free(&ctx);
 		return -1;
@@ -355,50 +356,50 @@
 	if (!PartitionManager.Mount_By_Path("/data", true))
 		return;
 
-	gui_print("Installing HTC Dumlock to system...\n");
+	gui_msg("install_dumlock=Installing HTC Dumlock to system...");
 	copy_file(TWHTCD_PATH "htcdumlocksys", "/system/bin/htcdumlock", 0755);
 	if (!Path_Exists("/system/bin/flash_image")) {
-		gui_print("Installing flash_image...\n");
+		LOGINFO("Installing flash_image...\n");
 		copy_file(TWHTCD_PATH "flash_imagesys", "/system/bin/flash_image", 0755);
 		need_libs = 1;
 	} else
-		gui_print("flash_image is already installed, skipping...\n");
+		LOGINFO("flash_image is already installed, skipping...\n");
 	if (!Path_Exists("/system/bin/dump_image")) {
-		gui_print("Installing dump_image...\n");
+		LOGINFO("Installing dump_image...\n");
 		copy_file(TWHTCD_PATH "dump_imagesys", "/system/bin/dump_image", 0755);
 		need_libs = 1;
 	} else
-		gui_print("dump_image is already installed, skipping...\n");
+		LOGINFO("dump_image is already installed, skipping...\n");
 	if (need_libs) {
-		gui_print("Installing libs needed for flash_image and dump_image...\n");
+		LOGINFO("Installing libs needed for flash_image and dump_image...\n");
 		copy_file(TWHTCD_PATH "libbmlutils.so", "/system/lib/libbmlutils.so", 0644);
 		copy_file(TWHTCD_PATH "libflashutils.so", "/system/lib/libflashutils.so", 0644);
 		copy_file(TWHTCD_PATH "libmmcutils.so", "/system/lib/libmmcutils.so", 0644);
 		copy_file(TWHTCD_PATH "libmtdutils.so", "/system/lib/libmtdutils.so", 0644);
 	}
-	gui_print("Installing HTC Dumlock app...\n");
+	LOGINFO("Installing HTC Dumlock app...\n");
 	mkdir("/data/app", 0777);
 	unlink("/data/app/com.teamwin.htcdumlock*");
 	copy_file(TWHTCD_PATH "HTCDumlock.apk", "/data/app/com.teamwin.htcdumlock.apk", 0777);
 	sync();
-	gui_print("HTC Dumlock is installed.\n");
+	gui_msg("done=Done.");
 }
 
 void TWFunc::htc_dumlock_restore_original_boot(void) {
 	if (!PartitionManager.Mount_By_Path("/sdcard", true))
 		return;
 
-	gui_print("Restoring original boot...\n");
+	gui_msg("dumlock_restore=Restoring original boot...");
 	Exec_Cmd("htcdumlock restore");
-	gui_print("Original boot restored.\n");
+	gui_msg("done=Done.");
 }
 
 void TWFunc::htc_dumlock_reflash_recovery_to_boot(void) {
 	if (!PartitionManager.Mount_By_Path("/sdcard", true))
 		return;
-	gui_print("Reflashing recovery to boot...\n");
+	gui_msg("dumlock_reflash=Reflashing recovery to boot...");
 	Exec_Cmd("htcdumlock recovery noreboot");
-	gui_print("Recovery is flashed to boot.\n");
+	gui_msg("done=Done.");
 }
 
 int TWFunc::Recursive_Mkdir(string Path) {
@@ -411,7 +412,7 @@
 		wholePath = pathCpy.substr(0, pos);
 		if (!TWFunc::Path_Exists(wholePath)) {
 			if (mkdir(wholePath.c_str(), 0777)) {
-				LOGERR("Unable to create folder: %s  (errno=%d)\n", wholePath.c_str(), errno);
+				gui_msg(Msg(msg::kError, "create_folder_strerr=Can not create '{1}' folder ({2})")(wholePath)(strerror(errno)));
 				return false;
 			} else {
 				tw_set_default_metadata(wholePath.c_str());
@@ -570,10 +571,10 @@
 	// Check for and run startup script if script exists
 	struct stat st;
 	if (stat(script_file, &st) == 0) {
-		gui_print("Running %s script...\n", display_name);
+		gui_msg(Msg("run_script=Running {1} script...")(display_name));
 		chmod(script_file, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
 		TWFunc::Exec_Cmd(script_file);
-		gui_print("\nFinished running %s script.\n", display_name);
+		gui_msg("done=Done.");
 	}
 }
 
@@ -583,7 +584,7 @@
 	string new_path;
 
 	if (d == NULL) {
-		LOGERR("Error opening dir: '%s'\n", path.c_str());
+		gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(path)(strerror(errno)));
 		return -1;
 	}
 
@@ -742,7 +743,7 @@
 	Restore_Path += "/";
 	d = opendir(Restore_Path.c_str());
 	if (d == NULL) {
-		LOGERR("Error opening '%s'\n", Restore_Path.c_str());
+		gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Restore_Path)(strerror(errno)));
 		return false;
 	}
 
@@ -1042,7 +1043,7 @@
 		// Disable flashing of stock recovery
 		if (TWFunc::Path_Exists("/system/recovery-from-boot.p")) {
 			rename("/system/recovery-from-boot.p", "/system/recovery-from-boot.bak");
-			gui_print("Renamed stock recovery file in /system to prevent\nthe stock ROM from replacing TWRP.\n");
+			gui_msg("rename_stock=Renamed stock recovery file in /system to prevent the stock ROM from replacing TWRP.");
 			sync();
 		}
 		PartitionManager.UnMount_By_Path("/system", false);