extends OptionButton


@export var languages: Dictionary[String, String] = {
	"English (English)": "en",
	"Deutsch (German)": "de"
}

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	for i: Variant in languages:
		add_item(i as String)
	
	select(SettingsManager.get_setting(&"misc", &"language"))
	
	if selected == -1:
		match OS.get_locale_language():
			"en": selected = 0
			"de": selected = 1
	
	item_selected.connect(option_selected)
	option_selected(selected)


func option_selected(index: int) -> void:
	var index_string: String = get_item_text(index)
	
	if languages.has(index_string):
		if languages[index_string] is not String:
			return
		
		SettingsManager.set_language(index)
		TranslationServer.set_locale(languages[index_string])
