Skip to content

Commit

Permalink
Add file references to po translation files. (#718)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kiisu-Master committed May 5, 2024
1 parent 51c1d70 commit df7d19c
Show file tree
Hide file tree
Showing 7 changed files with 1,373 additions and 21 deletions.
82 changes: 66 additions & 16 deletions godot_only/update_translations.gd
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,44 @@ const delimiters = {
'TranslationServer.translate_plural("""': '""")',
}

var strings: PackedStringArray = PackedStringArray(["translation-credits"])
var messages: Array[Message] = [Message.new("translation-credits")]


class Message:
var refs: Array[MsgRef]
var msgid := String()

func _init(p_msgid: String, ref := MsgRef.new("", 0)):
msgid = p_msgid
if not ref.is_empty():
refs.append(ref)

func _to_string() -> String:
var ret := "\n"
for ref in refs:
ret += "#: %s\n" % ref
return ret + 'msgid "%s"\nmsgstr ""\n' % msgid


class MsgRef:
var path := String()
var line := 0

func _init(p_path: String, p_line: int):
path = p_path
line = p_line

func is_empty():
return path.is_empty()

func _to_string():
return path + ":%d" % line


func _run() -> void:
search_directory(ProjectSettings.globalize_path("src"))
update_translations()
if not OS.execute("msgmerge", PackedStringArray()) == -1:
search_directory(ProjectSettings.globalize_path("src"))
update_translations()


func search_directory(dir: String) -> void:
Expand All @@ -50,34 +83,51 @@ func search_directory(dir: String) -> void:
break

var string_start := cursor + start_delim.length()
var line := file_text.count("\n", 0, string_start) + 1
cursor = file_text.find(end_delim, cursor)
var string := file_text.substr(string_start, cursor - string_start)
if not string in strings:
strings.append(string)

var msgid := file_text.substr(string_start, cursor - string_start)
var ref := MsgRef.new(dir.path_join(file_name), line)

var already_exists := false
for msg in messages:
if msg.msgid == msgid:
already_exists = true
msg.refs.append(ref)
break
if not already_exists:
messages.append(Message.new(msgid, ref))


func update_translations() -> void:
var location := ProjectSettings.globalize_path("translations/GodSVG.pot")
var fa := FileAccess.open(location, FileAccess.WRITE)
fa.store_string(HEADER)

for string in strings:
fa.store_string('\nmsgid "%s"\nmsgstr ""\n' % string)
for msg in messages:
fa.store_string(msg.to_string())
fa = null
print("Created translations/GodSVG.pot with %d strings" % (strings.size() + 1))
print("Created translations/GodSVG.pot with %d strings" % (messages.size() + 1))

var files := DirAccess.get_files_at(ProjectSettings.globalize_path("translations"))
for file in files:
if file.get_extension() != "po":
if not file.get_extension() == "po" and file != "GodSVG.pot":
continue

var args := PackedStringArray(["--update", "--quiet", "--verbose", "--backup=off",
ProjectSettings.globalize_path("translations").path_join(file), location])
var args := PackedStringArray([
"--update",
"--quiet",
"--verbose",
"--backup=off",
ProjectSettings.globalize_path("translations").path_join(file),
location
])
var output := []
var result := OS.execute("msgmerge", args, output, true)
if result == -1:
print("msgmerge failed.")
else:
if not output.is_empty():
if not result == -1:
if file == "GodSVG.pot":
continue
elif not output.is_empty():
print("Updated translations/%s: %s" % [file, output[0].rstrip("\n")])
else:
print("Updated translations%s" % file)
Loading

0 comments on commit df7d19c

Please sign in to comment.