Is there a way to get all possible keys from a CSV translation file ?

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

Hi,

I have a CSV file containing a certain number of keys associated to a dialog in game:

For example:

keys,en,fr
TUTO_1,First line in English, First line in french
TUTO_2,Second line in English, Second line in french 

Right now I load the dialog as follows in my code:

var dialog = [
	tr("TUTO_1"),
	tr("TUTO_2")	
]

If later I add TUTO_3 in the file I would like to fill this list aromatically. So I was wondering is it possible to get all possible keys from a CSV translation file so I can iterate over them ?

I’m thinking of something like this:

var dialog = []
for key in tr.get_keys(): 
    dialog .append(tr(key))

Any suggestions on how to proceed ?

:bust_in_silhouette: Reply From: clemens.tolboom

I did not know how translations worked :-p

I came up with this code … uncomment save to create the CSV first if you want to create the file first.

extends Control


var CSV:PoolStringArray = [
	"keys,en,fr",
	"TUTO_1,First line in English, First line in french",
	"TUTO_2,Second line in English, Second line in french" 
]

func _ready():
	#save(CSV.join('\n'))
	var data = load_csv()
	var header
	var keys:Array = []
	for d in data:
		if header == null:
			header = d
		else:
			keys.push_back(d[0])
	print(keys)

func save(content):
	var file = File.new()
	file.open("res://98940/98940.csv", File.WRITE)
	file.store_string(content)
	file.close()

func load_csv() -> Array:
	var data:Array = []
	var file = File.new()
	file.open("res://98940/98940.csv", File.READ)
	while not file.eof_reached():
		var line = file.get_csv_line()
		data.push_back(line)
	file.close()
	return data