forked from belisarius222/Meteor-handlebar-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.js
71 lines (62 loc) · 2.14 KB
/
common.js
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
// Helper scope
if (typeof Helpers === 'undefined') {
Helpers = {};
}
var languageText = {};
// expects an array: languageText['say.hello.to.me']['en'] = 'Say hello to me:)';
// ex.:
// getText('Say.Hello.To.Me') == 'say hello to me:)'; // lowercase
// getText('SAY.HELLO.TO.ME') == 'SAY HELLO TO ME:)'; // uppercase
// getText('Say.hello.to.me') == 'Say hello to me:)'; // uppercase first letter, rest lowercase
// getText('Say.Hello.To.Me') == 'Say Hello To Me:)'; // camelCase
// getText('SAy.hello.to.me') == 'Say hello To me:)'; // ignore case sensitivity
var _languageDeps = (Meteor.isClient) ? new Deps.Dependency() : null;
var currentLanguage = 'en';
// language = 'en'
Helpers.setLanguage = function (language) {
if (language && language !== currentLanguage) {
currentLanguage = language;
if (Meteor.isClient) _languageDeps.changed();
}
};
Helpers.language = function () {
if (Meteor.isClient) _languageDeps.depend();
return currentLanguage;
};
Helpers.setDictionary = function(dict) {
languageText = dict;
};
Helpers.addDictionary = function(dict) {
_.extend(languageText, dict);
};
// handleCase will mimic text Case making src same case as text
var handleCase = function (text, src) {
// Return lowercase
if (text == text.toLowerCase())
return src.toLowerCase();
// Return uppercase
if (text == text.toUpperCase())
return src.toUpperCase();
// Return uppercase first letter, rest lowercase
if (text.substr(1) == text.substr(1).toLowerCase())
return src.substr(0, 1).toUpperCase() + src.substr(1).toLowerCase();
// Return src withour changes
if (text.substr(0, 2) == text.substr(0, 2).toUpperCase())
return src;
// Return CamelCase
return src.replace(/( [a-z])/g, function ($1) {
return $1.toUpperCase();
});
};
/**
*
* @param {string} text
* @param {string} [lang]
* @return {string}
*/
Helpers.getText = function (text, lang) {
var txt = text.toLowerCase();
var langText = languageText && languageText[txt];
var langKey = (typeof lang === 'string') ? lang : Helpers.language();
return handleCase(text, (langText) ? ( (langText[langKey]) ? langText[langKey] : langText.en) : '[' + text + ']');
};