aboutsummaryrefslogtreecommitdiff
path: root/game/src/LocaleButton.gd
diff options
context:
space:
mode:
Diffstat (limited to 'game/src/LocaleButton.gd')
-rw-r--r--game/src/LocaleButton.gd60
1 files changed, 35 insertions, 25 deletions
diff --git a/game/src/LocaleButton.gd b/game/src/LocaleButton.gd
index c3c6925..2b717a4 100644
--- a/game/src/LocaleButton.gd
+++ b/game/src/LocaleButton.gd
@@ -1,40 +1,49 @@
extends OptionButton
-const section_name : String = "Localization"
-const setting_name : String = "Locale"
+const section_name : String = "localisation"
+const setting_name : String = "locale"
-var _locales_country_rename : Dictionary
-var _locales_list : Array[String]
+var _default_locale_index : int
func _ready():
- _locales_country_rename = ProjectSettings.get_setting("internationalization/locale/country_short_name", {})
+ var locales_country_rename : Dictionary = ProjectSettings.get_setting("internationalization/locale/country_short_name", {})
- _locales_list = [TranslationServer.get_locale()]
- _locales_list.append_array(TranslationServer.get_loaded_locales())
+ var locales_list = TranslationServer.get_loaded_locales()
+ var default_locale := Events.Localisation.get_default_locale()
+ if default_locale not in locales_list:
+ locales_list.push_back(default_locale)
- for locale in _locales_list:
+ for locale in locales_list:
+ # locale_name consists of a compulsory language name and optional script
+ # and country names, in the format: "<language>[ (script)][, country]"
var locale_name := TranslationServer.get_locale_name(locale)
- var locale_first_part := locale_name.get_slice(", ", 0)
- var locale_second_part := locale_name.substr(locale_first_part.length() + 2)
- if locale_second_part in _locales_country_rename:
- locale_second_part = _locales_country_rename[locale_second_part]
+ var comma_idx := locale_name.find(", ")
+ if comma_idx != -1:
+ var locale_country_name := locale_name.substr(comma_idx + 2)
+ locale_country_name = locales_country_rename.get(locale_country_name, "")
+ if not locale_country_name.is_empty():
+ locale_name = locale_name.left(comma_idx + 2) + locale_country_name
- add_item("%s, %s" % [locale_first_part, locale_second_part])
+ add_item(locale_name)
+ set_item_metadata(item_count - 1, locale)
+
+ if locale == default_locale:
+ _default_locale_index = item_count - 1
Events.Options.load_settings.connect(load_setting)
Events.Options.save_settings.connect(save_setting)
-
-func _notification(what):
+
+func _notification(what : int):
match what:
NOTIFICATION_TRANSLATION_CHANGED:
_select_locale_by_string(TranslationServer.get_locale())
func _valid_index(index : int) -> bool:
- return 0 <= index and index < _locales_list.size()
+ return 0 <= index and index < item_count
func load_setting(file : ConfigFile) -> void:
if file == null: return
- var load_value = file.get_value(section_name, setting_name, TranslationServer.get_locale())
+ var load_value = file.get_value(section_name, setting_name, Events.Localisation.get_default_locale())
match typeof(load_value):
TYPE_STRING, TYPE_STRING_NAME:
if _select_locale_by_string(load_value as String):
@@ -42,28 +51,29 @@ func load_setting(file : ConfigFile) -> void:
return
push_error("Setting value '%s' invalid for setting [%s] %s" % [load_value, section_name, setting_name])
reset_setting()
-
+
func _select_locale_by_string(locale : String) -> bool:
- var locale_index := _locales_list.find(locale)
- if locale_index != -1:
- selected = locale_index
- return true
+ for idx in item_count:
+ if get_item_metadata(idx) == locale:
+ selected = idx
+ return true
+ selected = _default_locale_index
return false
# REQUIREMENTS:
# * UIFUN-74
func save_setting(file : ConfigFile) -> void:
if file == null: return
- file.set_value(section_name, setting_name, _locales_list[selected])
+ file.set_value(section_name, setting_name, get_item_metadata(selected))
func reset_setting() -> void:
- selected = _locales_list.find(TranslationServer.get_locale())
+ _select_locale_by_string(TranslationServer.get_locale())
# REQUIREMENTS:
# * SS-58
func _on_item_selected(index : int) -> void:
if _valid_index(index):
- TranslationServer.set_locale(_locales_list[index])
+ TranslationServer.set_locale(get_item_metadata(index))
Events.Options.save_settings_to_file.call_deferred()
else:
push_error("Invalid LocaleButton index: %d" % index)