mirror of
https://github.com/luanti-org/luanti.git
synced 2025-08-06 17:41:04 +00:00
Settingtypes: Add comments and deduplicate translation strings
This commit is contained in:
parent
e1b057ca56
commit
ef7f1ac28f
1 changed files with 27 additions and 5 deletions
|
@ -109,19 +109,41 @@ local translation_file_header = [[
|
||||||
|
|
||||||
fake_function() {]]
|
fake_function() {]]
|
||||||
|
|
||||||
|
local function add_translation_string(result, str, seen)
|
||||||
|
if seen[str] then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
seen[str] = true
|
||||||
|
|
||||||
|
-- Prevent gettext from interpreting e.g. "50% of volume" as C-formatted string
|
||||||
|
-- Documentation: https://www.gnu.org/software/gettext/manual/html_node/c_002dformat-Flag.html
|
||||||
|
local force_no_c_format = str:find("%", 1, true)
|
||||||
|
local prefix = force_no_c_format and "/* xgettext:no-c-format */ " or ""
|
||||||
|
|
||||||
|
local have_newlines = str:find("\n", 1, true)
|
||||||
|
if have_newlines then
|
||||||
|
-- Formatting as "%q" inserts literal newlines. But we want '\n'.
|
||||||
|
-- Hence, use "%s" and escape relevant characters manually.
|
||||||
|
str = str:gsub("\n", "\\n")
|
||||||
|
str = str:gsub("\"", "\\\"")
|
||||||
|
insert(result, sprintf("\t%sgettext(\"%s\");", prefix, str))
|
||||||
|
else
|
||||||
|
insert(result, sprintf("\t%sgettext(%q);", prefix, str))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local function create_translation_file(settings)
|
local function create_translation_file(settings)
|
||||||
|
local seen = {} -- to deduplicate entries
|
||||||
local result = { translation_file_header }
|
local result = { translation_file_header }
|
||||||
for _, entry in ipairs(settings) do
|
for _, entry in ipairs(settings) do
|
||||||
if entry.type == "category" then
|
if entry.type == "category" then
|
||||||
insert(result, sprintf("\t/* xgettext:no-c-format */ gettext(%q);", entry.name))
|
add_translation_string(result, entry.name, seen)
|
||||||
else
|
else
|
||||||
if entry.readable_name then
|
if entry.readable_name then
|
||||||
insert(result, sprintf("\t/* xgettext:no-c-format */ gettext(%q);", entry.readable_name))
|
add_translation_string(result, entry.readable_name, seen)
|
||||||
end
|
end
|
||||||
if entry.comment ~= "" then
|
if entry.comment ~= "" then
|
||||||
local comment_escaped = entry.comment:gsub("\n", "\\n")
|
add_translation_string(result, entry.comment, seen)
|
||||||
comment_escaped = comment_escaped:gsub("\"", "\\\"")
|
|
||||||
insert(result, "\t/* xgettext:no-c-format */ gettext(\"" .. comment_escaped .. "\");")
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue