Add content selection before removing content
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / js / WoltLabSuite / Core / Language.js
... / ...
CommitLineData
1/**
2 * Manages language items.
3 *
4 * @author Tim Duesterhus
5 * @copyright 2001-2018 WoltLab GmbH
6 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
7 * @module WoltLabSuite/Core/Language
8 */
9define(['Dictionary', './Template'], function(Dictionary, Template) {
10 "use strict";
11
12 var _languageItems = new Dictionary();
13
14 /**
15 * @exports WoltLabSuite/Core/Language
16 */
17 var Language = {
18 /**
19 * Adds all the language items in the given object to the store.
20 *
21 * @param {Object.<string, string>} object
22 */
23 addObject: function(object) {
24 _languageItems.merge(Dictionary.fromObject(object));
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) {
34 _languageItems.set(key, value);
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
40 * WoltLabSuite/Core/Template with the given parameters.
41 *
42 * @param {string} key Language item to return.
43 * @param {Object=} parameters Parameters to provide to WoltLabSuite/Core/Template.
44 * @return {string}
45 */
46 get: function(key, parameters) {
47 if (!parameters) parameters = { };
48
49 var value = _languageItems.get(key);
50
51 if (value === undefined) {
52 return key;
53 }
54
55 // fetch Template, as it cannot be provided because of a circular dependency
56 if (Template === undefined) Template = require('WoltLabSuite/Core/Template');
57
58 if (typeof value === 'string') {
59 // lazily convert to WCF.Template
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 }
66 value = _languageItems.get(key);
67 }
68
69 if (value instanceof Template) {
70 value = value.fetch(parameters);
71 }
72
73 return value;
74 }
75 };
76
77 return Language;
78});