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

sile-typesetter / sile / 14727381542

29 Apr 2025 06:54AM UTC coverage: 32.062% (+0.3%) from 31.768%
14727381542

push

github

alerque
chore(shapers): Remove workarounds for HB versions we haven't supported in ages

6464 of 20161 relevant lines covered (32.06%)

2446.39 hits per line

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

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

4
local cldr = require("cldr")
195✔
5
local setenv = require("rusile").setenv
195✔
6

7
SILE.scratch.loaded_languages = {}
195✔
8

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

51
SILE.settings:declare({
390✔
52
   parameter = "document.language",
53
   type = "string",
54
   default = "en",
55
   hook = function (language)
56
      SILE.languageSupport.loadLanguage(language)
684✔
57
      fluent:set_locale(language)
684✔
58
      os.setlocale(language)
684✔
59
      setenv("LANG", language)
684✔
60
   end,
61
   help = "Locale for localized language support",
62
})
63

64
SILE.registerCommand("language", function (options, content)
390✔
65
   local main = SU.required(options, "main", "language setting")
18✔
66
   SILE.languageSupport.loadLanguage(main)
18✔
67
   if content[1] then
18✔
68
      SILE.settings:temporarily(function ()
4✔
69
         SILE.settings:set("document.language", main)
2✔
70
         SILE.process(content)
2✔
71
      end)
72
   else
73
      SILE.settings:set("document.language", main)
16✔
74
   end
75
end, nil, nil, true)
213✔
76

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

104
SILE.registerCommand("ftl", function (options, content)
390✔
105
   local original_locale = fluent:get_locale()
1✔
106
   local locale = options.locale or SILE.settings:get("document.language")
1✔
107
   SU.debug("fluent", "Loading message(s) into locale", locale)
1✔
108
   fluent:set_locale(locale)
1✔
109
   if options.src then
1✔
110
      fluent:load_file(options.src, locale)
×
111
   elseif SU.ast.hasContent(content) then
2✔
112
      local input = content[1]
1✔
113
      fluent:add_messages(input, locale)
1✔
114
   end
115
   fluent:set_locale(original_locale)
1✔
116
end, nil, nil, true)
196✔
117

118
require("languages.unicode")
195✔
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

© 2025 Coveralls, Inc