Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update tool to generate metadata translations #11869

Merged
merged 9 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion metadata/en-US/full_description.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<p>KOReader is an ebook reader optimized for e-ink screens. It can open many formats and provides advanced text adjustments.</p>
<p>See below for a selection of its many features:</p>
<ul>
<li>Supports both fixed page formats (PDF, DjVu, CBT, CBZ) and reflowable e-book formats (EPUB, FB2, Mobi, DOC, CHM, TXT, HTML). Scanned PDF/DjVu documents can be reflowed. Special flow directions for reading double column PDFs and manga.</li>
<li>Supports both fixed page formats (PDF, DjVu, CBT, CBZ) and reflowable e-book formats (EPUB, FB2, Mobi, DOC, CHM, TXT, HTML). Scanned PDF&#47;DjVu documents can be reflowed. Special flow directions for reading double column PDFs and manga.</li>
<li>Multi-lingual user interface optimized for e-ink screens. Highly customizable reader view with complete typesetting options. Multi-lingual hyphenation dictionaries are bundled in.</li>
<li>Non-Latin script support for books, including the Hebrew, Arabic, Persian, Russian, Chinese, Japanese and Korean languages.</li>
<li>Unique Book Map and Page Browser features to navigate your book.</li>
Expand Down
8 changes: 4 additions & 4 deletions platform/common/koreader.metainfo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
<p>KOReader is an ebook reader optimized for e-ink screens. It can open many formats and provides advanced text adjustments.</p>
<p>See below for a selection of its many features:</p>
<ul>
<li>Supports both fixed page formats (PDF, DjVu, CBT, CBZ) and reflowable e-book formats (EPUB, FB2, Mobi, DOC, CHM, TXT, HTML). Scanned PDF/DjVu documents can be reflowed. Special flow directions for reading double column PDFs and manga.</li>
<li>Supports both fixed page formats (PDF, DjVu, CBT, CBZ) and reflowable e-book formats (EPUB, FB2, Mobi, DOC, CHM, TXT, HTML). Scanned PDF&#47;DjVu documents can be reflowed. Special flow directions for reading double column PDFs and manga.</li>
<li>Multi-lingual user interface optimized for e-ink screens. Highly customizable reader view with complete typesetting options. Multi-lingual hyphenation dictionaries are bundled in.</li>
<li>Non-Latin script support for books, including the Hebrew, Arabic, Persian, Russian, Chinese, Japanese and Korean languages.</li>
<li>Unique Book Map and Page Browser features to navigate your book.</li>
Expand All @@ -107,13 +107,13 @@
</description>
<screenshots>
<screenshot type="default">
<image>https://github.com/koreader/koreader-artwork/raw/master/koreader-menu-framed.png</image>
<image>https:&#47;&#47;github.com&#47;koreader&#47;koreader-artwork&#47;raw&#47;master&#47;koreader-menu-framed.png</image>
pazos marked this conversation as resolved.
Show resolved Hide resolved
</screenshot>
<screenshot>
<image>https://github.com/koreader/koreader-artwork/raw/master/koreader-footnotes-framed.png</image>
<image>https:&#47;&#47;github.com&#47;koreader&#47;koreader-artwork&#47;raw&#47;master&#47;koreader-footnotes-framed.png</image>
</screenshot>
<screenshot>
<image>https://github.com/koreader/koreader-artwork/raw/master/koreader-dictionary-framed.png</image>
<image>https:&#47;&#47;github.com&#47;koreader&#47;koreader-artwork&#47;raw&#47;master&#47;koreader-dictionary-framed.png</image>
</screenshot>
</screenshots>
<keywords>
Expand Down
154 changes: 69 additions & 85 deletions tools/update-metadata.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
#!/usr/bin/env luajit

--[[
Tool to generate localized metadata that produces
appstream and fastlane compatible files.
This tool generates localized metadata for application stores.
We currently support F-Droid(fastlane) and Flathub(appstream).

usage: ./tools/update_metadata.lua
]]--

package.path = "frontend/?.lua;base/" .. package.path
local _ = require("gettext")

local updated_files = {}

-- we can't require util here, some C libraries might not be available
local function htmlEscape(text)
return text:gsub("[}{\">/<'&]", {
Expand All @@ -24,67 +22,6 @@ local function htmlEscape(text)
})
end

local function isFile(str)
local f = io.open(str, "r")
if f then
f:close()
return true
end
return false
end

local function writeFile(str, path)
local f = io.open(path, "w")
table.insert(updated_files, path)
if f then
f:write(str)
f:write("\n")
f:close()
end
end

local function isLocaleDir(str)
for _, v in ipairs({"templates", "README.md", "LICENSE"}) do
if str == v then return false end
end
return true
end

