Add romanize function

This commit is contained in:
kngako
2024-05-11 18:33:18 +02:00
parent ebe4273196
commit acca2f4055
5 changed files with 58 additions and 51 deletions

View File

@@ -2,6 +2,15 @@ const i18n = require('i18n')
const YAML = require('yaml')
const path = require("path")
const { transliterate } = require("transliteration")
const nonLatinPattern = /[^\u0000-\u007F]+/;
const needsRomanization = (text) => {
// Regex pattern to match non-Latin characters
return nonLatinPattern.test(text);
}
module.exports.baseLocale = "en"
module.exports.missingKeys = []
i18n.configure({
@@ -33,13 +42,20 @@ BaseLocaleKeys.forEach(baseLocaleKey => {
i18n.__h(baseLocaleKey)
})
module.exports.romanize = (text) => {
return needsRomanization(text) ? transliterate(text) : text;
}
module.exports.getText = (locale, key, substitution = null) => {
if (BaseLocaleKeys.includes(key)) {
// Check if substitions need to be made...
return i18n.__({
phrase: key,
locale: locale
}, substitution)
return i18n.__(
{
phrase: key,
locale: locale
},
substitution
);
} else {
// User requested a phrase we don't know
return `+404-${key}-${locale}`