Fix JavaScript for synonyms in tagAdd template
[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 /**
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
a278d41b
TD
60 try {
61 _languageItems.set(key, new Template(value));
62 }
63 catch (e) {
64 _languageItems.set(key, new Template('{literal}' + value.replace(/\{\/literal\}/g, '{/literal}{ldelim}/literal}{literal}') + '{/literal}'));
65 }
98ecc0b5 66 value = _languageItems.get(key);
6c0c61b5
TD
67 }
68
fd61a04b 69 if (value instanceof Template) {
6c0c61b5
TD
70 value = value.fetch(parameters);
71 }
72
73 return value;
74 }
75 };
76
bc8cd0ff 77 return Language;
6c0c61b5 78});