Format Strings (WIP)

This commit is contained in:
NeeEoo
2024-04-03 01:16:53 +02:00
parent 9d3f31b7f0
commit 3ad2dd1c6b
+108 -66
View File
@@ -12,113 +12,155 @@ import haxe.Exception;
*/
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`.
*
* Using this class function it'll never be `null`.
*/
public static var transMap(default, null):Map<String, String> = [];
public static var transMap(default, null):Map<String, String> = [];
/**
/**
* The default language used inside of the source code.
*/
public static var DEFAULT_LANGUAGE:String = 'en/English'; // no inline so psycopathic mods can edit it - Nex
public static var DEFAULT_LANGUAGE:String = 'en/English'; // no inline so psycopathic mods can edit it - Nex
/**
/**
* Returns the current language.
*/
inline public static function get_curLanguage():String
return Options.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;
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 != [];
inline public static function is_anyTransLoaded():Bool
return transMap != [];
/**
/**
* Changes the translations map.
*
* If `name` is `null`, it's gonna use the current language.
*
* If `name` is `null`, it's gonna use the current language.
*/
public static function setTransl(?name:String)
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`.
*
* If `id` is `null` then it's gonna search using `defString`.
*/
public static function checkTransl(defString:String, ?id:String):String
{
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);
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<String>
{
var translations:Array<String> = [];
public static function translList():Array<String>
{
var translations:Array<String> = [];
#if TRANSLATIONS_SUPPORT
var main:String = Paths.translationsMain('');
for(l in Paths.assetsTree.getFolders(main)) for(f in Paths.assetsTree.getFiles(main + l))
for(l in Paths.assetsTree.getFolders(main)) for(f in Paths.assetsTree.getFiles(main + l))
if(Path.extension('$l/$f') == "xml") translations.push(Path.withoutExtension('$l/$f'));
#end
return translations;
}
return translations;
}
/**
/**
* Returns a map of translations based on its XML.
*/
public static function loadLanguage(name:String):Map<String, String>
{
public static function loadLanguage(name:String):Map<String, String>
{
#if TRANSLATIONS_SUPPORT
var path:String = Paths.translationsMain(name);
var path:String = Paths.translationsMain(name);
if(!path.endsWith(".xml")) path += ".xml";
if (!Assets.exists(path, TEXT)) return [];
if (!Assets.exists(path, TEXT)) return [];
var xml:Access = null;
try xml = new Access(Xml.parse(Assets.getText(path)))
catch(e) {
var xml:Access = null;
try xml = new Access(Xml.parse(Assets.getText(path)))
catch(e) {
var msg:String = 'Error while parsing ${Path.withoutDirectory(name)}.xml: ${Std.string(e)}';
FlxG.log.error(msg);
throw new Exception(msg);
}
if (xml == null) return [];
if (!xml.hasNode.translations) {
FlxG.log.warn("A translation xml file requires a translations root element.");
return [];
}
FlxG.log.error(msg);
throw new Exception(msg);
}
if (xml == null) return [];
if (!xml.hasNode.translations) {
FlxG.log.warn("A translation xml file requires a translations root element.");
return [];
}
var leMap:Map<String, String> = [];
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);
}
}
var leMap:Map<String, String> = [];
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
}
}
}
/*
input:Hello {1}, how are you, {0}
strings: ["Hello ", ", how are you, ", ""]
indexes: [1, 0]
strings[0] + values[indexes[0]] + strings[1] + values[indexes[1]] + strings[2]
*/
class FormatInfo {
public var strings:Array<String> = [];
public var indexes:Array<Int> = [];
public function new(str:String) {
var i = 0;
while(i < str.length) {
var fi = str.indexOf("{", i); // search from the start of i
if(fi == -1) {
strings.push(str.substring(i));
break;
}
var fe = str.indexOf("}", fi);
strings.push(str.substring(i, fi));
indexes.push(Std.parseInt(str.substring(fi+1, fe)));
i = fe + 1;
}
}
public function format(values:Array<Dynamic>) {
// todo
var string:Array<Dynamic> = strings.copy();
for(i => v in strings) {
string.insert(i*2+1, Std.string(values[indexes[i]]));
}
return string.join("");
}
}