• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sile-typesetter / sile / 13989845427

21 Mar 2025 10:15AM UTC coverage: 32.874% (+2.1%) from 30.742%
13989845427

push

github

alerque
chore(tooling): Hard code spelling fix to keep changelog chugging

6628 of 20162 relevant lines covered (32.87%)

1147.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

55.0
/core/languages.lua
1
--- SILE language class.
2
-- @interfaces languages
3

4
local cldr = require("cldr")
83✔
5
local loadkit = require("loadkit")
83✔
6
local setenv = require("rusile").setenv
83✔
7

8
loadkit.register("ftl", function (file)
166✔
9
   local contents = assert(file:read("*a"))
134✔
10
   file:close()
134✔
11
   return assert(fluent:add_messages(contents))
268✔
12
end)
13

14
SILE.scratch.loaded_languages = {}
83✔
15

16
SILE.languageSupport = {
83✔
17
   languages = {},
83✔
18
   loadLanguage = function (language)
19
      language = language or SILE.settings:get("document.language")
2,772✔
20
      language = cldr.locales[language] and language or "und"
2,772✔
21
      if SILE.scratch.loaded_languages[language] then
2,772✔
22
         return
2,638✔
23
      end
24
      SILE.scratch.loaded_languages[language] = true
134✔
25
      local langresource = string.format("languages.%s", language)
134✔
26
      local gotlang, lang = pcall(require, langresource)
134✔
27
      if not gotlang then
134✔
28
         SU.warn(
×
29
            ("Unable to load language feature support (e.g. hyphenation rules) for %s: %s"):format(
×
30
               language,
31
               lang:gsub(":.*", "")
×
32
            )
33
         )
34
      end
35
      local ftlresource = string.format("languages.%s.messages", language)
134✔
36
      SU.debug("fluent", "Loading FTL resource", ftlresource, "into locale", language)
134✔
37
      -- This needs to be set so that we load localizations into the right bundle,
38
      -- but this breaks the sync enabled by the hook in the document.language
39
      -- setting, so we want to set it back when we're done.
40
      local original_language = fluent:get_locale()
134✔
41
      fluent:set_locale(language)
134✔
42
      local gotftl, ftl = pcall(require, ftlresource)
134✔
43
      if not gotftl then
134✔
44
         SU.warn(
×
45
            ("Unable to load localized strings (e.g. table of contents header text) for %s: %s"):format(
×
46
               language,
47
               ftl:gsub(":.*", "")
×
48
            )
49
         )
50
      end
51
      if type(lang) == "table" and lang.init then
134✔
52
         lang.init()
1✔
53
      end
54
      fluent:set_locale(original_language)
134✔
55
   end,
56
}
83✔
57

58
SILE.settings:declare({
166✔
59
   parameter = "document.language",
60
   type = "string",
61
   default = "en",
62
   hook = function (language)
63
      SILE.languageSupport.loadLanguage(language)
474✔
64
      fluent:set_locale(language)
474✔
65
      os.setlocale(language)
474✔
66
      setenv("LANG", language)
474✔
67
   end,
68
   help = "Locale for localized language support",
69
})
70

71
SILE.registerCommand("language", function (options, content)
166✔
72
   local main = SU.required(options, "main", "language setting")
4✔
73
   SILE.languageSupport.loadLanguage(main)
4✔
74
   if content[1] then
4✔
75
      SILE.settings:temporarily(function ()
×
76
         SILE.settings:set("document.language", main)
×
77
         SILE.process(content)
×
78
      end)
79
   else
80
      SILE.settings:set("document.language", main)
4✔
81
   end
82
end, nil, nil, true)
87✔
83

84
SILE.registerCommand("fluent", function (options, content)
166✔
85
   local key = content[1]
×
86
   local locale = options.locale or SILE.settings:get("document.language")
×
87
   local original_locale = fluent:get_locale()
×
88
   fluent:set_locale(locale)
×
89
   SU.debug("fluent", "Looking for", key, "in", locale)
×
90
   local entry
91
   if key then
×
92
      entry = fluent:get_message(key)
×
93
   else
94
      SU.warn("Fluent localization function called without passing a valid message id")
×
95
   end
96
   local message
97
   if entry then
×
98
      message = entry:format(options)
×
99
   else
100
      SU.warn(string.format("No localized message for %s found in locale %s", key, locale))
×
101
      fluent:set_locale("und")
×
102
      entry = fluent:get_message(key)
×
103
      if entry then
×
104
         message = entry:format(options)
×
105
      end
106
   end
107
   fluent:set_locale(original_locale)
×
108
   SILE.processString(("<sile>%s</sile>"):format(message), "xml")
×
109
end, nil, nil, true)
83✔
110

111
SILE.registerCommand("ftl", function (options, content)
166✔
112
   local original_locale = fluent:get_locale()
×
113
   local locale = options.locale or SILE.settings:get("document.language")
×
114
   SU.debug("fluent", "Loading message(s) into locale", locale)
×
115
   fluent:set_locale(locale)
×
116
   if options.src then
×
117
      fluent:load_file(options.src, locale)
×
118
   elseif SU.ast.hasContent(content) then
×
119
      local input = content[1]
×
120
      fluent:add_messages(input, locale)
×
121
   end
122
   fluent:set_locale(original_locale)
×
123
end, nil, nil, true)
83✔
124

125
require("languages.unicode")
83✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc