diff options
author | ewinslow <ewinslow@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2010-11-01 20:28:41 +0000 |
---|---|---|
committer | ewinslow <ewinslow@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2010-11-01 20:28:41 +0000 |
commit | 7e4cc929919f05e5b31c6ff7d1ed9b8e53e8a95a (patch) | |
tree | b0d3a33fdd8905577f6ef5701d3f05343e6458a8 | |
parent | 8400aa6e4fc0d5941729654487c1b09f9ceb1193 (diff) | |
download | elgg-7e4cc929919f05e5b31c6ff7d1ed9b8e53e8a95a.tar.gz elgg-7e4cc929919f05e5b31c6ff7d1ed9b8e53e8a95a.tar.bz2 |
Refs #2538: Pulled in some i18n features
git-svn-id: http://code.elgg.org/elgg/trunk@7182 36083f99-b078-4883-b0ff-0f9b5a30f544
-rw-r--r-- | engine/js/classes/ElggUser.js | 22 | ||||
-rw-r--r-- | engine/js/lib/elgglib.js | 32 | ||||
-rw-r--r-- | engine/js/lib/languages.js | 92 | ||||
-rw-r--r-- | engine/js/lib/session.js | 30 | ||||
-rw-r--r-- | engine/js/tests/ElggLanguagesTest.js | 45 |
5 files changed, 217 insertions, 4 deletions
diff --git a/engine/js/classes/ElggUser.js b/engine/js/classes/ElggUser.js new file mode 100644 index 000000000..dec7431de --- /dev/null +++ b/engine/js/classes/ElggUser.js @@ -0,0 +1,22 @@ +/** + * Create a new ElggUser + * + * @param {Object} o + * @extends ElggEntity + * @class Represents an ElggUser + * @property {string} name + * @property {string} username + */ +elgg.ElggUser = function(o) { + //elgg.ElggEntity.call(this, o); + this = o; +}; + +//elgg.inherit(elgg.ElggUser, elgg.ElggEntity); + +/** + * @return {boolean} Whether the user is an admin + */ +elgg.ElggUser.prototype.isAdmin = function() { + return this.admin === 'yes'; +};
\ No newline at end of file diff --git a/engine/js/lib/elgglib.js b/engine/js/lib/elgglib.js index c0ce69fab..28cae8f8f 100644 --- a/engine/js/lib/elgglib.js +++ b/engine/js/lib/elgglib.js @@ -69,6 +69,33 @@ elgg.provide = function(pkg) { };
/**
+ * Inherit the prototype methods from one constructor into another.
+ *
+ * @example
+ * <pre>
+ * function ParentClass(a, b) { }
+ *
+ * ParentClass.prototype.foo = function(a) { alert(a); }
+ *
+ * function ChildClass(a, b, c) {
+ * //equivalent of parent::__construct(a, b); in PHP
+ * ParentClass.call(this, a, b);
+ * }
+ *
+ * elgg.inherit(ChildClass, ParentClass);
+ *
+ * var child = new ChildClass('a', 'b', 'see');
+ * child.foo('boo!'); // alert('boo!');
+ * </pre>
+ *
+ * @param {Function} childCtor Child class.
+ * @param {Function} parentCtor Parent class.
+ */
+elgg.inherit = function(Child, Parent) {
+ Child.prototype = Parent;
+};
+
+/**
* Prepend elgg.config.wwwroot to a url if the url doesn't already have it.
*
* @param {String} url The url to extend
@@ -122,10 +149,7 @@ elgg.system_messages = function(msgs, delay, type) { + '<p>' + msgs.join('</p><p>') + '</p>'
+ '</div>';
- $(messages_html).insertAfter('#layout_header').click(function () {
- $(this).stop().fadeOut('slow');
- return false;
- }).show().animate({opacity:'1.0'},delay).fadeOut('slow');
+ $(messages_html).appendTo('#elgg_system_messages').show().animate({opacity:'1.0'},delay).fadeOut('slow');
};
/**
diff --git a/engine/js/lib/languages.js b/engine/js/lib/languages.js new file mode 100644 index 000000000..7bae74e6b --- /dev/null +++ b/engine/js/lib/languages.js @@ -0,0 +1,92 @@ +/**
+ * Provides language-related functionality
+ */
+elgg.provide('elgg.config.translations');
+
+elgg.config.language = 'en';
+
+elgg.config.translations.init = function() {
+ elgg.reload_all_translations();
+};
+
+elgg.add_translation = function(lang, translations) {
+ elgg.provide('elgg.config.translations.' + lang);
+
+ var t = elgg.config.translations;
+
+ t[lang] = $.extend(t[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);
+ }
+ });
+};
+
+/**
+ * @deprecated Use elgg.reload_all_translations
+ */
+elgg.config.translations.load = elgg.reload_all_translations;
+
+/**
+ * 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;
+};
+
+$(function() {
+ elgg.config.translations.init();
+});
\ No newline at end of file diff --git a/engine/js/lib/session.js b/engine/js/lib/session.js index 446dbfac1..227c607eb 100644 --- a/engine/js/lib/session.js +++ b/engine/js/lib/session.js @@ -81,6 +81,36 @@ elgg.session.cookie = function(name, value, options) { };
/**
+ * @return {ElggUser} The logged in user
+ */
+elgg.get_loggedin_user = function() {
+ return elgg.session.user;
+};
+
+/**
+ * @return {number} The GUID of the logged in user
+ */
+elgg.get_loggedin_userid = function() {
+ var user = elgg.get_loggedin_user();
+ return user ? user.guid : 0;
+};
+
+/**
+ * @return {boolean} Whether there is a user logged in
+ */
+elgg.isloggedin = function() {
+ return (elgg.get_loggedin_user() instanceof elgg.ElggUser);
+};
+
+/**
+ * @return {boolean} Whether there is an admin logged in
+ */
+elgg.isadminloggedin = function() {
+ var user = elgg.get_loggedin_user();
+ return (user instanceof ElggUser) && user.isAdmin();
+};
+
+/**
* @deprecated Use elgg.session.cookie instead
*/
$.cookie = elgg.session.cookie;
\ No newline at end of file diff --git a/engine/js/tests/ElggLanguagesTest.js b/engine/js/tests/ElggLanguagesTest.js new file mode 100644 index 000000000..950d5d3b8 --- /dev/null +++ b/engine/js/tests/ElggLanguagesTest.js @@ -0,0 +1,45 @@ +ElggLanguagesTest = TestCase("ElggLanguagesTest");
+
+ElggLanguagesTest.prototype.setUp = function() {
+ this.ajax = $.ajax;
+
+ //Immediately execute some dummy "returned" javascript instead of sending
+ //an actual ajax request
+ $.ajax = function(settings) {
+ var lang = settings.data.js.split('/')[1];
+ elgg.config.translations[lang] = {'language':lang};
+ };
+};
+
+ElggLanguagesTest.prototype.tearDown = function() {
+ $.ajax = this.ajax;
+
+ //clear translations
+ elgg.config.translations['en'] = undefined;
+ elgg.config.translations['aa'] = undefined;
+};
+
+ElggLanguagesTest.prototype.testLoadTranslations = function() {
+ assertUndefined(elgg.config.translations['en']);
+ assertUndefined(elgg.config.translations['aa']);
+
+ elgg.reload_all_translations();
+ elgg.reload_all_translations('aa');
+
+ assertNotUndefined(elgg.config.translations['en']['language']);
+ assertNotUndefined(elgg.config.translations['aa']['language']);
+};
+
+ElggLanguagesTest.prototype.testElggEchoTranslates = function() {
+ elgg.reload_all_translations('en');
+ elgg.reload_all_translations('aa');
+
+ assertEquals('en', elgg.echo('language'));
+ assertEquals('aa', elgg.echo('language', 'aa'));
+};
+
+ElggLanguagesTest.prototype.testElggEchoFallsBackToDefaultLanguage = function() {
+ elgg.reload_all_translations('en');
+ assertEquals('en', elgg.echo('language', 'aa'));
+};
+
|