From fca746fa499720cba3044faefd3b6d9dfcae7aeb Mon Sep 17 00:00:00 2001 From: Jowan-Spooner Date: Thu, 25 Apr 2024 16:17:02 +0200 Subject: [PATCH 1/2] Initial simplification --- .../Editor/Settings/settings_translation.gd | 11 ++- .../Layer_Glossary/glossary_popup_layer.gd | 82 ++++--------------- .../Modules/Glossary/glossary_resource.gd | 1 - .../Modules/Glossary/subsystem_glossary.gd | 68 ++++++++++++++- 4 files changed, 83 insertions(+), 79 deletions(-) diff --git a/addons/dialogic/Editor/Settings/settings_translation.gd b/addons/dialogic/Editor/Settings/settings_translation.gd index 60b0aa5b2..11873e0f2 100644 --- a/addons/dialogic/Editor/Settings/settings_translation.gd +++ b/addons/dialogic/Editor/Settings/settings_translation.gd @@ -380,7 +380,7 @@ func update_csv_files() -> void: %StatusMessage.text = status_message.format(status_message_args) ProjectSettings.set_setting(_USED_LOCALES_SETTING, _unique_locales) - get_locales("") + ## Iterates over all character resource files and creates or updates CSV files ## that contain the translations for character properties. @@ -460,10 +460,10 @@ func collect_translations() -> void: if not file_path in all_translation_files: all_translation_files.append(file_path) - var path_without_suffix := file_path.trim_suffix('.translation') - var path_parts := path_without_suffix.split(".") - var locale_part := path_parts[-1] - _collect_locale(locale_part) + + var path_without_suffix := file_path.trim_suffix('.translation') + var locale_part := path_without_suffix.split(".")[-1] + _collect_locale(locale_part) var valid_translation_files := PackedStringArray(all_translation_files) @@ -517,7 +517,6 @@ func erase_translations() -> void: var files: PackedStringArray = ProjectSettings.get_setting('internationalization/locale/translations', []) var translation_files := Array(files) ProjectSettings.set_setting(_USED_LOCALES_SETTING, []) - get_locales("") var deleted_csv_files := 0 var deleted_translation_files := 0 diff --git a/addons/dialogic/Modules/DefaultLayoutParts/Layer_Glossary/glossary_popup_layer.gd b/addons/dialogic/Modules/DefaultLayoutParts/Layer_Glossary/glossary_popup_layer.gd index 7b129864a..1c871e319 100644 --- a/addons/dialogic/Modules/DefaultLayoutParts/Layer_Glossary/glossary_popup_layer.gd +++ b/addons/dialogic/Modules/DefaultLayoutParts/Layer_Glossary/glossary_popup_layer.gd @@ -76,87 +76,32 @@ func _ready() -> void: _error = text_system.connect(&'meta_hover_ended', _on_dialogic_display_dialog_text_meta_hover_ended) -func _try_translate(tr_base: String, property: StringName, fallback_entry: Dictionary) -> String: - var tr_key := tr_base.path_join(property) - var tr_value := tr(tr_key) - - if tr_key == tr_value: - tr_value = fallback_entry.get(property, "") - - return tr_value - ## Method that shows the bubble and fills in the info func _on_dialogic_display_dialog_text_meta_hover_started(meta: String) -> void: - var dialogic := DialogicUtil.autoload() - var glossary: DialogicGlossary = dialogic.Glossary.find_glossary(meta) - - var entry_title := "" - var entry_text := "" - var entry_extra := "" - var entry_color: Variant = null + var entry_info := DialogicUtil.autoload().Glossary.get_entry(meta) - var title_color := title_custom_color - var text_color := text_custom_color - var extra_color := extra_custom_color - - if glossary == null: + if entry_info.is_empty(): return - var is_translation_enabled: bool = ProjectSettings.get_setting('dialogic/translation/enabled', false) - - if not is_translation_enabled or glossary._translation_id.is_empty(): - var entry := glossary.get_entry(meta) - - if entry.is_empty(): - return - - var variable_system := dialogic.VAR - entry_title = variable_system.parse_variables(entry.get("title", "")) - entry_text = variable_system.parse_variables(entry.get("text", "")) - entry_extra = variable_system.parse_variables(entry.get("extra", "")) - entry_color = entry.get("color") - - else: - var translation_key: String = glossary._translation_keys.get(meta) - var last_slash := translation_key.rfind('/') - - if last_slash == MISSING_INDEX: - return - - var tr_base := translation_key.substr(0, last_slash) - - var entry := glossary.get_entry(meta) - entry_color = entry.get('color') - - entry_title = _try_translate(tr_base, "title", entry) - entry_text = _try_translate(tr_base, "text", entry) - entry_extra = _try_translate(tr_base, "extra", entry) - - if not entry_color == null: - title_color = entry_color - text_color = entry_color - extra_color = entry_color - get_pointer().show() - get_title().text = entry_title - get_text().text = entry_text + get_title().text = entry_info.title + get_text().text = entry_info.text get_text().text = ['', '[center]', '[right]'][text_alignment] + get_text().text - get_extra().text = entry_extra + get_extra().text = entry_info.extra get_extra().text = ['', '[center]', '[right]'][extra_alignment] + get_extra().text get_pointer().global_position = get_pointer().get_global_mouse_position() - if title_color_mode == TextColorModes.ENTRY: - get_title().add_theme_color_override(&"font_color", title_color) + get_title().add_theme_color_override(&"font_color", entry_info.color) if text_color_mode == TextColorModes.ENTRY: - get_text().add_theme_color_override(&"default_color", text_color) + get_text().add_theme_color_override(&"default_color", entry_info.color) if extra_color_mode == TextColorModes.ENTRY: - get_extra().add_theme_color_override(&"default_color", extra_color) + get_extra().add_theme_color_override(&"default_color", entry_info.color) match box_modulate_mode: ModulateModes.ENTRY_COLOR_ON_BOX: - get_panel().self_modulate = title_color - get_panel_point().self_modulate = title_color + get_panel().self_modulate = entry_info.color + get_panel_point().self_modulate = entry_info.color ## Method that keeps the bubble at mouse position when visible @@ -206,16 +151,17 @@ func _apply_export_overrides() -> void: # Apply text colors + # this applies Global or Custom colors, entry colors are applied on hover var controls: Array[Control] = [get_title(), get_text(), get_extra()] - var global_settings: Array[StringName] = [&'font_color', &'default_color', &'default_color'] + var settings: Array[StringName] = [&'font_color', &'default_color', &'default_color'] var color_modes: Array[TextColorModes] = [title_color_mode, text_color_mode, extra_color_mode] var custom_colors: PackedColorArray = [title_custom_color, text_custom_color, extra_custom_color] for i : int in len(controls): match color_modes[i]: TextColorModes.GLOBAL: - controls[i].add_theme_color_override(global_settings[i], get_global_setting(&'font_color', custom_colors[i]) as Color) + controls[i].add_theme_color_override(settings[i], get_global_setting(&'font_color', custom_colors[i]) as Color) TextColorModes.CUSTOM: - controls[i].add_theme_color_override(global_settings[i], custom_colors[i]) + controls[i].add_theme_color_override(settings[i], custom_colors[i]) # Apply box size var panel: PanelContainer = get_panel() diff --git a/addons/dialogic/Modules/Glossary/glossary_resource.gd b/addons/dialogic/Modules/Glossary/glossary_resource.gd index 9a9f43d0b..c59967d32 100644 --- a/addons/dialogic/Modules/Glossary/glossary_resource.gd +++ b/addons/dialogic/Modules/Glossary/glossary_resource.gd @@ -37,7 +37,6 @@ const REGEX_OPTION_PROPERTY := "regex_options" ## Ignored when entries are translated. const PRIVATE_PROPERTY_PREFIX := "_" -const _MISSING_ENTRY_INDEX := -1 ## Private ID assigned when this glossary is translated. @export var _translation_id: String = "" diff --git a/addons/dialogic/Modules/Glossary/subsystem_glossary.gd b/addons/dialogic/Modules/Glossary/subsystem_glossary.gd index 389069db9..c0e47dd1b 100644 --- a/addons/dialogic/Modules/Glossary/subsystem_glossary.gd +++ b/addons/dialogic/Modules/Glossary/subsystem_glossary.gd @@ -10,6 +10,8 @@ var enabled := true ## Any key in this dictionary will overwrite the color for any item with that name. var color_overrides := {} +const SETTING_DEFAULT_COLOR := 'dialogic/glossary/default_color' + #region STATE #################################################################################################### @@ -31,7 +33,7 @@ func parse_glossary(text: String) -> String: return text var def_case_sensitive: bool = ProjectSettings.get_setting('dialogic/glossary/default_case_sensitive', true) - var def_color: Color = ProjectSettings.get_setting('dialogic/glossary/default_color', Color.POWDER_BLUE) + var def_color: Color = ProjectSettings.get_setting(SETTING_DEFAULT_COLOR, Color.POWDER_BLUE) var regex := RegEx.new() for glossary: DialogicGlossary in glossaries: @@ -110,7 +112,65 @@ func find_glossary(entry_key: String) -> DialogicGlossary: if glossary.entries.has(entry_key): return glossary - if glossary.entries.has(entry_key): - return glossary - return null + + +## Returns the first match for a given entry key. +## If translation is available and enabled, it will be translated +func get_entry(entry_key: String) -> Dictionary: + var glossary: DialogicGlossary = dialogic.Glossary.find_glossary(entry_key) + + var result := { + "title": "", + "text": "", + "extra": "", + "color": Color.WHITE, + } + + if glossary == null: + return {} + + var is_translation_enabled: bool = ProjectSettings.get_setting('dialogic/translation/enabled', false) + + var entry := glossary.get_entry(entry_key) + + if entry.is_empty(): + return {} + + result.color = entry.get("color") + if result.color == null: + result.color = ProjectSettings.get_setting(SETTING_DEFAULT_COLOR, Color.POWDER_BLUE) + + if is_translation_enabled and not glossary._translation_id.is_empty(): + var translation_key: String = glossary._translation_keys.get(entry_key) + var last_slash := translation_key.rfind('/') + + if last_slash == -1: + return {} + + var tr_base := translation_key.substr(0, last_slash) + + result.title = _try_translate(tr_base, "title", entry) + result.text = _try_translate(tr_base, "text", entry) + result.extra = _try_translate(tr_base, "extra", entry) + else: + result.title = entry.get("title", "") + result.text = entry.get("text", "") + result.extra = entry.get("extra", "") + + ## PARSE TEXTS FOR VARIABLES + result.title = dialogic.VAR.parse_variables(result.title) + result.text = dialogic.VAR.parse_variables(result.text) + result.extra = dialogic.VAR.parse_variables(result.extra) + + return result + + +func _try_translate(tr_base: String, property: StringName, fallback_entry: Dictionary) -> String: + var tr_key := tr_base.path_join(property) + var tr_value := tr(tr_key) + + if tr_key == tr_value: + tr_value = fallback_entry.get(property, "") + + return tr_value From b184b8bfadc8a82c06f929c228bf53dd16cfe4f4 Mon Sep 17 00:00:00 2001 From: Jowan-Spooner Date: Fri, 26 Apr 2024 12:26:03 +0200 Subject: [PATCH 2/2] Minor cleanup --- .../Editor/Settings/settings_translation.gd | 13 ++++------- .../Modules/Glossary/glossary_resource.gd | 23 ++++++------------- .../Modules/Glossary/subsystem_glossary.gd | 14 +++++------ 3 files changed, 18 insertions(+), 32 deletions(-) diff --git a/addons/dialogic/Editor/Settings/settings_translation.gd b/addons/dialogic/Editor/Settings/settings_translation.gd index 11873e0f2..59419f0bc 100644 --- a/addons/dialogic/Editor/Settings/settings_translation.gd +++ b/addons/dialogic/Editor/Settings/settings_translation.gd @@ -185,7 +185,6 @@ func _handle_glossary_translation( translation_mode: TranslationModes, translation_folder_path: String, orig_locale: String) -> void: - var glossary_csv_path := "" var glossary_csv: DialogicCsvFile = null var glossary_paths: Array = ProjectSettings.get_setting('dialogic/glossary/glossary_files', []) @@ -207,6 +206,7 @@ func _handle_glossary_translation( var file_name := path_parts[-1] csv_name = "dialogic_" + file_name + '_translation.csv' + var glossary_csv_path := "" # Get glossary CSV file path. match save_location_mode: SaveLocationModes.INSIDE_TRANSLATION_FOLDER: @@ -228,13 +228,10 @@ func _handle_glossary_translation( glossary_csv.add_translation_keys_to_glossary(glossary) ResourceSaver.save(glossary) - match translation_mode: - TranslationModes.PER_PROJECT: - pass - - TranslationModes.PER_TIMELINE: - glossary_csv.update_csv_file_on_disk() - glossary_csv = null + #If per-file mode is used, save this csv and begin a new one + if translation_mode == TranslationModes.PER_TIMELINE: + glossary_csv.update_csv_file_on_disk() + glossary_csv = null # If a Per-Project glossary is still open, we need to save it. if glossary_csv != null: diff --git a/addons/dialogic/Modules/Glossary/glossary_resource.gd b/addons/dialogic/Modules/Glossary/glossary_resource.gd index c59967d32..edc1fcc55 100644 --- a/addons/dialogic/Modules/Glossary/glossary_resource.gd +++ b/addons/dialogic/Modules/Glossary/glossary_resource.gd @@ -48,9 +48,6 @@ const PRIVATE_PROPERTY_PREFIX := "_" @export var _translation_keys: Dictionary = {} -func __get_property_list() -> Array: - return [] - ## Removes an entry and all its aliases (alternative property) from ## the glossary. @@ -225,6 +222,8 @@ func get_set_regex_option(entry_key: String) -> String: return regex_option +#region ADD AND CLEAR TRANSLATION KEYS + ## This is automatically called, no need to use this. func add_translation_id() -> String: _translation_id = DialogicUtil.get_next_translation_id() @@ -259,6 +258,10 @@ func clear_translation_keys() -> void: _translation_keys.clear() +#endregion + + +#region GET AND SET TRANSLATION IDS AND KEYS ## Returns a key used to reference this glossary in the translation CSV file. ## @@ -282,19 +285,6 @@ func get_property_translation_key(entry_key: String, property: String) -> String return glossary_csv_key -## Returns the matching translation key for the given [param word]. -## This key can be used via [method tr] to get the translation. -## -## Time complexity: O(1) -## Uses an internal dictionary to find the translation key. -## This dictionary is generated when the glossary is translated. -## See [member _translation_keys]. -func get_word_translation_key(word: String) -> String: - if _translation_keys.has(word): - return _translation_keys[word] - - return "" - ## Returns the translation key prefix for this glossary. ## The resulting format will look like this: Glossary/a2/ @@ -347,3 +337,4 @@ func get_set_glossary_translation_id() -> String: return _translation_id +#endregion diff --git a/addons/dialogic/Modules/Glossary/subsystem_glossary.gd b/addons/dialogic/Modules/Glossary/subsystem_glossary.gd index c0e47dd1b..d438bc4bc 100644 --- a/addons/dialogic/Modules/Glossary/subsystem_glossary.gd +++ b/addons/dialogic/Modules/Glossary/subsystem_glossary.gd @@ -100,10 +100,6 @@ func add_glossary(path:String) -> void: ## Iterates over all glossaries and returns the first one that matches the ## [param entry_key]. ## -## Returns null if none of the glossaries has an entry with that key. -## If translation is enabled, uses the [param entry_key] as well to check -## [member _translation_keys]. -## ## Runtime complexity: ## O(n), where n is the number of glossaries. func find_glossary(entry_key: String) -> DialogicGlossary: @@ -150,9 +146,9 @@ func get_entry(entry_key: String) -> Dictionary: var tr_base := translation_key.substr(0, last_slash) - result.title = _try_translate(tr_base, "title", entry) - result.text = _try_translate(tr_base, "text", entry) - result.extra = _try_translate(tr_base, "extra", entry) + result.title = translate(tr_base, "title", entry) + result.text = translate(tr_base, "text", entry) + result.extra = translate(tr_base, "extra", entry) else: result.title = entry.get("title", "") result.text = entry.get("text", "") @@ -166,7 +162,9 @@ func get_entry(entry_key: String) -> Dictionary: return result -func _try_translate(tr_base: String, property: StringName, fallback_entry: Dictionary) -> String: + +## Tries to translate the property with the given +func translate(tr_base: String, property: StringName, fallback_entry: Dictionary) -> String: var tr_key := tr_base.path_join(property) var tr_value := tr(tr_key)