Commit | Line | Data |
---|---|---|
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 | 9 | define(['Dictionary', './Template'], function(Dictionary, Template) { |
6c0c61b5 TD |
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 | |
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 | ||
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 | |
fd61a04b | 59 | languageItems.set(key, new Template(value)); |
6c0c61b5 TD |
60 | value = languageItems.get(key); |
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 | }); |