From c11a8062f4b358c7a1e38de6daeab5739be07171 Mon Sep 17 00:00:00 2001 From: Marc Gilleron Date: Sun, 14 Jun 2020 16:50:00 +0100 Subject: [PATCH] Fix TranslationServer calls not detected, have async and non-async in extractor API to make it easier to test --- .../tools/extractor.gd | 34 +++++++++++++++---- .../tools/extractor_dialog.gd | 2 +- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/addons/zylann.translation_editor/tools/extractor.gd b/addons/zylann.translation_editor/tools/extractor.gd index 1bd5b1b..9b70bce 100644 --- a/addons/zylann.translation_editor/tools/extractor.gd +++ b/addons/zylann.translation_editor/tools/extractor.gd @@ -6,6 +6,8 @@ const STATE_READING_TEXT = 1 signal finished(results) signal progress_reported(ratio) +# TODO Do we want to know if a text is found multiple times in the same file? +# text => { file => line number } var _strings = {} var _thread = null var _time_before = 0.0 @@ -13,7 +15,19 @@ var _ignored_paths = {} var _paths = [] +func extract_async(root, ignored_paths=[]): + _prepare(root, ignored_paths) + _thread = Thread.new() + _thread.start(self, "_extract_thread_func", root) + + func extract(root, ignored_paths=[]): + _prepare(root, ignored_paths) + _extract(root) + return _strings + + +func _prepare(root, ignored_paths): _time_before = OS.get_ticks_msec() assert(_thread == null) @@ -21,8 +35,7 @@ func extract(root, ignored_paths=[]): for p in ignored_paths: _ignored_paths[root.plus_file(p)] = true - _thread = Thread.new() - _thread.start(self, "_extract", root) + _strings.clear() func _extract(root): @@ -44,6 +57,9 @@ func _extract(root): f.close() call_deferred("_report_progress", float(i) / float(len(_paths))) + +func _extract_thread_func(root): + _extract(root) call_deferred("_finished") @@ -121,7 +137,6 @@ func _process_tscn(f, fpath): func _process_gd(f, fpath): - var pattern = "tr(" var text = "" var line_number = 0 @@ -136,13 +151,19 @@ func _process_gd(f, fpath): var search_index = 0 var counter = 0 while true: + var pattern = "tr(" var call_index = line.find(pattern, search_index) if call_index == -1: - break + pattern = "TranslationServer.translate(" + call_index = line.find(pattern, search_index) + if call_index == -1: + break + if call_index != 0: if line.substr(call_index - 1, 3).is_valid_identifier(): - # not a tr( call - break + # not a tr( call, skip + search_index = call_index + len(pattern) + continue if line[call_index - 1] == '"': break # TODO There may be more cases to handle @@ -168,6 +189,7 @@ func _process_gd(f, fpath): search_index = end_bracket_index counter += 1 + # If that fails it means we spent 100 iterations in the same line, that's suspicious assert(counter < 100) diff --git a/addons/zylann.translation_editor/tools/extractor_dialog.gd b/addons/zylann.translation_editor/tools/extractor_dialog.gd index bc0ca5c..a98dd3f 100644 --- a/addons/zylann.translation_editor/tools/extractor_dialog.gd +++ b/addons/zylann.translation_editor/tools/extractor_dialog.gd @@ -57,7 +57,7 @@ func _on_ExtractButton_pressed(): _extractor = Extractor.new() _extractor.connect("progress_reported", self, "_on_Extractor_progress_reported") _extractor.connect("finished", self, "_on_Extractor_finished") - _extractor.extract(root, excluded_dirs) + _extractor.extract_async(root, excluded_dirs) _progress_bar.value = 0 _progress_bar.show()