|
| 1 | +class_name DialogicTranslationGettextFile |
| 2 | +extends DialogicTranslationFile |
| 3 | +## Generates translation files in gettext format. |
| 4 | + |
| 5 | +var translations: Array[PotEntry] = [] |
| 6 | + |
| 7 | +## Configured original locale. |
| 8 | +var original_locale: String |
| 9 | + |
| 10 | +## Locations of the source files included in this translation. |
| 11 | +var locations: Array[String] = [] |
| 12 | + |
| 13 | + |
| 14 | +## There is no need to load the old file(s) here, because every locale has its own file |
| 15 | +## and this class doens't touch them. |
| 16 | +func _init(file_path: String, original_locale: String) -> void: |
| 17 | + super._init(file_path, original_locale) |
| 18 | + self.original_locale = original_locale |
| 19 | + |
| 20 | + |
| 21 | +func _append(key: String, value: String, path: String, line_number: int = -1) -> void: |
| 22 | + var entry = PotEntry.new() |
| 23 | + entry.key = key |
| 24 | + entry.translation = value |
| 25 | + entry.locations.append(PotReference.new(path, line_number)) |
| 26 | + translations.append(entry) |
| 27 | + |
| 28 | + |
| 29 | +## gettext doesn't support separators so this is a no-op. |
| 30 | +func _append_separator() -> void: |
| 31 | + pass |
| 32 | + |
| 33 | + |
| 34 | +## Overwrites the .pot file and the .po file of the original locale with the current [member translations] array. |
| 35 | +func update_file_on_disk() -> void: |
| 36 | + # Overwrite the POT file. |
| 37 | + var file = FileAccess.open(used_file_path, FileAccess.WRITE) |
| 38 | + _write_header(file) |
| 39 | + for entry in translations: |
| 40 | + _write_entry(file, entry, "") |
| 41 | + file.close() |
| 42 | + |
| 43 | + # Overwrite the original_locale PO file. |
| 44 | + file = FileAccess.open(used_file_path.trim_suffix(".pot") + "." + original_locale + ".po", FileAccess.WRITE) |
| 45 | + _write_header(file, original_locale) |
| 46 | + for entry in translations: |
| 47 | + _write_entry(file, entry) |
| 48 | + file.close() |
| 49 | + |
| 50 | + |
| 51 | +# This is based on POTGenerator::_write_to_pot() which unfortunately isn't exposed to gdscript. |
| 52 | +func _write_header(file: FileAccess, locale: String = "") -> void: |
| 53 | + var project_name = ProjectSettings.get("application/config/name"); |
| 54 | + var language_header = locale if !locale.is_empty() else "LANGUAGE" |
| 55 | + file.store_line("# " + language_header + " translation for " + project_name + " for the following files:") |
| 56 | + |
| 57 | + locations.sort() |
| 58 | + for location in locations: |
| 59 | + file.store_line("# " + location) |
| 60 | + |
| 61 | + file.store_line("") |
| 62 | + file.store_line("#, fuzzy"); |
| 63 | + file.store_line("msgid \"\"") |
| 64 | + file.store_line("msgstr \"\"") |
| 65 | + file.store_line("\"Project-Id-Version: " + project_name + "\\n\"") |
| 66 | + if !locale.is_empty(): |
| 67 | + file.store_line("\"Language: " + locale + "\\n\"") |
| 68 | + file.store_line("\"MIME-Version: 1.0\\n\"") |
| 69 | + file.store_line("\"Content-Type: text/plain; charset=UTF-8\\n\"") |
| 70 | + file.store_line("\"Content-Transfer-Encoding: 8-bit\\n\"") |
| 71 | + |
| 72 | + |
| 73 | +func _write_entry(file: FileAccess, entry: PotEntry, value: String = entry.translation) -> void: |
| 74 | + file.store_line("") |
| 75 | + |
| 76 | + entry.locations.sort_custom(func (a: String, b: String): return b > a) |
| 77 | + for location in entry.locations: |
| 78 | + file.store_line("#: " + location.as_str()) |
| 79 | + |
| 80 | + _write_line(file, "msgid", entry.key) |
| 81 | + _write_line(file, "msgstr", value) |
| 82 | + |
| 83 | + |
| 84 | +# This is based on POTGenerator::_write_msgid() which unfortunately isn't exposed to gdscript. |
| 85 | +func _write_line(file: FileAccess, type: String, value: String) -> void: |
| 86 | + file.store_string(type + " ") |
| 87 | + if value.is_empty(): |
| 88 | + file.store_line("\"\"") |
| 89 | + return |
| 90 | + |
| 91 | + var lines = value.split("\n") |
| 92 | + var last_line = lines[lines.size() - 1] |
| 93 | + var pot_line_count = lines.size() |
| 94 | + if last_line.is_empty(): |
| 95 | + pot_line_count -= 1 |
| 96 | + |
| 97 | + if pot_line_count > 1: |
| 98 | + file.store_line("\"\"") |
| 99 | + |
| 100 | + for i in range(0, lines.size() - 1): |
| 101 | + file.store_line("\"" + (lines[i] + "\n").json_escape() + "\"") |
| 102 | + |
| 103 | + if !last_line.is_empty(): |
| 104 | + file.store_line("\"" + last_line.json_escape() + "\"") |
| 105 | + |
| 106 | + |
| 107 | +func collect_lines_from_character(character: DialogicCharacter) -> void: |
| 108 | + super.collect_lines_from_character(character) |
| 109 | + locations.append(character.resource_path) |
| 110 | + |
| 111 | + |
| 112 | +func collect_lines_from_glossary(glossary: DialogicGlossary) -> void: |
| 113 | + super.collect_lines_from_glossary(glossary) |
| 114 | + locations.append(glossary.resource_path) |
| 115 | + |
| 116 | + |
| 117 | +func collect_lines_from_timeline(timeline: DialogicTimeline) -> void: |
| 118 | + super.collect_lines_from_timeline(timeline) |
| 119 | + locations.append(timeline.resource_path) |
| 120 | + |
| 121 | + |
| 122 | +class PotReference: |
| 123 | + var path: String |
| 124 | + var line_number: int |
| 125 | + |
| 126 | + |
| 127 | + func _init(path: String, line_number: int) -> void: |
| 128 | + self.path = path |
| 129 | + self.line_number = line_number |
| 130 | + |
| 131 | + |
| 132 | + func as_str() -> String: |
| 133 | + var str = "" |
| 134 | + if path.contains(" "): |
| 135 | + str += "\u2068" + path.trim_prefix("res://").replace("\n", "\\n") + "\u2069" |
| 136 | + else: |
| 137 | + str += path.trim_prefix("res://").replace("\n", "\\n") |
| 138 | + |
| 139 | + if line_number >= 0: |
| 140 | + str += ":" + str(line_number) |
| 141 | + |
| 142 | + return str |
| 143 | + |
| 144 | + |
| 145 | +class PotEntry: |
| 146 | + var key: String |
| 147 | + var translation: String |
| 148 | + var locations: Array[PotReference] = [] |
0 commit comments