From 239f488eec7e2fa7aab86a6f5a676ceaefa63a91 Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Mon, 18 Apr 2022 21:47:19 +0300 Subject: [PATCH 1/7] chore(languages): Correct failure modes for when loading FTL files fail --- core/languages.lua | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/core/languages.lua b/core/languages.lua index 2e5fa4cf1..8c5b94217 100644 --- a/core/languages.lua +++ b/core/languages.lua @@ -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 = { @@ -13,18 +13,20 @@ 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() From 557cf4232386d9c5a3c9df565d98ae37fa3a026f Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Mon, 18 Apr 2022 18:58:49 +0300 Subject: [PATCH 2/7] chore(tooling): Add scope for localization related commits --- .commitlintrc.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.commitlintrc.yml b/.commitlintrc.yml index 003bc78d1..91ad42de1 100644 --- a/.commitlintrc.yml +++ b/.commitlintrc.yml @@ -38,6 +38,7 @@ rules: - inputs - installation - languages + - l10n - manpage - manual - math From d98c3973bfb3b853d63bf19256b2afeb51e62e29 Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Mon, 18 Apr 2022 18:53:49 +0300 Subject: [PATCH 3/7] feat(cli): Add `-l` / `--locale` flag to set SILE interface locale --- core/sile.lua | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/core/sile.lua b/core/sile.lua index ae693e2d8..ee7161684 100644 --- a/core/sile.lua +++ b/core/sile.lua @@ -127,6 +127,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") @@ -153,6 +154,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 From dc0d8d2da9cf2fc5cc72ed750f0d59b72cee747d Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Mon, 18 Apr 2022 18:54:50 +0300 Subject: [PATCH 4/7] docs(manpage): Document new CLI locale option --- sile.1.in | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sile.1.in b/sile.1.in index 4cac0440a..fab689014 100644 --- a/sile.1.in +++ b/sile.1.in @@ -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. From 10167e066945afe26563c332de15d9075a0d093d Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Mon, 18 Apr 2022 18:55:32 +0300 Subject: [PATCH 5/7] chore(l10n): Load resource file with localized SILE UI strings --- core/languages.lua | 16 ++++++++++++++++ core/sile.lua | 3 ++- sile.in | 6 ++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/core/languages.lua b/core/languages.lua index 8c5b94217..372f64770 100644 --- a/core/languages.lua +++ b/core/languages.lua @@ -34,6 +34,22 @@ SILE.languageSupport = { 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) diff --git a/core/sile.lua b/core/sile.lua index ee7161684..17e172818 100644 --- a/core/sile.lua +++ b/core/sile.lua @@ -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")() diff --git a/sile.in b/sile.in index 65452416b..a717a182f 100755 --- a/sile.in +++ b/sile.in @@ -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') From e3c9a3ca0edf6c6bc96695bcc6cdd97624fe7f08 Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Mon, 18 Apr 2022 18:53:01 +0300 Subject: [PATCH 6/7] chore(l10n): Move SILE UI strings from code to Fluent locale --- l10n/en.ftl | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 l10n/en.ftl diff --git a/l10n/en.ftl b/l10n/en.ftl new file mode 100644 index 000000000..921327bd5 --- /dev/null +++ b/l10n/en.ftl @@ -0,0 +1,3 @@ +full-version = SILE { $a } + +loading = Loading { $a } From 3354bf19a0c9f6a9c8a0e931f3b425777491b024 Mon Sep 17 00:00:00 2001 From: Caleb Maclennan Date: Tue, 19 Apr 2022 01:51:25 +0300 Subject: [PATCH 7/7] NFY: Start work on howe to pack/unpack such that unused args won't be processed --- core/measurement.lua | 1 + core/utilities.lua | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/core/measurement.lua b/core/measurement.lua index 95ffa1c3b..01e4ab6a3 100644 --- a/core/measurement.lua +++ b/core/measurement.lua @@ -84,6 +84,7 @@ local measurement = pl.class({ end, __tostring = function (self) + SU.debug("l10n", "measurement to string") return self.amount .. self.unit end, diff --git a/core/utilities.lua b/core/utilities.lua index 226567562..810aaf459 100644 --- a/core/utilities.lua +++ b/core/utilities.lua @@ -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