Skip to content

Commit d100bf4

Browse files
authored
Merge pull request #484 from lichcay/main
Added language support
2 parents e069b86 + ff5b78e commit d100bf4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+330
-286
lines changed

assets/texts/texts.csv.import

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ uid="uid://b4mco2dr478ww"
66

77
[deps]
88

9-
files=["res://assets/texts/texts.en.translation", "res://assets/texts/texts.cn.translation"]
9+
files=["res://assets/texts/texts.en.translation", "res://assets/texts/texts.zh.translation"]
1010

1111
source_file="res://assets/texts/texts.csv"
12-
dest_files=["res://assets/texts/texts.en.translation", "res://assets/texts/texts.cn.translation"]
12+
dest_files=["res://assets/texts/texts.en.translation", "res://assets/texts/texts.zh.translation"]
1313

1414
[params]
1515

project.godot

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ ctrl={
188188

189189
[internationalization]
190190

191-
locale/translations=PackedStringArray("res://assets/texts/texts.cn.translation", "res://assets/texts/texts.en.translation")
191+
locale/translations=PackedStringArray("res://assets/texts/texts.zh.translation", "res://assets/texts/texts.en.translation")
192192

193193
[physics]
194194

src/enums/language.gd

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class_name Language extends Node
2+
3+
enum enm {
4+
ENGLISH,
5+
CHINESE,
6+
}
7+
8+
static var language_map: Dictionary = {
9+
Language.enm.ENGLISH: "en",
10+
Language.enm.CHINESE: "zh",
11+
}
12+
13+
static var language_option_map: Dictionary = {
14+
Language.enm.ENGLISH: 0,
15+
Language.enm.CHINESE: 1,
16+
}
17+
18+
static var language_option_locale_map: Dictionary = {
19+
0:"en",
20+
1:"zh",
21+
}
22+
23+
static func get_locale_from_enum (lan: Language.enm) -> String:
24+
return language_map[lan]
25+
26+
static func get_enum_from_option (opt: int) -> Language.enm:
27+
return language_option_map[opt]
28+
29+
static func get_option_from_locale (lan: String) -> int:
30+
return language_option_locale_map.find_key(lan)
31+
32+
static func get_locale_from_option (opt: int) -> String:
33+
return language_option_locale_map[opt]

src/singletons/settings.gd

+6-4
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ const COMBAT_LOG_X: String = "combat_log_x"
2727
const COMBAT_LOG_Y: String = "combat_log_y"
2828
const INTERFACE_SIZE: String = "interface_size"
2929
const INTERFACE_SIZE_DICT: Dictionary = {
30-
"Small": 0.75,
31-
"Medium": 1.0,
32-
"Large": 1.25
30+
"SETTINGS_INTERFACE_SMALL": 0.75,
31+
"SETTINGS_INTERFACE_MEDIUM": 1.0,
32+
"SETTINGS_INTERFACE_LARGE": 1.25
3333
}
3434
const SHOW_TUTORIAL_ON_START: String = "show_tutorial_on_start"
3535
const CACHED_GAME_DIFFICULTY: String = "CACHED_GAME_DIFFICULTY"
@@ -41,6 +41,7 @@ const WISDOM_UPGRADES_CACHED: String = "WISDOM_UPGRADES_CACHED"
4141
const DISPLAY_MODE: String = "DISPLAY_MODE"
4242
const MISSION_STATUS: String = "MISSION_STATUS"
4343
const SHOWED_ONE_TIME_HELP_POPUP: String = "SHOWED_ONE_TIME_HELP_POPUP"
44+
const LANGUAGE: String = "LANGUAGE"
4445

4546

4647
var _cache: Dictionary = {}
@@ -61,7 +62,7 @@ var _default_value_map: Dictionary = {
6162
SHOW_COMBAT_LOG: false,
6263
COMBAT_LOG_X: 20.0,
6364
COMBAT_LOG_Y: 600.0,
64-
INTERFACE_SIZE: "Medium",
65+
INTERFACE_SIZE: "SETTINGS_INTERFACE_MEDIUM",
6566
SHOW_TUTORIAL_ON_START: true,
6667
CACHED_GAME_DIFFICULTY: "beginner",
6768
CACHED_GAME_MODE: "random_with_upgrades",
@@ -72,6 +73,7 @@ var _default_value_map: Dictionary = {
7273
DISPLAY_MODE: DisplayMode.enm.FULLSCREEN as float,
7374
MISSION_STATUS: {},
7475
SHOWED_ONE_TIME_HELP_POPUP: false,
76+
LANGUAGE: "en"
7577
}
7678

7779

src/ui/buttons/menu_expanding_button.tscn

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
[ext_resource type="Script" path="res://src/ui/buttons/menu_expanding_button.gd" id="3_cmnlx"]
66
[ext_resource type="Texture2D" uid="uid://dex1xjcdq1fv7" path="res://resources/ui_textures/rect_container_m.tres" id="4_3y845"]
77

8-
98
[sub_resource type="StyleBoxTexture" id="StyleBoxTexture_ebngv"]
109
content_margin_left = 10.0
1110
content_margin_right = 50.0

src/ui/game_menu/game_menu.tscn

+4-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
[ext_resource type="PackedScene" uid="uid://bbrryq35rxvkv" path="res://src/ui/game_menu/help_menu.tscn" id="3_vf4xx"]
66
[ext_resource type="PackedScene" uid="uid://cx86ea3loo8a2" path="res://src/ui/game_menu/settings_menu.tscn" id="5_y1l7h"]
77

8-
98
[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_6c4dp"]
109

1110
[node name="GameMenu" type="VBoxContainer" node_paths=PackedStringArray("_tab_container")]
@@ -32,22 +31,22 @@ theme_override_constants/separation = 5
3231
[node name="ContinueButton" type="Button" parent="TabContainer/MainTab/VBoxContainer"]
3332
layout_mode = 2
3433
focus_mode = 0
35-
text = "Continue"
34+
text = "CONTINUE_BUTTON"
3635

3736
[node name="HelpButton" type="Button" parent="TabContainer/MainTab/VBoxContainer"]
3837
layout_mode = 2
3938
focus_mode = 0
40-
text = "Help"
39+
text = "HELP_BUTTON"
4140

4241
[node name="SettingsButton" type="Button" parent="TabContainer/MainTab/VBoxContainer"]
4342
layout_mode = 2
4443
focus_mode = 0
45-
text = "Settings"
44+
text = "SETTINGS_BUTTON"
4645

4746
[node name="QuitButton" type="Button" parent="TabContainer/MainTab/VBoxContainer"]
4847
layout_mode = 2
4948
focus_mode = 0
50-
text = "Quit To Menu"
49+
text = "QUIT_TO_MENU_BUTTON"
5150

5251
[node name="HintsMenu" parent="TabContainer" instance=ExtResource("3_vf4xx")]
5352
visible = false

src/ui/game_menu/help_menu.tscn

+8-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
[ext_resource type="Script" path="res://src/ui/game_menu/help_menu.gd" id="2_jpay6"]
55
[ext_resource type="PackedScene" uid="uid://dqb37g0dwa4o2" path="res://src/ui/game_menu/help_menu_tab.tscn" id="3_iygdm"]
66

7-
87
[node name="HelpMenu" type="TabContainer"]
98
custom_minimum_size = Vector2(1100, 700)
109
offset_right = 900.0
@@ -14,48 +13,48 @@ theme_type_variation = &"MainPanelContainer"
1413
current_tab = 0
1514
script = ExtResource("2_jpay6")
1615

17-
[node name="Basics" parent="." instance=ExtResource("3_iygdm")]
16+
[node name="HELP_MENU_BASICS" parent="." instance=ExtResource("3_iygdm")]
1817
layout_mode = 2
1918
csv_path = "res://data/hints/basics.csv"
2019
metadata/_tab_index = 0
2120

22-
[node name="Tutorial" parent="." instance=ExtResource("3_iygdm")]
21+
[node name="HELP_MENU_TUTORIAL" parent="." instance=ExtResource("3_iygdm")]
2322
visible = false
2423
layout_mode = 2
2524
csv_path = "res://data/hints/tutorial.csv"
2625
metadata/_tab_index = 1
2726

28-
[node name="Chat Commands" parent="." instance=ExtResource("3_iygdm")]
27+
[node name="HELP_MENU_CHAT_COMMANDS" parent="." instance=ExtResource("3_iygdm")]
2928
visible = false
3029
layout_mode = 2
3130
csv_path = "res://data/hints/chat_commands.csv"
3231
metadata/_tab_index = 2
3332

34-
[node name="Towers" parent="." instance=ExtResource("3_iygdm")]
33+
[node name="HELP_MENU_TOWERS" parent="." instance=ExtResource("3_iygdm")]
3534
visible = false
3635
layout_mode = 2
3736
csv_path = "res://data/hints/towers.csv"
3837
metadata/_tab_index = 3
3938

40-
[node name="Items" parent="." instance=ExtResource("3_iygdm")]
39+
[node name="HELP_MENU_ITEMS" parent="." instance=ExtResource("3_iygdm")]
4140
visible = false
4241
layout_mode = 2
4342
csv_path = "res://data/hints/items.csv"
4443
metadata/_tab_index = 4
4544

46-
[node name="Creeps" parent="." instance=ExtResource("3_iygdm")]
45+
[node name="HELP_MENU_CREEPS" parent="." instance=ExtResource("3_iygdm")]
4746
visible = false
4847
layout_mode = 2
4948
csv_path = "res://data/hints/creeps.csv"
5049
metadata/_tab_index = 5
5150

52-
[node name="Creep Specials" parent="." instance=ExtResource("3_iygdm")]
51+
[node name="HELP_MENU_CREEP_SPECIALS" parent="." instance=ExtResource("3_iygdm")]
5352
visible = false
5453
layout_mode = 2
5554
csv_path = "res://data/hints/creep_specials.csv"
5655
metadata/_tab_index = 6
5756

58-
[node name="Advanced" parent="." instance=ExtResource("3_iygdm")]
57+
[node name="HELP_MENU_ADVANCED" parent="." instance=ExtResource("3_iygdm")]
5958
visible = false
6059
layout_mode = 2
6160
csv_path = "res://data/hints/advanced.csv"
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
extends OptionButton
2+
3+
4+
# Called when the language option button init to set default selection
5+
func _ready() -> void:
6+
var locale = OS.get_locale_language()
7+
var index = Language.get_option_from_locale(locale)
8+
selected = index
9+
10+
func _on_item_selected(index: int) -> void:
11+
TranslationServer.set_locale(Language.get_locale_from_option(index))

src/ui/game_menu/settings_menu.gd

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ func _ready():
3636
_setting_to_combo_map = {
3737
Settings.DISPLAY_MODE: _display_mode_combo,
3838
}
39+
40+
#init TranslationServer locale based on OS locale
41+
var preferred_language = OS.get_locale_language()
42+
TranslationServer.set_locale(preferred_language)
43+
3944
for setting in _setting_to_combo_map.keys():
4045
var combo: OptionButton = _setting_to_combo_map[setting]
4146
combo.item_selected.connect(_on_combo_changed)

0 commit comments

Comments
 (0)