From 76110423c02f63c148937e18506eb41a0ba79bdb Mon Sep 17 00:00:00 2001 From: Ruben Vereecken Date: Wed, 12 Mar 2014 20:30:43 +0100 Subject: [PATCH] Created i18n utility function --- app/lib/utils.coffee | 24 +++++++++++++++++++++++- test/app/lib/utils.spec.coffee | 0 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 test/app/lib/utils.spec.coffee 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