2016-12-31 09:27:40 -05:00
|
|
|
/**
|
|
|
|
* Translation Manager
|
|
|
|
* Handles the JavaScript side of translating strings
|
|
|
|
* in a way which fits with Laravel.
|
|
|
|
*/
|
|
|
|
class Translator {
|
|
|
|
|
2023-04-19 10:20:04 -04:00
|
|
|
constructor() {
|
2019-07-06 09:52:25 -04:00
|
|
|
this.store = new Map();
|
|
|
|
this.parseTranslations();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse translations out of the page and place into the store.
|
|
|
|
*/
|
|
|
|
parseTranslations() {
|
|
|
|
const translationMetaTags = document.querySelectorAll('meta[name="translation"]');
|
2023-04-18 17:20:02 -04:00
|
|
|
for (const tag of translationMetaTags) {
|
2019-07-06 09:52:25 -04:00
|
|
|
const key = tag.getAttribute('key');
|
|
|
|
const value = tag.getAttribute('value');
|
|
|
|
this.store.set(key, value);
|
|
|
|
}
|
2016-12-31 09:27:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-04-19 10:20:04 -04:00
|
|
|
* Get a translation, Same format as Laravel's 'trans' helper
|
2016-12-31 09:27:40 -05:00
|
|
|
* @param key
|
|
|
|
* @param replacements
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
get(key, replacements) {
|
2019-07-06 09:52:25 -04:00
|
|
|
const text = this.getTransText(key);
|
2017-09-03 11:37:51 -04:00
|
|
|
return this.performReplacements(text, replacements);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-04-19 10:20:04 -04:00
|
|
|
* Get pluralised text, Dependent on the given count.
|
|
|
|
* Same format at Laravel's 'trans_choice' helper.
|
2017-09-03 11:37:51 -04:00
|
|
|
* @param key
|
|
|
|
* @param count
|
|
|
|
* @param replacements
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
getPlural(key, count, replacements) {
|
2019-07-06 09:52:25 -04:00
|
|
|
const text = this.getTransText(key);
|
2020-07-28 13:19:18 -04:00
|
|
|
return this.parsePlural(text, count, replacements);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the given translation and find the correct plural option
|
2023-04-19 10:20:04 -04:00
|
|
|
* to use. Similar format at Laravel's 'trans_choice' helper.
|
2020-07-28 13:19:18 -04:00
|
|
|
* @param {String} translation
|
|
|
|
* @param {Number} count
|
|
|
|
* @param {Object} replacements
|
|
|
|
* @returns {String}
|
|
|
|
*/
|
|
|
|
parsePlural(translation, count, replacements) {
|
|
|
|
const splitText = translation.split('|');
|
2019-07-06 09:52:25 -04:00
|
|
|
const exactCountRegex = /^{([0-9]+)}/;
|
|
|
|
const rangeRegex = /^\[([0-9]+),([0-9*]+)]/;
|
2017-09-03 11:37:51 -04:00
|
|
|
let result = null;
|
|
|
|
|
2023-04-18 17:20:02 -04:00
|
|
|
for (const t of splitText) {
|
2017-09-03 11:37:51 -04:00
|
|
|
// Parse exact matches
|
2019-07-06 09:52:25 -04:00
|
|
|
const exactMatches = t.match(exactCountRegex);
|
2017-09-03 11:37:51 -04:00
|
|
|
if (exactMatches !== null && Number(exactMatches[1]) === count) {
|
|
|
|
result = t.replace(exactCountRegex, '').trim();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse range matches
|
2019-07-06 09:52:25 -04:00
|
|
|
const rangeMatches = t.match(rangeRegex);
|
2017-09-03 11:37:51 -04:00
|
|
|
if (rangeMatches !== null) {
|
2019-07-06 09:52:25 -04:00
|
|
|
const rangeStart = Number(rangeMatches[1]);
|
2017-09-03 11:37:51 -04:00
|
|
|
if (rangeStart <= count && (rangeMatches[2] === '*' || Number(rangeMatches[2]) >= count)) {
|
|
|
|
result = t.replace(rangeRegex, '').trim();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result === null && splitText.length > 1) {
|
|
|
|
result = (count === 1) ? splitText[0] : splitText[1];
|
|
|
|
}
|
|
|
|
|
2019-08-25 06:02:58 -04:00
|
|
|
if (result === null) {
|
|
|
|
result = splitText[0];
|
|
|
|
}
|
|
|
|
|
2017-09-03 11:37:51 -04:00
|
|
|
return this.performReplacements(result, replacements);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetched translation text from the store for the given key.
|
|
|
|
* @param key
|
|
|
|
* @returns {String|Object}
|
|
|
|
*/
|
|
|
|
getTransText(key) {
|
2019-07-06 09:52:25 -04:00
|
|
|
const value = this.store.get(key);
|
2016-12-31 09:27:40 -05:00
|
|
|
|
|
|
|
if (value === undefined) {
|
2019-07-06 09:52:25 -04:00
|
|
|
console.warn(`Translation with key "${key}" does not exist`);
|
2016-12-31 09:27:40 -05:00
|
|
|
}
|
|
|
|
|
2017-09-03 11:37:51 -04:00
|
|
|
return value;
|
|
|
|
}
|
2016-12-31 09:27:40 -05:00
|
|
|
|
2017-09-03 11:37:51 -04:00
|
|
|
/**
|
|
|
|
* Perform replacements on a string.
|
|
|
|
* @param {String} string
|
|
|
|
* @param {Object} replacements
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
performReplacements(string, replacements) {
|
|
|
|
if (!replacements) return string;
|
2023-04-19 10:20:04 -04:00
|
|
|
const replaceMatches = string.match(/:(\S+)/g);
|
2017-09-03 11:37:51 -04:00
|
|
|
if (replaceMatches === null) return string;
|
2023-04-19 10:20:04 -04:00
|
|
|
let updatedString = string;
|
|
|
|
|
2016-12-31 09:27:40 -05:00
|
|
|
replaceMatches.forEach(match => {
|
2019-07-06 09:52:25 -04:00
|
|
|
const key = match.substring(1);
|
2016-12-31 09:27:40 -05:00
|
|
|
if (typeof replacements[key] === 'undefined') return;
|
2023-04-19 10:20:04 -04:00
|
|
|
updatedString = updatedString.replace(match, replacements[key]);
|
2016-12-31 09:27:40 -05:00
|
|
|
});
|
2023-04-19 10:20:04 -04:00
|
|
|
|
|
|
|
return updatedString;
|
2016-12-31 09:27:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-09 16:17:35 -05:00
|
|
|
export default Translator;
|