local function getLocales()
local locales = {}
local output = io.popen('ls l10n')
if not output then return {} end
for dir in output:lines() do
if isLocaleDir(dir) then
locales[#locales + 1] = dir
end
end
output:close()
table.insert(locales, 1, "en")
return locales
end



local function tag(element, lang, str, pad)
assert(type(element) == "string", "element must be a string")
assert(type(lang) == "string", "lang must be a string")
assert(type(str) == "string", "str must be a string")
local offset = ""
local pad = pad or 0
if pad >= 1 then
for i = 1, pad do
offset = offset .. " "
end
end
_.changeLang(lang)
if lang == "en" then
return offset .. '<' .. element .. '>' .. _(str) .. '</' .. element .. '>'
else
return offset .. '<' .. element .. ' xml:lang="' .. lang .. '">' .. _(str) .. '</' .. element .. '>'
end
end

local metadata = {
summary = _("Ebook reader"),
desc = {
Expand Down Expand Up @@ -128,7 +65,7 @@ local metadata = {
"djvu",
},

-- appstream metadata don't need to be translated
-- appstream metadata that needs no translation
component = [[
<id>rocks.koreader.KOReader</id>

Expand Down Expand Up @@ -222,12 +159,40 @@ local metadata = {
]],
}

local function summary(lang)
local lang = lang or "en"
_.changeLang(lang)
return _(metadata.summary)
local updated_files = {}
local function writeFile(str, path)
local f = io.open(path, "w")
table.insert(updated_files, path)
if f then
f:write(str)
f:write("\n")
f:close()
end
end

local function isLocaleDir(str)
for _, v in ipairs({"templates", "README.md", "LICENSE"}) do
if str == v then return false end
end
return true
end

local function getLocales()
local locales = {}
local output = io.popen('ls l10n')
if not output then return {} end
for dir in output:lines() do
if isLocaleDir(dir) then
locales[#locales + 1] = dir
end
end
output:close()
table.insert(locales, 1, "en")
return locales
end

local locales = getLocales()

local function htmlDescription(lang)
local lang = lang or "en"
local desc = metadata.desc
Expand All @@ -236,20 +201,34 @@ local function htmlDescription(lang)
for i, v in ipairs (desc.paragraphs) do
table.insert(t, "<p>" .. htmlEscape(_(v)) .. "</p>")
end

table.insert(t, "<ul>")
for i, v in ipairs(desc.highlights) do
table.insert(t, " <li>" .. _(v) .. "</li>")
table.insert(t, " <li>" .. htmlEscape(_(v)) .. "</li>")
end
table.insert(t, "</ul>")
return table.concat(t, "\n")
end

local locales = getLocales()
local function tag(element, lang, str, pad)
local offset = ""
local pad = pad or 0
if pad >= 1 then
for i = 1, pad do
offset = offset .. " "
end
end
if lang == "en" then
return string.format("%s<%s>%s</%s>",
offset, element, htmlEscape(str), element)
else
return string.format('%s<%s xml:lang="%s">%s</%s>',
offset, element, lang, htmlEscape(str), element)
end
end

local function genAppstream()
local metadata_file = "platform/common/koreader.metainfo.xml"
print("Building appstream metadata")
print("Building appstream metadata, this might take a while...")
local t = {}
local desc = metadata.desc
table.insert(t, '<?xml version="1.0" encoding="UTF-8"?>')
Expand All @@ -263,7 +242,7 @@ local function genAppstream()
_.changeLang(lang)
translated = _(metadata.summary)
if orig ~= translated or lang == "en" then
table.insert(t, tag("summary", lang, _(metadata.summary), 2))
table.insert(t, tag("summary", lang, translated, 2))
end
end
table.insert(t, ' <description>')
Expand Down Expand Up @@ -353,20 +332,25 @@ end

local function genFastlane()
print("Building fastlane metadata")


local short, full = "short_description.txt", "full_description.txt"
local short_orig, full_orig = summary(), htmlDescription()
local short_orig = metadata.summary
local full_orig = htmlDescription()
local short_translated, full_translated
local metadata_file
local metadata_dir = "metadata/en-US/"
metadata_file = metadata_dir .. short
writeFile(short_orig, metadata_file)
metadata_file = metadata_dir .. full
writeFile(full_orig, metadata_file)

for __, lang in ipairs(locales) do
if lang ~= "en" then
short_translated = summary(lang)
full_translated = htmlDescription(lang)
_.changeLang(lang)
if lang == "en" then
metadata_dir = "metadata/en-US/"
metadata_file = metadata_dir .. short
writeFile(short_orig, metadata_file)
metadata_file = metadata_dir .. full
writeFile(full_orig, metadata_file)
else
metadata_dir = "metadata/" .. lang .. "/"
short_translated = _(metadata.summary)
full_translated = htmlDescription(lang)
if short_orig ~= short_translated or full_orig ~= full_translated then
os.execute('mkdir -p ' .. metadata_dir)
end
Expand Down
Loading