]> BookStack Code Mirror - bookstack/blob - resources/assets/js/translations.js
add missing icon, fix name conventions
[bookstack] / resources / assets / js / translations.js
1 /**
2  *  Translation Manager
3  *  Handles the JavaScript side of translating strings
4  *  in a way which fits with Laravel.
5  */
6 class Translator {
7
8     /**
9      * Create an instance, Passing in the required translations
10      * @param translations
11      */
12     constructor(translations) {
13         this.store = translations;
14     }
15
16     /**
17      * Get a translation, Same format as laravel's 'trans' helper
18      * @param key
19      * @param replacements
20      * @returns {*}
21      */
22     get(key, replacements) {
23         let text = this.getTransText(key);
24         return this.performReplacements(text, replacements);
25     }
26
27     /**
28      * Get pluralised text, Dependant on the given count.
29      * Same format at laravel's 'trans_choice' helper.
30      * @param key
31      * @param count
32      * @param replacements
33      * @returns {*}
34      */
35     getPlural(key, count, replacements) {
36         let text = this.getTransText(key);
37         let splitText = text.split('|');
38         let result = null;
39         let exactCountRegex = /^{([0-9]+)}/;
40         let rangeRegex = /^\[([0-9]+),([0-9*]+)]/;
41
42         for (let i = 0, len = splitText.length; i < len; i++) {
43             let t = splitText[i];
44
45             // Parse exact matches
46             let exactMatches = t.match(exactCountRegex);
47             if (exactMatches !== null && Number(exactMatches[1]) === count) {
48                 result = t.replace(exactCountRegex, '').trim();
49                 break;
50             }
51
52             // Parse range matches
53             let rangeMatches = t.match(rangeRegex);
54             if (rangeMatches !== null) {
55                 let rangeStart = Number(rangeMatches[1]);
56                 if (rangeStart <= count && (rangeMatches[2] === '*' || Number(rangeMatches[2]) >= count)) {
57                     result = t.replace(rangeRegex, '').trim();
58                     break;
59                 }
60             }
61         }
62
63         if (result === null && splitText.length > 1) {
64             result = (count === 1) ? splitText[0] : splitText[1];
65         }
66
67         if (result === null) result = splitText[0];
68         return this.performReplacements(result, replacements);
69     }
70
71     /**
72      * Fetched translation text from the store for the given key.
73      * @param key
74      * @returns {String|Object}
75      */
76     getTransText(key) {
77         let splitKey = key.split('.');
78         let value = splitKey.reduce((a, b) => {
79             return a !== undefined ? a[b] : a;
80         }, this.store);
81
82         if (value === undefined) {
83             console.log(`Translation with key "${key}" does not exist`);
84             value = key;
85         }
86
87         return value;
88     }
89
90     /**
91      * Perform replacements on a string.
92      * @param {String} string
93      * @param {Object} replacements
94      * @returns {*}
95      */
96     performReplacements(string, replacements) {
97         if (!replacements) return string;
98         let replaceMatches = string.match(/:([\S]+)/g);
99         if (replaceMatches === null) return string;
100         replaceMatches.forEach(match => {
101             let key = match.substring(1);
102             if (typeof replacements[key] === 'undefined') return;
103             string = string.replace(match, replacements[key]);
104         });
105         return string;
106     }
107
108 }
109
110 module.exports = Translator;