0 votes

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 ?

Godot version 3.2.3
in Engine by (33 points)

1 Answer

+1 vote
Best answer

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
by (640 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.