Is possible to add localization files programmatically?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By sante

The real question is this one: I would like to create “expansion pack” for my game. My game is localized. So the problem is how to add new (unknowed) translations files to my game after I’ve loaded them from a PCK file. I’ve no “expansion packs” for now, so I’ve no Idea about how many translation files I need and their content.
Doing a couple of tests, I’ve found is possible to add more than one file for each language to translate it. However I don’t know how to simulate, programmatically, the act of adding these files to the project, like when they are added from “project-setting” dialog.
Someone know the answer?!

The funcion is simply ProjectSettings.load_resource_pack("res://mod.pck"). You can find more info here

Or the question is about loading menu for user? Then use Buttons and FileDialog.

USBashka | 2022-07-12 18:41

I would like to know how to add translations files (I mean the ones with “.translation” extensions) to a project by code.
PCK files can be loaded by ProjectSettings.load_resource_pack(), as you say, but after then I need to add new translations to the one’s of original project. How to do it?

sante | 2022-07-12 21:32

:bust_in_silhouette: Reply From: ominocutherium

After loading the pack file, you also want to load the Translation resource into TranslationServer singleton.

TranslationServer.add_translation(load("path_to_translation_file.translation"))

Great! Thank you very much!

sante | 2022-07-15 16:22

Don’t works… Simply add_translation require a Translation object, but loading a translation file don’t give back one of that type…
At the end, I did it with this code:

func _load_translations():
var dir: Directory = Directory.new();	
var translation_array: Array = [];

if dir.open(TRANSLATION_DIRECTORY) == OK:
	var _unused = dir.list_dir_begin();
	var file_name = dir.get_next();
	while file_name != "":
		if (".csv" in file_name) and !(".import" in file_name):
			var file: File  = File.new();
			if file.open(TRANSLATION_DIRECTORY + file_name, File.READ) == OK:
				while file.get_position() < file.get_len():
					translation_array.append(file.get_csv_line());
		file_name = dir.get_next();
		
	for i in range(1, translation_array.size()):
		for y in range (1, translation_array[i].size()):				
			var translation: Translation = Translation.new();
			
			var str_locale: String = translation_array[0][y];
			var str_origin: String = translation_array[i][0];
			var str_destination: String = translation_array[i][y];
			
			translation.locale = str_locale;
			translation.add_message(str_origin, str_destination);
			TranslationServer.add_translation(translation);
pass

Now I can load how many *.csv files from packages I would…

sante | 2022-07-16 21:02