Skip to content

Commit

Permalink
fix(languages): Always set Fluent locale when setting `document.langu…
Browse files Browse the repository at this point in the history
…age`
  • Loading branch information
alerque committed Jun 12, 2024
1 parent d42ef0c commit 283fdc3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
12 changes: 10 additions & 2 deletions core/languages.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 33,10 @@ SILE.languageSupport = {
end
local ftlresource = string.format("i18n.%s", language)
SU.debug("fluent", "Loading FTL resource", ftlresource, "into locale", language)
-- This needs to be set so that we load localizations into the right bundle,
-- but this breaks the sync enabled by the hook in the document.language
-- setting, so we want to set it back when we're done.
local original_language = fluent:get_locale()
fluent:set_locale(language)
local gotftl, ftl = pcall(require, ftlresource)
if not gotftl then
Expand All @@ -46,6 50,7 @@ SILE.languageSupport = {
if type(lang) == "table" and lang.init then
lang.init()
end
fluent:set_locale(original_language)
end,
}

Expand All @@ -65,10 70,11 @@ end, nil, nil, true)
SILE.registerCommand("fluent", function (options, content)
local key = content[1]
local locale = options.locale or SILE.settings:get("document.language")
local original_locale = fluent:get_locale()
fluent:set_locale(locale)
SU.debug("fluent", "Looking for", key, "in", locale)
local entry
if key then
fluent:set_locale(locale)
entry = fluent:get_message(key)
else
SU.warn("Fluent localization function called without passing a valid message id")
Expand All @@ -83,12 89,13 @@ SILE.registerCommand("fluent", function (options, content)
if entry then
message = entry:format(options)
end
fluent:set_locale(locale)
end
fluent:set_locale(original_locale)
SILE.processString(("<sile>%s</sile>"):format(message), "xml")
end, nil, nil, true)

SILE.registerCommand("ftl", function (options, content)
local original_locale = fluent:get_locale()
local locale = options.locale or SILE.settings:get("document.language")
SU.debug("fluent", "Loading message(s) into locale", locale)
fluent:set_locale(locale)
Expand All @@ -98,6 105,7 @@ SILE.registerCommand("ftl", function (options, content)
local input = content[1]
fluent:add_messages(input, locale)
end
fluent:set_locale(original_locale)
end, nil, nil, true)

require("languages.unicode")
Expand Down
3 changes: 3 additions & 0 deletions core/settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 19,9 @@ function settings:_init ()
parameter = "document.language",
type = "string",
default = "en",
hook = function (language)
fluent:set_locale(language)
end,
help = "Locale for localized language support",
})

Expand Down

0 comments on commit 283fdc3

Please sign in to comment.