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

Setup SILE interface to be localized #1374

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
1 change: 1 addition & 0 deletions .commitlintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ rules:
- inputs
- installation
- languages
- l10n
- manpage
- manual
- math
Expand Down
34 changes: 26 additions & 8 deletions core/languages.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ local loadkit = require("loadkit")
loadkit.register("ftl", function (file)
local contents = assert(file:read("*a"))
file:close()
return assert(SILE.fluent:add_messages(contents))
return contents
end)

SILE.languageSupport = {
Expand All @@ -13,25 +13,43 @@ SILE.languageSupport = {
language = SILE.cldr.locales[language] and language or "und"
if SILE.languageSupport.languages[language] then return end
if SILE.hyphenator.languages[language] then return end
local lang, fail = pcall(function () SILE.require("languages/" .. language) end)
if fail then
if fail:match("not found") then fail = "no support for this language" end
SU.warn("Error loading language " .. language .. ": " .. fail)
local ret1, lang = pcall(SILE.require, "languages/" .. language)
if not ret1 then
if lang:match("not found") then lang = "no support for this language" end
SU.warn("Error loading language " .. language .. ": " .. lang)
SILE.languageSupport.languages[language] = {} -- Don't try again
end
local ftlresource = string.format("i18n.%s", language)
SU.debug("fluent", "Loading FTL resource", ftlresource, "into locale", language)
SILE.fluent:set_locale(language)
local _, ftlfail = pcall(function () return require(ftlresource) end)
if not ftlfail then
SU.warn("Error loading localizations " .. language .. ": " .. ftlfail)
local ret2, ftl = pcall(require, ftlresource)
if ret2 then
SILE.fluent:add_messages(ftl)
else
SU.warn("No document localizations found for " .. language .. ": " .. ftl)
end
if type(lang) == "table" and lang.init then
lang.init()
end
end
}

-- Function to load UI localizations for SILE itself. Also sets the default
-- document language, but this is primarily about the UI not the document.
SILE.set_locale = function (locale)
locale = SILE.cldr.locales[locale] and locale or "en"
SILE.languageSupport.loadLanguage(locale)
SILE.settings.set("document.language", locale, true)
SILE.l10n:set_locale(locale)
local ftlresource = string.format("l10n.%s", locale)
local ret, ftl = pcall(require, ftlresource)
if ret then
SILE.l10n:add_messages(ftl)
else
SU.warn("Error loading UI localizations " .. locale .. ": " .. ftl)
end
end

SILE.registerCommand("language", function (options, content)
local main = SU.required(options, "main", "language setting")
SILE.languageSupport.loadLanguage(main)
Expand Down
1 change: 1 addition & 0 deletions core/measurement.lua
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ local measurement = pl.class({
end,

__tostring = function (self)
SU.debug("l10n", "measurement to string")
return self.amount .. self.unit
end,

Expand Down
9 changes: 8 additions & 1 deletion core/sile.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ SILE.preamble = {}

-- Internal functions / classes / factories
SILE.cldr = require("cldr")
SILE.fluent = require("fluent")()
SILE.fluent = require("fluent")() -- for document localizations
SILE.l10n = require("fluent")() -- for UI localizations
SILE.utilities = require("core/utilities")
SU = SILE.utilities -- alias
SILE.traceStack = require("core/tracestack")()
Expand Down Expand Up @@ -127,6 +128,7 @@ SILE.parseArguments = function ()
cli:splat("INPUT", "input file, SIL or XML format")
cli:option("-b, --backend=VALUE", "choose an alternative output backend")
cli:option("-d, --debug=VALUE", "show debug information for tagged aspects of SILE’s operation", {})
cli:option("-l, --locale=VALUE", "locale for SILE interface and default document language", {})
cli:option("-e, --evaluate=VALUE", "evaluate some Lua code before processing file", {})
cli:option("-f, --fontmanager=VALUE", "choose an alternative font manager")
cli:option("-m, --makedeps=FILE", "generate a list of dependencies in Makefile format")
Expand All @@ -153,6 +155,11 @@ SILE.parseArguments = function ()
SILE.masterFilename = string.match(SILE.inputFile, "(.+)%..-$") or SILE.inputFile
SILE.masterDir = SILE.masterFilename:match("(.-)[^%/]+$")
end
if opts.locale then
for _, locale in ipairs(opts.locale) do
SILE.set_locale(locale)
end
end
if opts.backend then
SILE.backend = opts.backend
end
Expand Down
19 changes: 19 additions & 0 deletions core/utilities.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@ utilities.warn = function(message, bug)
io.stderr:write("\n")
end

utilities.info = function(message, ...)
SU.msg(message, ...)
end

utilities.msg = function(message, ...)
local inputs = pl.utils.pack(...)
for i, v in ipairs(inputs) do
inputs[i] = nil
inputs[string.char(96+i)] = tostring(v)
end
local msg = SILE.l10n[message](inputs)
SU.debug("l10n", msg)
-- SU.dump{ message, callback }
-- local arg = { ... } -- Avoid things that Lua stuffs in arg like args to self()
-- pl.pretty.dump(#arg == 1 and arg[1] or arg, "/dev/stderr")
-- local inputs = table.pack(...)
-- for i, input in ipairs(inputs) do
end

utilities.debugging = function (category)
return SILE.debugFlags.all or SILE.debugFlags[category]
end
Expand Down
3 changes: 3 additions & 0 deletions l10n/en.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
full-version = SILE { $a }

loading = Loading { $a }
4 changes: 4 additions & 0 deletions sile.1.in
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ Print help message and exit.
.BR \-v ", " \-\-version
Print version information and exit.
.TP
.BR \-l ", " \-\-locale= \fIvalue\fR
Interface locale for localized messages.
Also sets the default document language.
.TP
.BR \-b ", " \-\-backend= \fIvalue\fR
Choose an alternative output backend.
The default backend for producing PDF files is \fIlibtexpdf\fR.
Expand Down
6 changes: 6 additions & 0 deletions sile.in
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ extendPath(".")
SILE = require("core/sile")
SILE.version = "v@VERSION@"
io.stdout:setvbuf 'no'

-- Load at least a default locale, may be overridden shortly when we parse arguments
-- but we will always want to be able to fall back to these localizations for any
-- messages that are not localized anyway so this is not a waste.
SILE.set_locale("en")

SILE.parseArguments()
if not os.getenv 'LUA_REPL_RLWRAP' then
io.stderr:write(SILE.full_version .. '\n')
Expand Down