Switch language at runtime

This commit is contained in:
luboslenco
2020-03-21 12:46:50 +01:00
parent b10e74be42
commit bd079ab6fb
7 changed files with 69 additions and 59 deletions
+1 -1
View File
@@ -54,7 +54,7 @@
"Export Mesh": "Exporter le maillage",
"Apply Displacement": "Appliquer déplacement",
"Interface": "Interface",
"Language (restart to apply changes)": "Langue (redémarrer pour appliquer)",
"Language": "Langue",
"UI Scale": "Échelle UI",
"Theme": "Thème",
"Native File Browser": "Explorateur de fichiers natif",
+3 -47
View File
@@ -39,10 +39,6 @@ import arm.Res;
class App {
public static var uiEnabled = true;
// The locale should be specified in ISO 639-1 format: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
// "system" is a special case that will use the system locale
public static var locale = "system";
public static var translations: Map<String, String> = [];
public static var isDragging = false;
public static var isResizing = false;
public static var dragMaterial: MaterialSlot = null;
@@ -71,12 +67,10 @@ class App {
public function new() {
Log.init();
lastWindowHeight = System.windowHeight();
Translator.loadTranslations(Config.raw.locale);
if (Config.raw.locale != null) {
locale = Config.raw.locale;
};
loadTranslations(locale);
lastWindowHeight = System.windowHeight();
UIFiles.filename = tr("untitled");
#if arm_resizable
iron.App.onResize = onResize;
@@ -187,44 +181,6 @@ class App {
});
}
// (Re)loads translations for the specified locale.
public static function loadTranslations(newLocale: String) {
if (newLocale == "system") {
locale = Krom.language();
}
// Check whether the requested or detected locale is available
if (getSupportedLocales().indexOf(newLocale) == -1) {
// Fall back to English
locale = "en";
}
if (locale == "en") {
// No translations to load, as source strings are in English.
// Clear existing translations if switching languages at runtime.
translations.clear();
return;
}
// Load the translation file
var translationJson = Bytes.ofData(Krom.loadBlob('data/locale/${locale}.json')).toString();
var data: haxe.DynamicAccess<String> = haxe.Json.parse(translationJson);
for (key => value in data) {
translations[Std.string(key)] = value;
}
}
// Returns a list of supported locales (plus English and the automatically detected system locale).
public static function getSupportedLocales(): Array<String> {
var locales = ["system", "en"];
for (localeFilename in File.readDirectory(Path.data() + Path.sep + "locale")) {
// Trim the `.json` file extension from file names
locales.push(localeFilename.substr(0, -5));
}
return locales;
}
static function saveAndQuitCallback(save: Bool) {
saveWindowRect();
if (save) Project.projectSave(true);
+5 -2
View File
@@ -93,6 +93,7 @@ class Config {
zui.Zui.Handle.global = new zui.Zui.Handle(); // Reset ui handles
configLoaded = false;
init();
Translator.loadTranslations(raw.locale);
#if arm_painter
applyConfig();
#end
@@ -182,8 +183,10 @@ class Config {
}
typedef TConfig = {
@:optional var locale: String; // ISO 639-1 locale code or "system" to use the system locale automatically
// Window
// The locale should be specified in ISO 639-1 format: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
// "system" is a special case that will use the system locale
@:optional var locale: String;
// Window
@:optional var window_mode: Null<Int>; // window, fullscreen
@:optional var window_w: Null<Int>;
@:optional var window_h: Null<Int>;
+48 -3
View File
@@ -1,15 +1,22 @@
package arm;
import haxe.io.Bytes;
import haxe.Json;
import arm.sys.File;
import arm.sys.Path;
class Translator {
static var translations: Map<String, String> = [];
// Localizes a string with the given placeholders replaced (format is `{placeholderName}`).
// If the string isn't available in the translation, this method will return the source English string.
public static function tr(id: String, ?vars: Map<String, Dynamic>): String {
public static function tr(id: String, vars: Map<String, String> = null): String {
var translation = id;
// English is the source language
if (App.locale != "en" && App.translations != null && App.translations.exists(id)) {
translation = App.translations[id];
if (Config.raw.locale != "en" && translations.exists(id)) {
translation = translations[id];
}
if (vars != null) {
@@ -20,4 +27,42 @@ class Translator {
return translation;
}
// (Re)loads translations for the specified locale.
public static function loadTranslations(newLocale: String) {
if (newLocale == "system") {
Config.raw.locale = Krom.language();
}
// Check whether the requested or detected locale is available
if (Config.raw.locale != "en" && getSupportedLocales().indexOf(newLocale) == -1) {
// Fall back to English
Config.raw.locale = "en";
}
if (Config.raw.locale == "en") {
// No translations to load, as source strings are in English.
// Clear existing translations if switching languages at runtime.
translations.clear();
return;
}
// Load the translation file
var translationJson = Bytes.ofData(Krom.loadBlob('data/locale/${Config.raw.locale}.json')).toString();
var data: haxe.DynamicAccess<String> = Json.parse(translationJson);
for (key => value in data) {
translations[Std.string(key)] = value;
}
}
// Returns a list of supported locales (plus English and the automatically detected system locale).
public static function getSupportedLocales(): Array<String> {
var locales = ["system", "en"];
for (localeFilename in File.readDirectory(Path.data() + Path.sep + "locale")) {
// Trim the `.json` file extension from file names
locales.push(localeFilename.substr(0, -5));
}
return locales;
}
}
+8 -4
View File
@@ -18,20 +18,24 @@ class BoxPreferences {
public static var filesPlugin: Array<String> = null;
public static var filesKeymap: Array<String> = null;
public static var presetHandle: Handle;
static var locales: Array<String> = null;
@:access(zui.Zui)
public static function show() {
UIBox.showCustom(function(ui: Zui) {
if (ui.tab(htab, tr("Interface"), true)) {
var locales = App.getSupportedLocales();
var localeHandle = Id.handle({position: locales.indexOf(App.locale)});
ui.combo(localeHandle, locales, tr("Language (restart to apply changes)"), true);
if (locales == null) {
locales = Translator.getSupportedLocales();
}
var localeHandle = Id.handle({position: locales.indexOf(Config.raw.locale)});
ui.combo(localeHandle, locales, tr("Language"), true);
if (localeHandle.changed) {
var localeCode = locales[localeHandle.position];
Config.raw.locale = localeCode;
Config.save();
App.loadTranslations(localeCode);
Translator.loadTranslations(localeCode);
UISidebar.inst.tagUIRedraw();
}
+1 -1
View File
@@ -12,7 +12,7 @@ import arm.sys.File;
class UIFiles {
public static var filename = tr("untitled");
public static var filename: String;
public static var path = defaultPath;
static var lastPath = "";
static var files: Array<String> = null;
+3 -1
View File
@@ -136,7 +136,7 @@ class UISidebar {
public var showAssetNames = false;
public var textToolImage: Image = null;
public var textToolText = tr("Text");
public var textToolText: String;
public var textToolHandle = new Handle();
public var particleMaterial: MaterialData = null;
@@ -277,6 +277,8 @@ class UISidebar {
new UIStatus();
new UIMenubar();
textToolText = tr("Text");
windowW = Std.int(defaultWindowW * Config.raw.window_scale);
UIToolbar.inst.toolbarw = Std.int(UIToolbar.defaultToolbarW * Config.raw.window_scale);
UIHeader.inst.headerh = Std.int(UIHeader.defaultHeaderH * Config.raw.window_scale);