Add content selection before removing content
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / js / WoltLabSuite / Core / Language.js
CommitLineData
6c0c61b5
TD
1/**
2 * Manages language items.
3 *
4 * @author Tim Duesterhus
c839bd49 5 * @copyright 2001-2018 WoltLab GmbH
6c0c61b5 6 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
58d7e8f8 7 * @module WoltLabSuite/Core/Language
6c0c61b5 8 */
fd61a04b 9define(['Dictionary', './Template'], function(Dictionary, Template) {
6c0c61b5
TD
10 "use strict";
11
98ecc0b5 12 var _languageItems = new Dictionary();
6c0c61b5
TD
13
14 /**
58d7e8f8 15 * @exports WoltLabSuite/Core/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
58d7e8f8 40 * WoltLabSuite/Core/Template with the given parameters.
6c0c61b5
TD
41 *
42 * @param {string} key Language item to return.
58d7e8f8 43 * @param {Object=} parameters Parameters to provide to WoltLabSuite/Core/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) {
6c0c61b5
TD
52 return key;
53 }
54
0d9fa991
MS
55 // fetch Template, as it cannot be provided because of a circular dependency
56 if (Template === undefined) Template = require('WoltLabSuite/Core/Template');
57
6c0c61b5
TD
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});