From 6c0c61b54af62880efdf815020b3ad35c4c8acc4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Sun, 17 May 2015 19:23:48 +0200 Subject: [PATCH] Add WoltLab/WCF/Language --- .../templates/headIncludeJavaScript.tpl | 156 +++++++++--------- wcfsetup/install/files/js/WCF.js | 52 ++---- .../install/files/js/WoltLab/WCF/Bootstrap.js | 16 +- .../install/files/js/WoltLab/WCF/Language.js | 72 ++++++++ 4 files changed, 176 insertions(+), 120 deletions(-) create mode 100644 wcfsetup/install/files/js/WoltLab/WCF/Language.js diff --git a/com.woltlab.wcf/templates/headIncludeJavaScript.tpl b/com.woltlab.wcf/templates/headIncludeJavaScript.tpl index 9c143ac40b..cc19269589 100644 --- a/com.woltlab.wcf/templates/headIncludeJavaScript.tpl +++ b/com.woltlab.wcf/templates/headIncludeJavaScript.tpl @@ -52,83 +52,85 @@ diff --git a/wcfsetup/install/files/js/WCF.js b/wcfsetup/install/files/js/WCF.js index b1bd2ef027..a8a6504740 100755 --- a/wcfsetup/install/files/js/WCF.js +++ b/wcfsetup/install/files/js/WCF.js @@ -3376,54 +3376,24 @@ WCF.Dictionary = Class.extend({ }); /** - * Global language storage. - * - * @see WCF.Dictionary + * @deprecated Use WoltLab/WCF/Language */ WCF.Language = { - _variables: new WCF.Dictionary(), - - /** - * @see WCF.Dictionary.add() - */ add: function(key, value) { - this._variables.add(key, value); + console.warn('Call to deprecated WCF.Language.add("' + key + '")'); + require(['WoltLab/WCF/Language'], function(Language) { + Language.add(key, value); + }); }, - - /** - * @see WCF.Dictionary.addObject() - */ addObject: function(object) { - this._variables.addObject(object); + console.warn('Call to deprecated WCF.Language.addObject()'); + require(['WoltLab/WCF/Language'], function(Language) { + Language.addObject(object); + }); }, - - /** - * Retrieves a variable. - * - * @param string key - * @return mixed - */ get: function(key, parameters) { - // initialize parameters with an empty object - if (parameters == null) var parameters = { }; - - var value = this._variables.get(key); - - if (value === null) { - // return key again - return key; - } - else if (typeof value === 'string') { - // transform strings into template and try to refetch - this.add(key, new WCF.Template(value)); - return this.get(key, parameters); - } - else if (typeof value.fetch === 'function') { - // evaluate templates - value = value.fetch(parameters); - } - - return value; + // This cannot be sanely provided as a compatibility wrapper. + throw new Error('Call to deprecated WCF.Language.get("' + key + '")'); } }; diff --git a/wcfsetup/install/files/js/WoltLab/WCF/Bootstrap.js b/wcfsetup/install/files/js/WoltLab/WCF/Bootstrap.js index 8cd286fee1..e0de36cc42 100644 --- a/wcfsetup/install/files/js/WoltLab/WCF/Bootstrap.js +++ b/wcfsetup/install/files/js/WoltLab/WCF/Bootstrap.js @@ -12,18 +12,30 @@ define( [ 'jquery', 'favico', 'enquire', 'WoltLab/WCF/Date/Time/Relative', 'UI/SimpleDropdown', 'WoltLab/WCF/UI/Mobile', 'WoltLab/WCF/UI/TabMenu', 'WoltLab/WCF/UI/FlexibleMenu', - 'UI/Dialog', 'WoltLab/WCF/UI/Tooltip' + 'UI/Dialog', 'WoltLab/WCF/UI/Tooltip', 'WoltLab/WCF/Language' ], function( $, favico, enquire, relativeTime, simpleDropdown, UIMobile, UITabMenu, UIFlexibleMenu, - UIDialog, UITooltip + UIDialog, UITooltip, Language ) { "use strict"; window.Favico = favico; window.enquire = enquire; + window.WCF.Language.get = function(key, parameters) { + console.warn('Call to deprecated WCF.Language.get("' + key + '")'); + return Language.get(key); + }; + window.WCF.Language.add = function(key, value) { + console.warn('Call to deprecated WCF.Language.add("' + key + '")'); + return Language.add(key, value); + }; + window.WCF.Language.addObject = function(object) { + console.warn('Call to deprecated WCF.Language.addObject()'); + return Language.addObject(object); + }; /** * @constructor diff --git a/wcfsetup/install/files/js/WoltLab/WCF/Language.js b/wcfsetup/install/files/js/WoltLab/WCF/Language.js new file mode 100644 index 0000000000..bf87c6efd0 --- /dev/null +++ b/wcfsetup/install/files/js/WoltLab/WCF/Language.js @@ -0,0 +1,72 @@ +/** + * Manages language items. + * + * @author Tim Duesterhus + * @copyright 2001-2015 WoltLab GmbH + * @license GNU Lesser General Public License + * @module WoltLab/WCF/Language + */ +define(['Dictionary'], function(Dictionary) { + "use strict"; + + var languageItems = new Dictionary(); + + /** + * @constructor + */ + function Language() { }; + Language.prototype = { + /** + * Adds all the language items in the given object to the store. + * + * @param {Object.} object + */ + addObject: function (object) { + languageItems.merge(Dictionary.fromObject(object)); + }, + + /** + * Adds a single language item to the store. + * + * @param {string} key + * @param {string} value + */ + add: function(key, value) { + languageItems.set(key, value); + }, + + /** + * Fetches the language item specified by the given key. + * If the language item is a string it will be evaluated as + * WCF.Template with the given parameters. + * + * @param {string} key Language item to return. + * @param {Object=} parameters Parameters to provide to WCF.Template. + * @return {string} + */ + get: function(key, parameters) { + if (!parameters) parameters = { }; + + var value = languageItems.get(key); + + if (value === undefined) { + console.warn("Trying to get() undefined language item."); + return key; + } + + if (typeof value === 'string') { + // lazily convert to WCF.Template + languageItems.set(key, new WCF.Template(value)); + value = languageItems.get(key); + } + + if (value instanceof WCF.Template) { + value = value.fetch(parameters); + } + + return value; + } + }; + + return new Language(); +}); -- 2.20.1