44 lines
982 B
JavaScript
44 lines
982 B
JavaScript
|
const { normalize } = require('../utility')
|
||
|
|
||
|
function plainStringify (text, { lang = {} } = {}) {
|
||
|
text = normalize(text)
|
||
|
|
||
|
let string = ''
|
||
|
|
||
|
if (text.text != null) string += text.text
|
||
|
else if (text.translate != null) {
|
||
|
let format
|
||
|
if (Object.hasOwn(lang, text.translate)) format = lang[text.translate]
|
||
|
else if (text.fallback != null) format = text.fallback
|
||
|
else format = text.translate
|
||
|
|
||
|
const _with = text.with || []
|
||
|
let i = 0
|
||
|
|
||
|
string += format.replace(/%(?:(\d+)\$)?(s|%)/g, (g0, g1) => {
|
||
|
if (g0 === '%%') return '%'
|
||
|
|
||
|
const idx = g1 ? parseInt(g1) : i++
|
||
|
if (_with[idx]) {
|
||
|
return plainStringify(_with[idx], { lang })
|
||
|
}
|
||
|
|
||
|
return ''
|
||
|
})
|
||
|
}
|
||
|
else if (text.selector != null) string += text.selector
|
||
|
else if (text.keybind) {
|
||
|
// TODO
|
||
|
}
|
||
|
|
||
|
if (text.extra) {
|
||
|
for (const extra of text.extra) {
|
||
|
string += plainStringify(extra, { lang })
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return string
|
||
|
}
|
||
|
|
||
|
module.exports = plainStringify
|