Fixed minor issue in `WoltLab/WCF/Controller/Clipboard`
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / js / WoltLab / WCF / Language.js
CommitLineData
6c0c61b5
TD
1/**
2 * Manages language items.
3 *
4 * @author Tim Duesterhus
5 * @copyright 2001-2015 WoltLab GmbH
6 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
7 * @module WoltLab/WCF/Language
8 */
fd61a04b 9define(['Dictionary', './Template'], function(Dictionary, Template) {
6c0c61b5
TD
10 "use strict";
11
98ecc0b5 12 var _languageItems = new Dictionary();
6c0c61b5
TD
13
14 /**
15 * @constructor
16 */
17 function Language() { };
18 Language.prototype = {
19 /**
20 * Adds all the language items in the given object to the store.
21 *
22 * @param {Object.<string, string>} object
23 */
98ecc0b5
TD
24 addObject: function(object) {
25 _languageItems.merge(Dictionary.fromObject(object));
6c0c61b5
TD
26 },
27
28 /**
29 * Adds a single language item to the store.
30 *
31 * @param {string} key
32 * @param {string} value
33 */
34 add: function(key, value) {
98ecc0b5 35 _languageItems.set(key, value);
6c0c61b5
TD
36 },
37
38 /**
39 * Fetches the language item specified by the given key.
40 * If the language item is a string it will be evaluated as
87fe4ed0 41 * WoltLab/WCF/Template with the given parameters.
6c0c61b5
TD
42 *
43 * @param {string} key Language item to return.
87fe4ed0 44 * @param {Object=} parameters Parameters to provide to WoltLab/WCF/Template.
6c0c61b5
TD
45 * @return {string}
46 */
47 get: function(key, parameters) {
48 if (!parameters) parameters = { };
49
98ecc0b5 50 var value = _languageItems.get(key);
6c0c61b5
TD
51
52 if (value === undefined) {
53 console.warn("Trying to get() undefined language item.");
54 return key;
55 }
56
57 if (typeof value === 'string') {
58 // lazily convert to WCF.Template
98ecc0b5
TD
59 _languageItems.set(key, new Template(value));
60 value = _languageItems.get(key);
6c0c61b5
TD
61 }
62
fd61a04b 63 if (value instanceof Template) {
6c0c61b5
TD
64 value = value.fetch(parameters);
65 }
66
67 return value;
68 }
69 };
70
71 return new Language();
72});