Skip to content
Snippets Groups Projects

Staged translation updates

Open Otowa Kotori requested to merge otowakotori/tome-addon-dev:i18n into master
17 files
+ 1564
63
Compare changes
  • Side-by-side
  • Inline
Files
17
@@ -23,6 +23,11 @@ local Dialog = require "engine.ui.Dialog"
local lpeg = require 'lpeg'
local locale = config.settings.locale
local I18N = require "engine.I18N"
local PrintXlf = require"engine.i18nhelper.PrintXlf"
local PrintTmx = require"engine.i18nhelper.PrintTmx"
local PrintOriginal = require"engine.i18nhelper.PrintOriginal"
local lom = require "lxp.lom"
module(..., package.seeall, class.make)
local MARK_SEPRATOR = "$$$"
@@ -166,7 +171,6 @@ local PFmtF = PFmtPm ^ -1 * (PFmtDigit * ("." * PFmtDigit)^-1)^-1 * PFmtFType
local PFmtPld = lpeg.C(PFmtS + PFmtD + PFmtF) + "%" + patt_error
local PFmtStr = "%" * PFmtPld
local PFmt = (PFmtStr+1)^0
function _M:checkTFormat(src, tgt, tag, args_order)
local tformat_like_tags = {
tformat = true, ["nil"]=true,
@@ -177,6 +181,17 @@ function _M:checkTFormat(src, tgt, tag, args_order)
return
end
err_msg = nil
local old_tgt = tgt
if not args_order and tgt:find("%%(%d+)%$") then
args_order = {}
tgt = tgt:gsub('%%(%d+)%$(.+)^', function(i, x)
table.insert(args_order, tonumber(i))
return '%'
end):gsub('%%(%d+)%$', function(i)
table.insert(args_order, tonumber(i))
return '%'
end)
end
local src_s = {lpeg.match(PFmt, src)}
local tgt_s = {lpeg.match(PFmt, tgt)}
if err_msg then
@@ -184,28 +199,29 @@ function _M:checkTFormat(src, tgt, tag, args_order)
return
end
if #tgt_s == 1 and type(tgt_s[1]) == "number" then return end
local checked = true
if args_order then
for k, v in ipairs(tgt_s) do
if src_s[args_order[k]] ~= v then
checked = false
local checked = #src_s == #tgt_s
if checked then
if args_order then
for k, v in ipairs(tgt_s) do
if src_s[args_order[k]] ~= v then
checked = false
break
end
end
end
else
for k, v in ipairs(tgt_s) do
if src_s[k] ~= v then
checked = false
else
for k, v in ipairs(tgt_s) do
if src_s[k] ~= v then
checked = false
break
end
end
end
end
if #src_s ~= #tgt_s then
checked = false
end
if not checked then
self:log([[[WARNING]Mismatched tformat string:
Source: %s %s
Target: %s %s (args=%s)
]], src, table.tostring(src_s), tgt, table.tostring(tgt_s), args_order and table.tostring(args_order) or "nil")
]], src, table.tostring(src_s), old_tgt, table.tostring(tgt_s), args_order and table.tostring(args_order) or "nil")
end
end
@@ -303,10 +319,107 @@ def copy_file(locale, part, path):
f:close()
end
function _M:loadXml(filepath, delete)
if fs.exists(filepath) then
local f = fs.open(filepath, "r")
local data = f:read(10485760 * 2) -- 20M
f:close()
local res = lom.parse(data)
if res:findOne("trans-unit") then
local list = res:findAll("trans-unit")
print("find trans units")
self:changeSection("xml-part")
for i, v in ipairs(list) do
local tag = v.attr.tag
local source = v:findOne("source")[1]
local target = v:findOne("target")[1]
game.log("%s, %s, %s", tag, source, target)
self:loadT(source, target, tag)
end
end
if delete then
local f= fs.open(filepath, "w")
f:write("")
f:close()
end
end
end
function _M:print()
self:cleanT()
local wp = FSHelper.locale_working_path
local ep = FSHelper.extracted_text_path
local xmlwp = FSHelper.locale_xml_working_path
self.logger = fs.open(wp .. "i18n.log", "w")
self:loadDefinition(wp .. "_t_append.lua")
self:loadDefinition(wp .. "_not_merged.lua")
if not fs.exists(wp .. "_t_append.lua") then
FSHelper.write_file(wp .. "_t_append.lua", "\n")
end
local parts = FSHelper.parts
for part_name, _ in pairs(parts) do
self:loadDefinition(wp .. part_name .. ".lua")
self:loadXml(xmlwp .. part_name .. ".xlf")
end
local cur_locale_name, cur_locale, cur_locale_args, cur_locale_special = I18N:getLocalesData()
local function xmlGet(source, tag)
local target = source
if get(self.td_target, source, tag) then
target = get(self.td_target, source, tag)
else
target = get(cur_locale, source, tag)
end
return source, tag, target, self.cur_section
end
for part_name, _ in pairs(parts) do
self:cleanTDef()
if part_name == "engine" then
self:loadDefinition(wp .. "_tdef_append.lua")
end
self:loadDefinition(ep .. part_name .. ".lua")
self:loadXml(xmlwp .. part_name .. ".xlf", true)
local translation_memory_list = {}
for _, section in ipairs(self.cur_file_sections) do
for _, t in ipairs(self.tdefs[section]) do
local source, tag = t.source, t.tag
local mark = source .. MARK_SEPRATOR .. tag
self.sec_tsmark[section][mark] = nil
self.global_tsmark[mark] = nil
local res = { xmlGet(source, tag) }
if get(self.td_target, source, tag) or _t(source, tag) ~= source then
print("[DEBUGGING] normal",tag, self.td_target, source)
translation_memory_list[#translation_memory_list + 1] = res
end
end
end
if #translation_memory_list > 0 then
if not fs.exists(wp .. "original/".. part_name .. ".xml") then
FSHelper.write_file(wp .. "original/".. part_name .. ".xml", "\n")
end
if not fs.exists(wp .. "trans/".. part_name .. ".xml") then
FSHelper.write_file(wp .. "trans/".. part_name .. ".xml", "\n")
end
local f = fs.open(wp .. "original/".. part_name .. ".xml", "w")
local fres, tres = PrintOriginal:getOriginalAndTranslationXml(translation_memory_list)
f:write(fres)
f:close()
f = fs.open(wp .. "trans/".. part_name .. ".xml", "w")
f:write(tres)
f:close()
end
end
self.logger:close()
self.logger = nil
if not silent then
Dialog:simplePopup(_t"Success", _t("Translation text printed in xml format."))
end
end
function _M:arranges(scan_only, silent)
self:cleanT()
local wp = FSHelper.locale_working_path
local ep = FSHelper.extracted_text_path
local xmlwp = FSHelper.locale_xml_working_path
self.logger = fs.open(wp .. "i18n.log", "w")
self:loadDefinition(wp .. "_t_append.lua")
self:loadDefinition(wp .. "_not_merged.lua")
@@ -316,7 +429,10 @@ function _M:arranges(scan_only, silent)
local parts = FSHelper.parts
for part_name, _ in pairs(parts) do
self:loadDefinition(wp .. part_name .. ".lua")
self:loadXml(xmlwp .. part_name .. ".xlf")
end
if scan_only then
self.logger:close()
self.logger = nil
@@ -347,14 +463,27 @@ function _M:arranges(scan_only, silent)
return ("t(%s, %s, %s)"):format(suitable_text(source), suitable_text(source), suitable_text(tag))
end
end
local function xmlGet(source, tag)
local target = source
if get(self.td_target, source, tag) then
target = get(self.td_target, source, tag)
else
target = get(cur_locale, source, tag)
end
return source, tag, target, self.cur_section
end
for part_name, _ in pairs(parts) do
self:cleanTDef()
if part_name == "engine" then
self:loadDefinition(wp .. "_tdef_append.lua")
end
self:loadDefinition(ep .. part_name .. ".lua")
self:loadXml(xmlwp .. part_name .. ".xlf", true)
FSHelper.copy_file(wp .. part_name .. ".lua", wp .. "backup/" .. part_name .. ".lua")
local f = fs.open(wp .. part_name .. ".lua", "w")
local to_translate_list = {}
local translation_memory_list = {}
local lore_memory_list = {}
for _, section in ipairs(self.cur_file_sections) do
local normal, untranslated, new_text, old_text = {}, {}, {}, {}
for _, t in ipairs(self.tdefs[section]) do
@@ -362,12 +491,24 @@ function _M:arranges(scan_only, silent)
local mark = source .. MARK_SEPRATOR .. tag
self.sec_tsmark[section][mark] = nil
self.global_tsmark[mark] = nil
if get(self.td_target, source, tag) then
local res = { xmlGet(source, tag) }
if get(self.td_target, source, tag) or _t(source, tag) ~= source then
normal[#normal+1] = tText(source, tag)
print("[DEBUGGING] normal",tag, self.td_target, source)
if not section:find("/lore/") then
translation_memory_list[#translation_memory_list + 1] = res
else
lore_memory_list[#lore_memory_list + 1] = res
end
-- to_translate_list[#to_translate_list + 1] = res
elseif t.isnew then
new_text[#new_text+1] = tText(source, tag)
print("[DEBUGGING] new",tag, self.td_target, source)
to_translate_list[#to_translate_list + 1] = res
else
untranslated[#untranslated+1] = tText(source, tag)
print("[DEBUGGING] un",tag, self.td_target, source)
to_translate_list[#to_translate_list + 1] = res
end
end
for _, mark in ipairs(self.sec_tsmark_list[section]) do
@@ -376,6 +517,11 @@ function _M:arranges(scan_only, silent)
local l, r = mark:find(MARK_SEPRATOR, 1, true)
local source, tag = mark:sub(1, l-1), mark:sub(r+1)
old_text[#old_text+1] = tText(source, tag)
if not section:find("/lore/") then
translation_memory_list[#translation_memory_list + 1] = { xmlGet(source, tag) }
else
lore_memory_list[#lore_memory_list + 1] = { xmlGet(source, tag) }
end
end
end
if normal[1] or new_text[1] or untranslated[1] or old_text[1] then
@@ -401,6 +547,31 @@ function _M:arranges(scan_only, silent)
end
end
f:close()
if #to_translate_list > 0 then
if not fs.exists(wp .. "omegaT-proj/source/".. part_name .. ".xlf") then
FSHelper.write_file(wp .. "omegaT-proj/source/".. part_name .. ".xlf", "\n")
end
f = fs.open(wp .. "omegaT-proj/source/".. part_name .. ".xlf", "w")
f:write(PrintXlf:toXlf(to_translate_list))
f:close()
end
--tm
if #translation_memory_list > 0 then
if not fs.exists(wp .. "omegaT-proj/tm/".. part_name .. ".tmx") then
FSHelper.write_file(wp .. "omegaT-proj/tm/".. part_name .. ".tmx", "\n")
end
f = fs.open(wp .. "omegaT-proj/tm/".. part_name .. ".tmx", "w")
f:write(PrintTmx:toTmx(translation_memory_list))
f:close()
end
if #lore_memory_list > 0 then
if not fs.exists(wp .. "omegaT-proj/tm/".. part_name .. "-lore" .. ".tmx") then
FSHelper.write_file(wp .. "omegaT-proj/tm/".. part_name .. "-lore".. ".tmx", "\n")
end
f = fs.open(wp .. "omegaT-proj/tm/".. part_name .. "-lore" .. ".tmx", "w")
f:write(PrintTmx:toTmx(lore_memory_list))
f:close()
end
end
local f = fs.open(wp .. "_not_merged.lua", "w")
f:write(('section ".not_merged"\n\n'))
Loading