X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/e91ef54cc9f8ce6b264bced8191275b6a33e594f..refs/pull/5239/head:/resources/js/services/translations.js diff --git a/resources/js/services/translations.js b/resources/js/services/translations.js index b595a05e6..e562a9152 100644 --- a/resources/js/services/translations.js +++ b/resources/js/services/translations.js @@ -5,11 +5,7 @@ */ class Translator { - /** - * Create an instance, Passing in the required translations - * @param translations - */ - constructor(translations) { + constructor() { this.store = new Map(); this.parseTranslations(); } @@ -19,7 +15,7 @@ class Translator { */ parseTranslations() { const translationMetaTags = document.querySelectorAll('meta[name="translation"]'); - for (let tag of translationMetaTags) { + for (const tag of translationMetaTags) { const key = tag.getAttribute('key'); const value = tag.getAttribute('value'); this.store.set(key, value); @@ -27,7 +23,7 @@ class Translator { } /** - * Get a translation, Same format as laravel's 'trans' helper + * Get a translation, Same format as Laravel's 'trans' helper * @param key * @param replacements * @returns {*} @@ -38,8 +34,8 @@ class Translator { } /** - * Get pluralised text, Dependant on the given count. - * Same format at laravel's 'trans_choice' helper. + * Get pluralised text, Dependent on the given count. + * Same format at Laravel's 'trans_choice' helper. * @param key * @param count * @param replacements @@ -47,12 +43,24 @@ class Translator { */ getPlural(key, count, replacements) { const text = this.getTransText(key); - const splitText = text.split('|'); + return this.parsePlural(text, count, replacements); + } + + /** + * Parse the given translation and find the correct plural option + * to use. Similar format at Laravel's 'trans_choice' helper. + * @param {String} translation + * @param {Number} count + * @param {Object} replacements + * @returns {String} + */ + parsePlural(translation, count, replacements) { + const splitText = translation.split('|'); const exactCountRegex = /^{([0-9]+)}/; const rangeRegex = /^\[([0-9]+),([0-9*]+)]/; let result = null; - for (let t of splitText) { + for (const t of splitText) { // Parse exact matches const exactMatches = t.match(exactCountRegex); if (exactMatches !== null && Number(exactMatches[1]) === count) { @@ -105,14 +113,17 @@ class Translator { */ performReplacements(string, replacements) { if (!replacements) return string; - const replaceMatches = string.match(/:([\S]+)/g); + const replaceMatches = string.match(/:(\S+)/g); if (replaceMatches === null) return string; + let updatedString = string; + replaceMatches.forEach(match => { const key = match.substring(1); if (typeof replacements[key] === 'undefined') return; - string = string.replace(match, replacements[key]); + updatedString = updatedString.replace(match, replacements[key]); }); - return string; + + return updatedString; } }