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 | ||
98ecc0b5 | 12 | var _languageItems = new Dictionary(); |
6c0c61b5 TD |
13 | |
14 | /** | |
bc8cd0ff | 15 | * @exports WoltLab/WCF/Language |
6c0c61b5 | 16 | */ |
bc8cd0ff | 17 | var Language = { |
6c0c61b5 TD |
18 | /** |
19 | * Adds all the language items in the given object to the store. | |
20 | * | |
21 | * @param {Object.<string, string>} object | |
22 | */ | |
98ecc0b5 TD |
23 | addObject: function(object) { |
24 | _languageItems.merge(Dictionary.fromObject(object)); | |
6c0c61b5 TD |
25 | }, |
26 | ||
27 | /** | |
28 | * Adds a single language item to the store. | |
29 | * | |
30 | * @param {string} key | |
31 | * @param {string} value | |
32 | */ | |
33 | add: function(key, value) { | |
98ecc0b5 | 34 | _languageItems.set(key, value); |
6c0c61b5 TD |
35 | }, |
36 | ||
37 | /** | |
38 | * Fetches the language item specified by the given key. | |
39 | * If the language item is a string it will be evaluated as | |
87fe4ed0 | 40 | * WoltLab/WCF/Template with the given parameters. |
6c0c61b5 TD |
41 | * |
42 | * @param {string} key Language item to return. | |
87fe4ed0 | 43 | * @param {Object=} parameters Parameters to provide to WoltLab/WCF/Template. |
6c0c61b5 TD |
44 | * @return {string} |
45 | */ | |
46 | get: function(key, parameters) { | |
47 | if (!parameters) parameters = { }; | |
48 | ||
98ecc0b5 | 49 | var value = _languageItems.get(key); |
6c0c61b5 TD |
50 | |
51 | if (value === undefined) { | |
02a54851 MS |
52 | // TODO |
53 | //console.warn("Attempt to retrieve unknown phrase '" + key + "'."); | |
54 | //console.warn(new Error().stack); | |
6c0c61b5 TD |
55 | return key; |
56 | } | |
57 | ||
58 | if (typeof value === 'string') { | |
59 | // lazily convert to WCF.Template | |
98ecc0b5 TD |
60 | _languageItems.set(key, new Template(value)); |
61 | value = _languageItems.get(key); | |
6c0c61b5 TD |
62 | } |
63 | ||
fd61a04b | 64 | if (value instanceof Template) { |
6c0c61b5 TD |
65 | value = value.fetch(parameters); |
66 | } | |
67 | ||
68 | return value; | |
69 | } | |
70 | }; | |
71 | ||
bc8cd0ff | 72 | return Language; |
6c0c61b5 | 73 | }); |