|
| 1 | +class_name DialogicCsvFile |
| 2 | +extends RefCounted |
| 3 | +## Handles translation of a [class DialogicTimeline] to a CSV file. |
| 4 | + |
| 5 | +var lines: Array[PackedStringArray] = [] |
| 6 | +## Dictionary of lines from the original file. |
| 7 | +## Key: String, Value: PackedStringArray |
| 8 | +var old_lines: Dictionary = {} |
| 9 | + |
| 10 | +## The amount of columns the CSV file has after loading it. |
| 11 | +## Used to add trailing commas to new lines. |
| 12 | +var column_count := 0 |
| 13 | + |
| 14 | +## Whether this CSV file was able to be loaded a defined |
| 15 | +## file path. |
| 16 | +var is_new_file: bool = false |
| 17 | + |
| 18 | +## The underlying file used to read and write the CSV file. |
| 19 | +var file: FileAccess |
| 20 | + |
| 21 | +## File path used to load the CSV file. |
| 22 | +var used_file_path: String |
| 23 | + |
| 24 | +## The amount of events that were updated in the CSV file. |
| 25 | +var updated_events: int = 0 |
| 26 | + |
| 27 | +## The amount of events that were added to the CSV file. |
| 28 | +var new_events: int = 0 |
| 29 | + |
| 30 | +## Attempts to load the CSV file from [param file_path]. |
| 31 | +## If the file does not exist, a single entry is added to the [member lines] |
| 32 | +## array. |
| 33 | +func _init(file_path: String, original_locale: String) -> void: |
| 34 | + used_file_path = file_path |
| 35 | + |
| 36 | + # The first entry must be the locale row. |
| 37 | + # [method collect_lines_from_timeline] will add the other locales, if any. |
| 38 | + var locale_array_line := PackedStringArray(["keys", original_locale]) |
| 39 | + lines.append(locale_array_line) |
| 40 | + |
| 41 | + if not FileAccess.file_exists(file_path): |
| 42 | + is_new_file = true |
| 43 | + |
| 44 | + # The "keys" and original locale are the only columns in a new file. |
| 45 | + # For example: "keys, en" |
| 46 | + column_count = 2 |
| 47 | + |
| 48 | + file = FileAccess.open(file_path, FileAccess.WRITE) |
| 49 | + return |
| 50 | + |
| 51 | + file = FileAccess.open(file_path, FileAccess.READ) |
| 52 | + |
| 53 | + var locale_csv_row := file.get_csv_line() |
| 54 | + column_count = locale_csv_row.size() |
| 55 | + var locale_key := locale_csv_row[0] |
| 56 | + |
| 57 | + old_lines[locale_key] = locale_csv_row |
| 58 | + |
| 59 | + _read_file_into_lines() |
| 60 | + |
| 61 | + |
| 62 | +## Private function to read the CSV file into the [member lines] array. |
| 63 | +func _read_file_into_lines() -> void: |
| 64 | + while not file.eof_reached(): |
| 65 | + var line := file.get_csv_line() |
| 66 | + var row_key := line[0] |
| 67 | + old_lines[row_key] = line |
| 68 | + |
| 69 | + |
| 70 | +func collect_lines_from_timeline(timeline: DialogicTimeline) -> void: |
| 71 | + for event in timeline.events: |
| 72 | + |
| 73 | + if event.can_be_translated(): |
| 74 | + |
| 75 | + if event._translation_id.is_empty(): |
| 76 | + event.add_translation_id() |
| 77 | + event.update_text_version() |
| 78 | + |
| 79 | + for property in event._get_translatable_properties(): |
| 80 | + var line_key: String = event.get_property_translation_key(property) |
| 81 | + var line_value: String = event._get_property_original_translation(property) |
| 82 | + var array_line := PackedStringArray([line_key, line_value]) |
| 83 | + lines.append(array_line) |
| 84 | + |
| 85 | + |
| 86 | +## Clears the CSV file on disk and writes the current [member lines] array to it. |
| 87 | +## Uses the [member old_lines] dictionary to update existing translations. |
| 88 | +## If a translation row misses a column, a trailing comma will be added to |
| 89 | +## conform to the CSV file format. |
| 90 | +func update_csv_file_on_disk() -> void: |
| 91 | + # Clear the current CSV file. |
| 92 | + file = FileAccess.open(used_file_path, FileAccess.WRITE) |
| 93 | + |
| 94 | + for line in lines: |
| 95 | + var row_key := line[0] |
| 96 | + |
| 97 | + # In case there might be translations for this line already, |
| 98 | + # add them at the end again (orig locale text is replaced). |
| 99 | + if row_key in old_lines: |
| 100 | + var old_line: PackedStringArray = old_lines[row_key] |
| 101 | + var updated_line: PackedStringArray = line + old_line.slice(2) |
| 102 | + |
| 103 | + var line_columns: int = updated_line.size() |
| 104 | + var line_columns_to_add := column_count - line_columns |
| 105 | + |
| 106 | + # Add trailing commas to match the amount of columns. |
| 107 | + for _i in range(line_columns_to_add): |
| 108 | + updated_line.append("") |
| 109 | + |
| 110 | + file.store_csv_line(updated_line) |
| 111 | + updated_events += 1 |
| 112 | + |
| 113 | + else: |
| 114 | + var line_columns: int = line.size() |
| 115 | + var line_columns_to_add := column_count - line_columns |
| 116 | + |
| 117 | + # Add trailing commas to match the amount of columns. |
| 118 | + for _i in range(line_columns_to_add): |
| 119 | + line.append("") |
| 120 | + |
| 121 | + file.store_csv_line(line) |
| 122 | + new_events += 1 |
| 123 | + |
| 124 | + file.close() |
0 commit comments