How to load CSV translation with code?

Godot Version

4.2.2 Stable Mono

Question

I’m now working on a custom localization system for players to make localization by themselves
I want to load csv file
But I don’t know why, the code can’t work properly
After I executed GetTranslationObject() and got the object,
GetMessageList()only gave me a empty string[ ]
But GetMessage() still works properly
And EraseMessage() & AddMessage() didn’t work
I tried to find a plugin for loading CSV files but I found nothing

Part of the original localization CSV file:

keys,en,zh_CN,zh_TW,ja
locYes,Yes,是,是,はい

Custom CSV file:

keys,en,zh_CN,zh_TW,ja
locYes,No,否,否,いいえ

Part of my code:

var csvm="res://Locales/"+i+".csv";
if (i != "" && FileAccess.FileExists(csvm))
{
	var fff=FileAccess.Open(csvm,FileAccess.ModeFlags.Read);
	var errr=FileAccess.GetOpenError();
	if (errr == Error.Ok)
	{
		locale_loaded = locale_loaded.Append(i).ToArray<string>();
		var langs=fff.GetCsvLine();
		var lines=fff.GetAsText().Split("\n");
		foreach (var lang in langs)
		{
			if (lang != "keys")
			{
				fff.Close();
				fff=FileAccess.Open(csvm,FileAccess.ModeFlags.Read);
				var translation=TranslationServer.GetTranslationObject(lang);
				if (translation == null)
				{
					translation=new Translation();
					translation.Locale=lang;
				}
				else
				{
					TranslationServer.RemoveTranslation(translation);
				}
				foreach (var b in lines)
				{
					var current=fff.GetCsvLine();
					if (System.Array.IndexOf(lines,b) != 0)
					{
						if (b !="")
						{
							GD.Print("in file:"+current[0],current[System.Array.IndexOf(langs,lang)].Replace("\\n","\n"));
							GD.Print("intro:"+translation.GetMessage(current[0]));
							GD.Print("list:");
							GD.Print(translation.GetMessageList());
							translation.EraseMessage(current[0]);
							GD.Print("erased:"+translation.GetMessage(current[0]));
							translation.AddMessage(current[0],current[System.Array.IndexOf(langs,lang)].Replace("\\n","\n"));
							GD.Print("added:"+translation.GetMessage(current[0]));
						}
					}
				}
				TranslationServer.AddTranslation(translation);
			}
		}
		fff.Close();
	}
}

Execute result:

in file:locYesNo
intro:Yes
list:

erased:Yes
added:Yes
in file:locYes否
intro:是
list:

erased:是
added:是
in file:locYes否
intro:是
list:

erased:是
added:是
in file:locYesいいえ
intro:はい
list:

erased:はい
added:はい

anyone help :sob: :sob:

Godot have build in localization with csv

Yes I know, I’m currently using it
I want to load CSV file when game is running
So that player can make their custom localization

What i Found


,

,

your code is hard to understand, what is b? b and var don’t saying anything.

is one of traps of strings.
use:

		foreach (var item in myCollection)
		{
			if (b.Equals(""))
				continue;
			GD.Print("Do My Stuff");
		}
1 Like

uhh actually, the codes’ works, it can read CSV and get the key and translation correctly
The real problem is, EraseMessage can’t erase message, and GetMessageList only gets a empty array, GetMessage always be original translation
It can’t be override

ok thx for advice, I will change it

1 Like