Route cache did not distinguish between ACP and frontend destinations
[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
50d96bd8 5 * @copyright 2001-2017 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
55 if (typeof value === 'string') {
56 // lazily convert to WCF.Template
a278d41b
TD
57 try {
58 _languageItems.set(key, new Template(value));
59 }
60 catch (e) {
61 _languageItems.set(key, new Template('{literal}' + value.replace(/\{\/literal\}/g, '{/literal}{ldelim}/literal}{literal}') + '{/literal}'));
62 }
98ecc0b5 63 value = _languageItems.get(key);
6c0c61b5
TD
64 }
65
fd61a04b 66 if (value instanceof Template) {
6c0c61b5
TD
67 value = value.fetch(parameters);
68 }
69
70 return value;
71 }
72 };
73
bc8cd0ff 74 return Language;
6c0c61b5 75});