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

sile-typesetter / sile / 14390735465

10 Apr 2025 09:30PM UTC coverage: 34.559% (-31.7%) from 66.23%
14390735465

push

github

alerque
Merge tag 'v0.15.12' into develop

4 of 4 new or added lines in 3 files covered. (100.0%)

4064 existing lines in 73 files now uncovered.

6918 of 20018 relevant lines covered (34.56%)

2597.26 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,015✔
13
      language = cldr.locales[language] and language or "und"
8,015✔
14
      if SILE.scratch.loaded_languages[language] then
8,015✔
15
         return
7,687✔
16
      end
17
      SILE.scratch.loaded_languages[language] = true
328✔
18
      local langresource = string.format("languages.%s", language)
328✔
19
      local gotlang, lang = pcall(require, langresource)
328✔
20
      if not gotlang then
328✔
UNCOV
21
         SU.warn(
×
UNCOV
22
            ("Unable to load language feature support (e.g. hyphenation rules) for %s: %s"):format(
×
23
               language,
UNCOV
24
               lang:gsub(":.*", "")
×
25
            )
26
         )
27
      end
28
      local ftlresource = string.format("languages.%s.messages", language)
328✔
29
      SU.debug("fluent", "Loading FTL resource", ftlresource, "into locale", language)
328✔
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()
328✔
34
      fluent:set_locale(language)
328✔
35
      local gotftl, ftl = pcall(require, ftlresource)
328✔
36
      if not gotftl then
328✔
UNCOV
37
         SU.warn(
×
UNCOV
38
            ("Unable to load localized strings (e.g. table of contents header text) for %s: %s"):format(
×
39
               language,
UNCOV
40
               ftl:gsub(":.*", "")
×
41
            )
42
         )
43
      end
44
      if type(lang) == "table" and lang.init then
328✔
45
         lang.init()
5✔
46
      end
47
      fluent:set_locale(original_language)
328✔
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)
693✔
57
      fluent:set_locale(language)
693✔
58
      os.setlocale(language)
693✔
59
      setenv("LANG", language)
693✔
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")
19✔
66
   SILE.languageSupport.loadLanguage(main)
19✔
67
   if content[1] then
19✔
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)
17✔
74
   end
75
end, nil, nil, true)
214✔
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
UNCOV
93
      SU.warn(string.format("No localized message for %s found in locale %s", key, locale))
×
UNCOV
94
      fluent:set_locale("und")
×
UNCOV
95
      entry = fluent:get_message(key)
×
UNCOV
96
      if entry then
×
UNCOV
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✔
UNCOV
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