Skip to content

Commit 141232d

Browse files
committed
Add csv exporter
1 parent 6ad687a commit 141232d

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

addons/dialog_plugin/Core/Dialog_i18n.gd

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,90 @@ static func load_translations() -> void:
1919
var _translations:Array = Array(load(DialogResources.TRANSLATIONSDB_PATH).resources.get_resources())
2020
for translation in _translations:
2121
TranslationService.add_translation(translation)
22+
23+
24+
static func export_translations_as_csv() -> void:
25+
var _translations:Array = Array(load(DialogResources.TRANSLATIONSDB_PATH).resources.get_resources())
26+
for translation in _translations:
27+
translation = translation as Translation
28+
29+
var translation_file_name = (translation.resource_path as String).get_file().split(".")[0]
30+
var translation_file_extension = ".csv"
31+
var translation_file_path = DialogResources.TRANSLATIONS_DIR+translation_file_name+translation_file_extension
32+
33+
var file = File.new()
34+
var _err:int
35+
36+
if file.file_exists(translation_file_path):
37+
file.open(translation_file_path, file.READ)
38+
else:
39+
file.open(translation_file_path, file.WRITE_READ)
40+
41+
if _err == OK:
42+
# Save old keys
43+
var csv_keys:Array = Array(file.get_csv_line())
44+
var csv_content:Array = []
45+
var csv_translation_keys:Array = []
46+
47+
# Save old data in the file
48+
while not file.eof_reached():
49+
var line = Array(file.get_csv_line())
50+
if not line.empty():
51+
if line.size() == 1 and line[0] == "":
52+
continue
53+
csv_content.append(line)
54+
55+
if not "key" in csv_keys:
56+
csv_keys[0] = "key"
57+
if not translation.locale in csv_keys:
58+
csv_keys.append(translation.locale)
59+
60+
file.close()
61+
62+
# Save a reference for xlation keys
63+
for line in csv_content:
64+
if line.size() == 1 and line[0] == "":
65+
continue
66+
csv_translation_keys.append(line[0])
67+
68+
# For key ID in the translation file
69+
for t_key in translation.get_message_list():
70+
var t_key_position = csv_translation_keys.find(t_key)
71+
var locale_position = csv_keys.find(translation.locale)
72+
73+
if t_key_position != -1:
74+
# They key exist, replace its content
75+
var line = csv_content[t_key_position]
76+
if locale_position >= line.size():
77+
while locale_position >= line.size():
78+
line.append("")
79+
line[locale_position] = translation.get_message(t_key)
80+
else:
81+
# The key doesn't exist, create one
82+
var line = [t_key]
83+
for locale in csv_keys.size()-1:
84+
line.append(" ")
85+
line[locale_position] = translation.get_message(t_key)
86+
csv_content.append(line)
87+
88+
89+
_err = file.open(translation_file_path, file.WRITE)
90+
91+
if _err == OK:
92+
# Save the keys
93+
file.store_csv_line(csv_keys)
94+
95+
# Save all the content
96+
for line in csv_content:
97+
file.store_csv_line(line)
98+
99+
file.close()
100+
101+
# Force the importer to not compress the translations
102+
# This had to be this way while PHashTranslation get fixed
103+
var import_config = ConfigFile.new()
104+
_err = import_config.load(translation_file_path)
105+
assert(_err == OK)
106+
import_config.set_value("params", "compress", false)
107+
import_config.save(translation_file_path+".import")
108+
assert(_err == OK)

addons/dialog_plugin/Other/translation_service/translation_service.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ static func get_project_test_locale() -> String:
101101

102102
static func get_project_translations() -> PoolStringArray:
103103

104-
# This must be done once, since the property doesn't exist if you never a translation before
104+
# This must be done once, since the property doesn't exist if you never had a translation before
105105
if not ProjectSettings.get_setting("locale/translations"):
106106
var _empty_poolstringarray = PoolStringArray([])
107107
ProjectSettings.set_setting("locale/translations", _empty_poolstringarray)

0 commit comments

Comments
 (0)