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

sile-typesetter / sile / 9322841962

31 May 2024 06:41PM UTC coverage: 47.763% (-8.9%) from 56.674%
9322841962

push

github

web-flow
Merge 71aa6f2a1 into bb89a7e79

8081 of 16919 relevant lines covered (47.76%)

246.83 hits per line

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

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

4
local loadkit = require("loadkit")
7✔
5
local cldr = require("cldr")
7✔
6

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

13
local loadonce = {}
7✔
14

15
SILE.languageSupport = {
7✔
16
   languages = {},
7✔
17
   loadLanguage = function (language)
18
      language = language or SILE.settings:get("document.language")
143✔
19
      language = cldr.locales[language] and language or "und"
143✔
20
      if loadonce[language] then
143✔
21
         return
132✔
22
      end
23
      loadonce[language] = true
11✔
24
      local langresource = string.format("languages.%s", language)
11✔
25
      local gotlang, lang = pcall(require, langresource)
11✔
26
      if not gotlang then
11✔
27
         SU.warn(
×
28
            ("Unable to load language feature support (e.g. hyphenation rules) for %s: %s"):format(
×
29
               language,
30
               lang:gsub(":.*", "")
×
31
            )
32
         )
33
      end
34
      local ftlresource = string.format("i18n.%s", language)
11✔
35
      SU.debug("fluent", "Loading FTL resource", ftlresource, "into locale", language)
11✔
36
      fluent:set_locale(language)
11✔
37
      local gotftl, ftl = pcall(require, ftlresource)
11✔
38
      if not gotftl then
11✔
39
         SU.warn(
×
40
            ("Unable to load localized strings (e.g. table of contents header text) for %s: %s"):format(
×
41
               language,
42
               ftl:gsub(":.*", "")
×
43
            )
44
         )
45
      end
46
      if type(lang) == "table" and lang.init then
11✔
47
         lang.init()
×
48
      end
49
   end,
50
}
7✔
51

52
SILE.registerCommand("language", function (options, content)
14✔
53
   local main = SU.required(options, "main", "language setting")
×
54
   SILE.languageSupport.loadLanguage(main)
×
55
   if content[1] then
×
56
      SILE.settings:temporarily(function ()
×
57
         SILE.settings:set("document.language", main)
×
58
         SILE.process(content)
×
59
      end)
60
   else
61
      SILE.settings:set("document.language", main)
×
62
   end
63
end, nil, nil, true)
7✔
64

65
SILE.registerCommand("fluent", function (options, content)
14✔
66
   local key = content[1]
×
67
   local locale = options.locale or SILE.settings:get("document.language")
×
68
   SU.debug("fluent", "Looking for", key, "in", locale)
×
69
   local entry
70
   if key then
×
71
      fluent:set_locale(locale)
×
72
      entry = fluent:get_message(key)
×
73
   else
74
      SU.warn("Fluent localization function called without passing a valid message id")
×
75
   end
76
   local message
77
   if entry then
×
78
      message = entry:format(options)
×
79
   else
80
      SU.warn(string.format("No localized message for %s found in locale %s", key, locale))
×
81
      fluent:set_locale("und")
×
82
      entry = fluent:get_message(key)
×
83
      if entry then
×
84
         message = entry:format(options)
×
85
      end
86
      fluent:set_locale(locale)
×
87
   end
88
   SILE.processString(("<sile>%s</sile>"):format(message), "xml")
×
89
end, nil, nil, true)
7✔
90

91
SILE.registerCommand("ftl", function (options, content)
14✔
92
   local locale = options.locale or SILE.settings:get("document.language")
×
93
   SU.debug("fluent", "Loading message(s) into locale", locale)
×
94
   fluent:set_locale(locale)
×
95
   if options.src then
×
96
      fluent:load_file(options.src, locale)
×
97
   elseif SU.ast.hasContent(content) then
×
98
      local input = content[1]
×
99
      fluent:add_messages(input, locale)
×
100
   end
101
end, nil, nil, true)
7✔
102

103
require("languages.unicode")
7✔
104

105
-- The following languages neither have hyphenation nor specific
106
-- language support at present. This code is here to suppress warnings.
107
SILE.hyphenator.languages.ar = { patterns = {} }
7✔
108
SILE.hyphenator.languages.bo = { patterns = {} }
7✔
109
SILE.hyphenator.languages.sd = { patterns = {} }
7✔
110
SILE.hyphenator.languages.ur = { patterns = {} }
7✔
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