]> BookStack Code Mirror - bookstack/blob - resources/assets/js/translations.js
Made some changes to the comment system
[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             console.log(exactMatches);
48             if (exactMatches !== null && Number(exactMatches[1]) === count) {
49                 result = t.replace(exactCountRegex, '').trim();
50                 break;
51             }
52
53             // Parse range matches
54             let rangeMatches = t.match(rangeRegex);
55             if (rangeMatches !== null) {
56                 let rangeStart = Number(rangeMatches[1]);
57                 if (rangeStart <= count && (rangeMatches[2] === '*' || Number(rangeMatches[2]) >= count)) {
58                     result = t.replace(rangeRegex, '').trim();
59                     break;
60                 }
61             }
62         }
63
64         if (result === null && splitText.length > 1) {
65             result = (count === 1) ? splitText[0] : splitText[1];
66         }
67
68         if (result === null) result = splitText[0];
69         return this.performReplacements(result, replacements);
70     }
71
72     /**
73      * Fetched translation text from the store for the given key.
74      * @param key
75      * @returns {String|Object}
76      */
77     getTransText(key) {
78         let splitKey = key.split('.');
79         let value = splitKey.reduce((a, b) => {
80             return a !== undefined ? a[b] : a;
81         }, this.store);
82
83         if (value === undefined) {
84             console.log(`Translation with key "${key}" does not exist`);
85             value = key;
86         }
87
88         return value;
89     }
90
91     /**
92      * Perform replacements on a string.
93      * @param {String} string
94      * @param {Object} replacements
95      * @returns {*}
96      */
97     performReplacements(string, replacements) {
98         if (!replacements) return string;
99         let replaceMatches = string.match(/:([\S]+)/g);
100         if (replaceMatches === null) return string;
101         replaceMatches.forEach(match => {
102             let key = match.substring(1);
103             if (typeof replacements[key] === 'undefined') return;
104             string = string.replace(match, replacements[key]);
105         });
106         return string;
107     }
108
109 }
110
111 module.exports = Translator;