aboutsummaryrefslogtreecommitdiff
path: root/js/lib/languages.js
blob: 3231cf77d92338ad811cbd53b8f9cb2293a4ded6 (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
/**
 * Provides language-related functionality
 */
elgg.provide('elgg.config.translations');

elgg.config.language = 'en';

elgg.add_translation = function(lang, translations) {
	elgg.provide('elgg.config.translations.' + lang);
	
	$.extend(elgg.config.translations[lang], translations);
}

/**
 * Load the translations for the given language.
 * 
 * If no language is specified, the default language is used.
 * @param {string} language
 * @return {XMLHttpRequest}
 */
elgg.reload_all_translations = function(language) {
	var lang = language || elgg.get_language();
	elgg.getJSON('_css/js.php', {
		data: {
			'js': 'languages/'+lang,
			'viewtype': 'default',
			'lastcache': elgg.config.lastcache
		},
		success: function(json) {
			elgg.add_translation(lang, json);
		}
	});
};

/**
 * Get the current language
 * @return {String}
 */
elgg.get_language = function() {
	var user = elgg.get_loggedin_user();
	
	if (user && user.language) {
		return user.language;
	}
	
	return elgg.config.language;
};

/**
 * Translates a string
 * 
 * @param {String} key The string to translate
 * @param {String} language The language to display it in
 * @return {String} The translation
 */
elgg.echo = function(key, language) {
	var translations,
		dlang = elgg.get_language();
	
	language = language || dlang;
	
	translations = elgg.config.translations[language];
	if (translations && translations[key]) {
		return translations[key];
	}
	
	if (language == dlang) {
		return undefined;
	}
	
	translations = elgg.config.translations[dlang];
	if (translations && translations[key]) {
		return translations[key];
	}
	
	return undefined;
};

elgg.config.translations.init = function() {
	elgg.reload_all_translations();
};

elgg.register_event_handler('boot', 'system', elgg.config.translations.init);