Skip to content

Instantly share code, notes, and snippets.

@squeek502 squeek502/merge_lang.lua Secret
Last active Aug 29, 2015

Embed
What would you like to do?
Merge Fortress Forever lang key value pairs from one file to another
-- NOTE:
-- the input files can't be UTF-16 with BOM encoded like the language files are;
-- they need to be converted to UTF-8 before running this script
local function file_exists(name)
local f=io.open(name,"r")
if f~=nil then io.close(f) return true else return false end
end
local function read_whole_file(filepath)
if not file_exists(filepath) then return nil end
local f = io.open(filepath, "rb")
local content = f:read("*all")
content = content:gsub("\r\n", "\n")
f:close()
return content
end
local function write_to_file(filepath, contents)
if contents == nil then return end
local outputfile = io.open(filepath, "w+")
io.output(outputfile)
io.write(contents)
io.close(outputfile)
end
local function make_pattern_plain(str)
return str:gsub("[%(%)%.%%%+%-%*%?%[%]%^%$]", function(c) return "%" .. c end)
end
local function make_pattern_plain(str)
return str:gsub("[%(%)%.%%%+%-%*%?%[%]%^%$]", function(c) return "%" .. c end)
end
local function make_replacement_safe(str)
return str:gsub("%%", "%%%%")
end
local function plain_gsub(haystack, needle, repl, n)
needle = make_pattern_plain(needle)
repl = make_replacement_safe(repl)
return string.gsub(haystack, needle, repl, n)
end
local function safe_gsub(haystack, needle, repl, n)
repl = make_replacement_safe(repl)
return string.gsub(haystack, needle, repl, n)
end
local function parse_lang_file(filepath)
local contents = read_whole_file(filepath)
local parsed = {}
if contents then
for indent, key, spacing, str in contents:gmatch("(%s*)\"([^\"]+)\"(%s+)\"([^\"]+)\"") do
parsed[key] = str
end
end
return parsed
end
local function merge_lang_file(from, to, output)
local parsed_from = parse_lang_file(from)
local output_lines = {}
local key_merged_status = {}
for key, str in pairs(parsed_from) do
key_merged_status[key] = false
end
if file_exists(to) then
for line in io.lines(to) do
local key = line:match("%s*\"([^\"]+)\"%s+\"[^\"]+\"")
if key then
if parsed_from[key] ~= nil then
line = line:gsub("(%s*\""..make_pattern_plain(key).."\"%s+)\"([^\"]+)\"", "%1\""..make_replacement_safe(parsed_from[key]).."\"")
key_merged_status[key] = true
else
print("Added key: " .. key)
end
end
table.insert(output_lines, line)
end
for key, merged in pairs(key_merged_status) do
if not merged then
print("Removed key: " .. key .. " ("..parsed_from[key]..")")
end
end
end
return write_to_file(output, table.concat(output_lines, "\n"))
end
local from = "from.txt"
local to = "to.txt"
local output = "merged.txt"
merge_lang_file(from, to, output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.