diff --git a/app/lib/utils.coffee b/app/lib/utils.coffee index 8fe47dbee..29f1434f2 100644 --- a/app/lib/utils.coffee +++ b/app/lib/utils.coffee @@ -44,4 +44,26 @@ module.exports.hslToHex = (hsl) -> toHex = (n) -> h = Math.floor(n).toString(16) h = '0'+h if h.length is 1 - h \ No newline at end of file + h + +i18n = (say, target, language=me.lang(), fallback='en') -> + generalResult = null + fallbackResult = null + fallforwardResult = null # If a general language isn't available, the first specific one will do + matches = (/\w+/gi).exec(language) + generalName = matches[0] if matches + + for locale in say?.i18n + if target of say[locale] + result = say[locale][target] + else continue + return result if locale == language + generalResult = result if locale == generalName + fallbackResult = result if locale == fallback + fallforwardResult = result if locale.indexOf language != -1 and not fallforwardResult? + + return generalResult if generalResult? + return fallbackResult if fallbackResult? + return fallforwardResult if fallforwardResult? + return say.text if 'text' of say + null \ No newline at end of file diff --git a/test/app/lib/utils.spec.coffee b/test/app/lib/utils.spec.coffee new file mode 100644 index 000000000..e69de29bb