From aa9af9b052df75515dcf09bb08dfe7ebb57696d5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E2=8D=9A=7ENex?=
<87421482+NexIsDumb@users.noreply.github.com>
Date: Mon, 1 Apr 2024 21:10:09 +0200
Subject: [PATCH] very early wip
might crash idk
stuff is super very undone i still havent tested as i gtg for now
---
project.xml | 3 +
source/funkin/backend/assets/Paths.hx | 6 +
source/funkin/backend/system/MainState.hx | 1 +
.../funkin/backend/utils/TranslationsUtil.hx | 127 ++++++++++++++++++
source/funkin/options/Options.hx | 1 +
5 files changed, 138 insertions(+)
create mode 100644 source/funkin/backend/utils/TranslationsUtil.hx
diff --git a/project.xml b/project.xml
index 77ae6af6..0bb98931 100644
--- a/project.xml
+++ b/project.xml
@@ -124,6 +124,9 @@
+
+
+
diff --git a/source/funkin/backend/assets/Paths.hx b/source/funkin/backend/assets/Paths.hx
index d8c6ba39..35bfb08b 100644
--- a/source/funkin/backend/assets/Paths.hx
+++ b/source/funkin/backend/assets/Paths.hx
@@ -36,6 +36,12 @@ class Paths
public static inline function getPath(file:String, ?library:String)
return library != null ? '$library:assets/$library/$file' : 'assets/$file';
+ public static inline function translationsMain(key:String)
+ return getPath(key, 'translations');
+
+ public static inline function translations(key:String)
+ return translationsMain('${TranslationsUtil.get_curLanguage()}/$key');
+
public static inline function video(key:String, ?ext:String = "mp4")
return getPath('videos/$key.$ext');
diff --git a/source/funkin/backend/system/MainState.hx b/source/funkin/backend/system/MainState.hx
index 885da7b0..461a7e8e 100644
--- a/source/funkin/backend/system/MainState.hx
+++ b/source/funkin/backend/system/MainState.hx
@@ -55,6 +55,7 @@ class MainState extends FlxState {
Main.refreshAssets();
ModsFolder.onModSwitch.dispatch(ModsFolder.currentModFolder);
+ TranslationsUtil.setTransl();
DiscordUtil.init();
EventsData.reloadEvents();
TitleState.initialized = false;
diff --git a/source/funkin/backend/utils/TranslationsUtil.hx b/source/funkin/backend/utils/TranslationsUtil.hx
new file mode 100644
index 00000000..1d75cf9d
--- /dev/null
+++ b/source/funkin/backend/utils/TranslationsUtil.hx
@@ -0,0 +1,127 @@
+package funkin.backend.utils;
+
+import openfl.utils.Assets;
+import haxe.io.Path;
+import haxe.xml.Access;
+import haxe.Exception;
+
+/**
+ * The class used for translations based on the XMLs inside the translations folders.
+ *
+ * Made by @NexIsDumb originally for the Poldhub mod.
+ */
+class TranslationsUtil
+{
+ /**
+ * The current language selected translation map; If the current language it's english, this map will be empty.
+ *
+ * Using this class function it'll never be `null`.
+ */
+ public static var transMap(default, null):Map = [];
+
+ /**
+ * The default language used inside of the source code (`en` is English in this case).
+ */
+ public static var DEFAULT_LANGUAGE:String = 'en'; // no inline so psycopathic mods can edit it - Nex
+
+ /**
+ * Returns the current language.
+ */
+ inline public static function get_curLanguage():String
+ return Options.language;
+
+ /**
+ * Returns if the current language is the default one (`DEFAULT_LANGUAGE`).
+ */
+ inline public static function is_defaultLanguage():Bool
+ return Options.language == DEFAULT_LANGUAGE;
+
+ /**
+ * Returns if any translation is loaded.
+ */
+ inline public static function is_anyTransLoaded():Bool
+ return transMap != [];
+
+ /**
+ * Changes the translations map.
+ *
+ * If `name` is `null`, it's gonna use the current language.
+ */
+ public static function setTransl(?name:String)
+ transMap = loadLanguage(name == null ? get_curLanguage() : name);
+
+ /**
+ * This is for checking a translation, `defString` it's just the string that gets returned just in case it won't find the translation OR the current language selected is ``DEFAULT_LANGUAGE``.
+ *
+ * If `id` is `null` then it's gonna search using `defString`.
+ */
+ public static function checkTransl(defString:String, ?id:String):String
+ {
+ #if TRANSLATIONS_SUPPORT
+ if(id == null) id = defString;
+ if(transMap.exists(id)) return transMap.get(id);
+ #end
+ return defString;
+ }
+
+ /**
+ * Returns an array that specifies which translations were found.
+ */
+ public static function translList():Array
+ {
+ var translations:Array = [];
+ #if TRANSLATIONS_SUPPORT
+ // to finish once done with the lib thing - Nex
+ for(l in Assets.list()) {
+ var d = Path.directory(l);
+ if(d.startsWith(Paths.translationsMain(''))) {
+ d = (d.replace(Paths.translationsMain(''), "").split("/"))[0];
+ if(!translations.contains(d)) translations.push(d);
+ }
+ }
+ #end
+ return translations;
+ }
+
+ /**
+ * Returns a map of translations based on its main.xml.
+ */
+ public static function loadLanguage(name:String):Map
+ {
+ #if TRANSLATIONS_SUPPORT
+ var path:String = 'main.xml';
+ if (!Assets.exists(Paths.translations('$name/$path'), TEXT)) return [];
+
+ var xml:Access = null;
+ try xml = new Access(Xml.parse(Assets.getText(path)))
+ catch(e) {
+ FlxG.log.error('Error while parsing main.xml: ${Std.string(e)}');
+ throw new Exception('Error while parsing main.xml: ${Std.string(e)}');
+ }
+ if (xml == null) return [];
+ if (!xml.hasNode.translations) {
+ FlxG.log.warn("A translation xml file requires a translations root element.");
+ return [];
+ }
+
+ var leMap:Map = [];
+ for(node in xml.node.translations.elements) {
+ switch(node.name) {
+ case "trans":
+ if (!node.has.id) {
+ FlxG.log.warn("A translation node requires an ID attribute.");
+ continue;
+ }
+ if (!node.has.string) {
+ FlxG.log.warn("A translation node requires a string attribute.");
+ continue;
+ }
+ leMap.set(node.att.id, node.att.string);
+ }
+ }
+ return leMap;
+ #else
+ return [];
+ #end
+ }
+}
\ No newline at end of file
diff --git a/source/funkin/options/Options.hx b/source/funkin/options/Options.hx
index a9bf72ac..79eda33f 100644
--- a/source/funkin/options/Options.hx
+++ b/source/funkin/options/Options.hx
@@ -35,6 +35,7 @@ class Options
public static var songOffset:Float = 0;
public static var framerate:Int = 120;
public static var gpuOnlyBitmaps:Bool = #if (mac || web) false #else true #end; // causes issues on mac and web
+ public static var language = funkin.backend.utils.TranslationsUtil.DEFAULT_LANGUAGE;
public static var lastLoadedMod:String = null;