aboutsummaryrefslogtreecommitdiff
path: root/game/src/LocaleButton.gd
blob: a8220de0c3386df9699a7c785d387ca62b8bda52 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
extends OptionButton

const section_name : String = "Localization"
const setting_name : String = "Locale"

var _locales_country_rename : Dictionary
var _locales_list : Array[String]

func _ready():
   _locales_country_rename = ProjectSettings.get_setting("internationalization/locale/country_short_name", {})

   _locales_list = [TranslationServer.get_locale()]
   _locales_list.append_array(TranslationServer.get_loaded_locales())

   for locale in _locales_list:
      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]

      add_item("%s, %s" % [locale_first_part, locale_second_part])

   Events.Options.load_settings.connect(load_setting)
   Events.Options.save_settings.connect(save_setting)

func _valid_index(index : int) -> bool:
   return 0 <= index and index < _locales_list.size()

func load_setting(file : ConfigFile) -> void:
   if file == null: return
   var load_value = file.get_value(section_name, setting_name, TranslationServer.get_locale())
   match typeof(load_value):
      TYPE_STRING, TYPE_STRING_NAME:
         var locale_index := _locales_list.find(load_value as String)
         if locale_index != -1:
            selected = locale_index
            return
   push_error("Setting value '%s' invalid for setting [%s] %s" % [load_value, section_name, setting_name])
   reset_setting()

# REQUIREMENTS:
# * UIFUN-74
func save_setting(file : ConfigFile) -> void:
   if file == null: return
   file.set_value(section_name, setting_name, _locales_list[selected])

func reset_setting() -> void:
   selected = _locales_list.find(TranslationServer.get_locale())

# REQUIREMENTS:
# * SS-58
func _on_item_selected(index : int) -> void:
   if _valid_index(index):
      TranslationServer.set_locale(_locales_list[index])
      Events.Options.save_settings_to_file.call_deferred()
   else:
      push_error("Invalid LocaleButton index: %d" % index)
      reset_setting()