Skip to content

Commit 283fdc3

Browse files
committed
fix(languages): Always set Fluent locale when setting document.language
1 parent d42ef0c commit 283fdc3

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

core/languages.lua

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ SILE.languageSupport = {
3333
end
3434
local ftlresource = string.format("i18n.%s", language)
3535
SU.debug("fluent", "Loading FTL resource", ftlresource, "into locale", language)
36+
-- This needs to be set so that we load localizations into the right bundle,
37+
-- but this breaks the sync enabled by the hook in the document.language
38+
-- setting, so we want to set it back when we're done.
39+
local original_language = fluent:get_locale()
3640
fluent:set_locale(language)
3741
local gotftl, ftl = pcall(require, ftlresource)
3842
if not gotftl then
@@ -46,6 +50,7 @@ SILE.languageSupport = {
4650
if type(lang) == "table" and lang.init then
4751
lang.init()
4852
end
53+
fluent:set_locale(original_language)
4954
end,
5055
}
5156

@@ -65,10 +70,11 @@ end, nil, nil, true)
6570
SILE.registerCommand("fluent", function (options, content)
6671
local key = content[1]
6772
local locale = options.locale or SILE.settings:get("document.language")
73+
local original_locale = fluent:get_locale()
74+
fluent:set_locale(locale)
6875
SU.debug("fluent", "Looking for", key, "in", locale)
6976
local entry
7077
if key then
71-
fluent:set_locale(locale)
7278
entry = fluent:get_message(key)
7379
else
7480
SU.warn("Fluent localization function called without passing a valid message id")
@@ -83,12 +89,13 @@ SILE.registerCommand("fluent", function (options, content)
8389
if entry then
8490
message = entry:format(options)
8591
end
86-
fluent:set_locale(locale)
8792
end
93+
fluent:set_locale(original_locale)
8894
SILE.processString(("<sile>%s</sile>"):format(message), "xml")
8995
end, nil, nil, true)
9096

9197
SILE.registerCommand("ftl", function (options, content)
98+
local original_locale = fluent:get_locale()
9299
local locale = options.locale or SILE.settings:get("document.language")
93100
SU.debug("fluent", "Loading message(s) into locale", locale)
94101
fluent:set_locale(locale)
@@ -98,6 +105,7 @@ SILE.registerCommand("ftl", function (options, content)
98105
local input = content[1]
99106
fluent:add_messages(input, locale)
100107
end
108+
fluent:set_locale(original_locale)
101109
end, nil, nil, true)
102110

103111
require("languages.unicode")

core/settings.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ function settings:_init ()
1919
parameter = "document.language",
2020
type = "string",
2121
default = "en",
22+
hook = function (language)
23+
fluent:set_locale(language)
24+
end,
2225
help = "Locale for localized language support",
2326
})
2427

0 commit comments

Comments
 (0)