Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 97 additions & 15 deletions addons/dialogic/Editor/Settings/CoreSettingsPages/csv_file.gd
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,15 @@ func _read_file_into_lines() -> void:
##
## If this is the character name CSV file, use this method to
## take previously collected characters from other [class DialogicCsvFile]s.
func collect_lines_from_characters(characters: Dictionary) -> void:
func collect_lines_from_characters(characters: Dictionary, update_text: bool = false, text_dict: Dictionary = {}) -> void:
for character: DialogicCharacter in characters.values():
# Add row for display names.
var name_property := DialogicCharacter.TranslatedProperties.NAME
var display_name_key: String = character.get_property_translation_key(name_property)

if update_text and display_name_key in text_dict and text_dict[display_name_key]:
character.display_name = text_dict[display_name_key]

var line_value: String = character.display_name
var array_line := PackedStringArray([display_name_key, line_value])
lines.append(array_line)
Expand All @@ -104,6 +108,11 @@ func collect_lines_from_characters(characters: Dictionary) -> void:
var nick_name_property := DialogicCharacter.TranslatedProperties.NICKNAMES
var nickname_string: String = ",".join(nicknames)
var nickname_name_line_key: String = character.get_property_translation_key(nick_name_property)

if update_text and nickname_name_line_key in text_dict and text_dict[nickname_name_line_key]:
nickname_string = text_dict[nickname_name_line_key]
character.nicknames = nickname_string.split(",")

var nick_array_line := PackedStringArray([nickname_name_line_key, nickname_string])
lines.append(nick_array_line)

Expand Down Expand Up @@ -226,11 +235,17 @@ func _sort_glossary_entry_property_keys(property_key_a: String, property_key_b:

return value_a < value_b

# We use a space after the comma to make it easier to read.
var glossary_item_array_separator = ", "

## Collects properties from glossary entries from the given [param glossary] and
## adds them to the [member lines].
func collect_lines_from_glossary(glossary: DialogicGlossary) -> void:

func collect_lines_from_glossary(glossary: DialogicGlossary, update_text: bool = false, text_dict: Dictionary = {}) -> void:

var entry_keys_to_replace : Dictionary = {} # "old entry key": "new entry key"
var aliases_to_remove : Array = []
var aliases_to_add : Dictionary = {} # " "new alias": "entry key"

for glossary_value: Variant in glossary.entries.values():

if glossary_value is String:
Expand All @@ -247,30 +262,73 @@ func collect_lines_from_glossary(glossary: DialogicGlossary) -> void:

var entry_name_property: String = glossary_entry[DialogicGlossary.NAME_PROPERTY]

for entry_key: String in entry_property_keys:
for entry_property_key: String in entry_property_keys:
# Ignore private keys.
if entry_key.begins_with(DialogicGlossary.PRIVATE_PROPERTY_PREFIX):
if entry_property_key.begins_with(DialogicGlossary.PRIVATE_PROPERTY_PREFIX):
continue

var item_value: Variant = glossary_entry[entry_key]
var item_value: Variant = glossary_entry[entry_property_key]
var item_value_str := ""

if item_value is Array:
var item_array := item_value as Array
# We use a space after the comma to make it easier to read.
item_value_str = " ,".join(item_array)
item_value_str = glossary_item_array_separator.join(item_array)

elif not item_value is String or item_value.is_empty():
continue

else:
item_value_str = item_value

var glossary_csv_key := glossary._get_glossary_translation_key(entry_translation_id, entry_key)

if (entry_key == DialogicGlossary.NAME_PROPERTY
or entry_key == DialogicGlossary.ALTERNATIVE_PROPERTY):
glossary.entries[glossary_csv_key] = entry_name_property
var glossary_csv_key := glossary._get_glossary_translation_key(entry_translation_id, entry_property_key)

if update_text and glossary_csv_key in text_dict and text_dict[glossary_csv_key]:

if entry_property_key == DialogicGlossary.ALTERNATIVE_PROPERTY:
aliases_to_remove.append_array(item_value)
var new_aliases_array = text_dict[glossary_csv_key].split(glossary_item_array_separator) as Array

var new_aliases_array_dedup : Array = []

for alias in new_aliases_array:

var new_alias = alias
# It's possible that translations of two words could lead to the same key,
# we add a suffix in this case to avoid any issues
while new_alias in aliases_to_add:
new_alias += " (duplicate)"
while new_alias in entry_keys_to_replace.values():
new_alias += " (duplicate)"

aliases_to_add[new_alias] = entry_name_property

new_aliases_array_dedup.append(new_alias)

item_value_str = glossary_item_array_separator.join(new_aliases_array_dedup)
glossary_entry[entry_property_key] = new_aliases_array_dedup
else:
item_value_str = text_dict[glossary_csv_key]
glossary_entry[entry_property_key] = text_dict[glossary_csv_key]

if entry_property_key == DialogicGlossary.NAME_PROPERTY:
var new_entry_key : String = text_dict[glossary_csv_key]

# It's possible that translations of two words could lead to the same key,
# we add a suffix in this case to avoid any issues
while new_entry_key in entry_keys_to_replace.values():
new_entry_key += " (duplicate)"
while new_entry_key in aliases_to_add:
new_entry_key += " (duplicate)"

entry_keys_to_replace[entry_name_property] = new_entry_key

if (entry_property_key == DialogicGlossary.NAME_PROPERTY
or entry_property_key == DialogicGlossary.ALTERNATIVE_PROPERTY):
var entry_key = entry_name_property
if entry_key in entry_keys_to_replace:
entry_key = entry_keys_to_replace[entry_key]

glossary.entries[glossary_csv_key] = entry_key

var glossary_line := PackedStringArray([glossary_csv_key, item_value_str])

Expand All @@ -279,25 +337,49 @@ func collect_lines_from_glossary(glossary: DialogicGlossary) -> void:
# New glossary item, if needed, add a separator.
if add_separator:
_append_empty()



if update_text:
for old_entry_key in entry_keys_to_replace:
var new_entry_key = entry_keys_to_replace[old_entry_key]
glossary.replace_entry_key(old_entry_key, new_entry_key)

for old_alias in aliases_to_remove:
glossary._remove_entry_alias(old_alias)

for new_alias in aliases_to_add:

var entry_key = aliases_to_add[new_alias]

if entry_key in entry_keys_to_replace:
entry_key = entry_keys_to_replace[entry_key]

glossary._add_entry_key_alias(entry_key, new_alias)


## Collects translatable events from the given [param timeline] and adds
## them to the [member lines].
func collect_lines_from_timeline(timeline: DialogicTimeline) -> void:
func collect_lines_from_timeline(timeline: DialogicTimeline, update_text: bool = false, text_dict: Dictionary = {}) -> void:
for event: DialogicEvent in timeline.events:

if event.can_be_translated():

if event._translation_id.is_empty():
event.add_translation_id()
event.update_text_version()


var properties: Array = event._get_translatable_properties()

for property: String in properties:
var line_key: String = event.get_property_translation_key(property)

if update_text and line_key in text_dict and text_dict[line_key]:
event._set_property_original_translation(property, text_dict[line_key])
event.update_text_version()

var line_value: String = event._get_property_original_translation(property)

var array_line := PackedStringArray([line_key, line_value])
lines.append(array_line)

Expand Down
Loading