-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathI18N.lua
120 lines (98 loc) · 2.86 KB
/
I18N.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
-- Interface to the text localization system.
-- @module I18N
local ILocalizable = require("api.ILocalizable")
local i18n = require("internal.i18n")
local I18N = {}
--- Returns the current langauge identifier as a string.
---
--- @treturn string
function I18N.language()
return i18n.language
end
--- @treturn string
function I18N.quote_character()
return "「"
end
--- @treturn string
function I18N.space()
if I18N.is_fullwidth() then
return ""
end
return " "
end
--- True if the current language uses fullwidth characters.
---
--- @treturn bool
function I18N.is_fullwidth()
return I18N.language() == "jp"
end
--- @function I18N.capitalize(str)
--- @tparam string str
--- @treturn string
I18N.capitalize = i18n.capitalize
--- Localizes a string with arguments. Pass the ID of a localized
--- string and any arguments to its formatting function. May return
--- nil if the ID was not found.
---
--- If any of the arguments implement ILocalizable, then the
--- localization data for them will be produced and sent instead.
---
--- @tparam string id ID of the localized string
--- @tparam ... ... Extra arguments to pass to the formatter.
--- @treturn[opt] string The localized text
--- @see ILocalizable
function I18N.get_optional(text, ...)
local args = {}
for i = 1, select("#", ...) do
local arg = select(i, ...)
local i18n = require("internal.i18n.init")
if class.is_an(ILocalizable, arg) then
args[i] = arg:produce_locale_data()
else
args[i] = I18N.get_optional(arg) or arg
end
end
return i18n.get(text, table.unpack(args))
end
--- Localizes a string with arguments. Pass the ID of a localized
--- string and any arguments to its formatting function. May return
--- nil if the ID was not found.
---
--- If any of the arguments implement ILocalizable, then the
--- localization data for them will be produced and sent instead.
---
--- @tparam string id ID of the localized string
--- @param ... Extra arguments to pass to the formatter.
--- @treturn string The localized text
--- @see ILocalizable
function I18N.get(text, ...)
return I18N.get_optional(text, ...) or ("<error: %s>"):format(text)
end
-- TODO for itemname, provide a set of "cut points" so the user can
-- split the string and insert whatever.
--- Switches the current language.
---
--- @tparam string lang Language identifier.
--- @function switch_language
I18N.switch_language = i18n.switch_language
function I18N.get_choice_count(prefix, suffix)
local i = 1
if suffix then
suffix = "." .. suffix
else
suffix = ""
end
while I18N.get_optional(prefix .. "._" .. i .. suffix) do
i = i + 1
end
return i - 1
end
function I18N.quote_speech(id, ...)
local text = I18N.get_optional(id, ...) or id
-- TODO
if not I18N.is_fullwidth() then
text = ("\"%s\""):format(text)
end
return text
end
return I18N