Skip to content

Commit

Permalink
Simplify glossary translation code (#2210)
Browse files Browse the repository at this point in the history
Moves some of the translation code for glossaries from the glossary layer into the glossary subsystem.
  • Loading branch information
Jowan-Spooner authored Apr 30, 2024
1 parent 56c3eda commit ee1ca8c
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 107 deletions.
24 changes: 10 additions & 14 deletions addons/dialogic/Editor/Settings/settings_translation.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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', [])
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -380,7 +377,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.
Expand Down Expand Up @@ -460,10 +457,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)
Expand Down Expand Up @@ -517,7 +514,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
24 changes: 7 additions & 17 deletions addons/dialogic/Modules/Glossary/glossary_resource.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand All @@ -49,9 +48,6 @@ const _MISSING_ENTRY_INDEX := -1
@export var _translation_keys: Dictionary = {}


func __get_property_list() -> Array:
return []


## Removes an entry and all its aliases (alternative property) from
## the glossary.
Expand Down Expand Up @@ -226,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()
Expand Down Expand Up @@ -260,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.
##
Expand All @@ -283,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/
Expand Down Expand Up @@ -348,3 +337,4 @@ func get_set_glossary_translation_id() -> String:

return _translation_id

#endregion
74 changes: 66 additions & 8 deletions addons/dialogic/Modules/Glossary/subsystem_glossary.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
####################################################################################################
Expand All @@ -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:
Expand Down Expand Up @@ -98,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:
Expand All @@ -110,7 +108,67 @@ 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 = 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", "")
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



## 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)

if tr_key == tr_value:
tr_value = fallback_entry.get(property, "")

return tr_value

0 comments on commit ee1ca8c

Please sign in to comment.