]> BookStack Code Mirror - bookstack/blob - resources/assets/js/services/translations.js
Alter docker paths
[bookstack] / resources / assets / js / services / 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 = new Map();
14         this.parseTranslations();
15     }
16
17     /**
18      * Parse translations out of the page and place into the store.
19      */
20     parseTranslations() {
21         const translationMetaTags = document.querySelectorAll('meta[name="translation"]');
22         for (let tag of translationMetaTags) {
23             const key = tag.getAttribute('key');
24             const value = tag.getAttribute('value');
25             this.store.set(key, value);
26         }
27     }
28
29     /**
30      * Get a translation, Same format as laravel's 'trans' helper
31      * @param key
32      * @param replacements
33      * @returns {*}
34      */
35     get(key, replacements) {
36         const text = this.getTransText(key);
37         return this.performReplacements(text, replacements);
38     }
39
40     /**
41      * Get pluralised text, Dependant on the given count.
42      * Same format at laravel's 'trans_choice' helper.
43      * @param key
44      * @param count
45      * @param replacements
46      * @returns {*}
47      */
48     getPlural(key, count, replacements) {
49         const text = this.getTransText(key);
50         const splitText = text.split('|');
51         const exactCountRegex = /^{([0-9]+)}/;
52         const rangeRegex = /^\[([0-9]+),([0-9*]+)]/;
53         let result = null;
54
55         for (const i = 0, len = splitText.length; i < len; i++) {
56             const t = splitText[i];
57
58             // Parse exact matches
59             const exactMatches = t.match(exactCountRegex);
60             if (exactMatches !== null && Number(exactMatches[1]) === count) {
61                 result = t.replace(exactCountRegex, '').trim();
62                 break;
63             }
64
65             // Parse range matches
66             const rangeMatches = t.match(rangeRegex);
67             if (rangeMatches !== null) {
68                 const rangeStart = Number(rangeMatches[1]);
69                 if (rangeStart <= count && (rangeMatches[2] === '*' || Number(rangeMatches[2]) >= count)) {
70                     result = t.replace(rangeRegex, '').trim();
71                     break;
72                 }
73             }
74         }
75
76         if (result === null && splitText.length > 1) {
77             result = (count === 1) ? splitText[0] : splitText[1];
78         }
79
80         if (result === null) result = splitText[0];
81         return this.performReplacements(result, replacements);
82     }
83
84     /**
85      * Fetched translation text from the store for the given key.
86      * @param key
87      * @returns {String|Object}
88      */
89     getTransText(key) {
90         const value = this.store.get(key);
91
92         if (value === undefined) {
93             console.warn(`Translation with key "${key}" does not exist`);
94         }
95
96         return value;
97     }
98
99     /**
100      * Perform replacements on a string.
101      * @param {String} string
102      * @param {Object} replacements
103      * @returns {*}
104      */
105     performReplacements(string, replacements) {
106         if (!replacements) return string;
107         const replaceMatches = string.match(/:([\S]+)/g);
108         if (replaceMatches === null) return string;
109         replaceMatches.forEach(match => {
110             const key = match.substring(1);
111             if (typeof replacements[key] === 'undefined') return;
112             string = string.replace(match, replacements[key]);
113         });
114         return string;
115     }
116
117 }
118
119 export default Translator;