Add WoltLab/WCF/Language
[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 */
9define(['Dictionary'], function(Dictionary) {
10 "use strict";
11
12 var languageItems = new Dictionary();
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 */
24 addObject: function (object) {
25 languageItems.merge(Dictionary.fromObject(object));
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) {
35 languageItems.set(key, value);
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
41 * WCF.Template with the given parameters.
42 *
43 * @param {string} key Language item to return.
44 * @param {Object=} parameters Parameters to provide to WCF.Template.
45 * @return {string}
46 */
47 get: function(key, parameters) {
48 if (!parameters) parameters = { };
49
50 var value = languageItems.get(key);
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
59 languageItems.set(key, new WCF.Template(value));
60 value = languageItems.get(key);
61 }
62
63 if (value instanceof WCF.Template) {
64 value = value.fetch(parameters);
65 }
66
67 return value;
68 }
69 };
70
71 return new Language();
72});