3 * Handles the JavaScript side of translating strings
4 * in a way which fits with Laravel.
9 * Create an instance, Passing in the required translations
12 constructor(translations) {
13 this.store = translations;
17 * Get a translation, Same format as laravel's 'trans' helper
22 get(key, replacements) {
23 let text = this.getTransText(key);
24 return this.performReplacements(text, replacements);
28 * Get pluralised text, Dependant on the given count.
29 * Same format at laravel's 'trans_choice' helper.
35 getPlural(key, count, replacements) {
36 let text = this.getTransText(key);
37 let splitText = text.split('|');
39 let exactCountRegex = /^{([0-9]+)}/;
40 let rangeRegex = /^\[([0-9]+),([0-9*]+)]/;
42 for (let i = 0, len = splitText.length; i < len; i++) {
45 // Parse exact matches
46 let exactMatches = t.match(exactCountRegex);
47 if (exactMatches !== null && Number(exactMatches[1]) === count) {
48 result = t.replace(exactCountRegex, '').trim();
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();
63 if (result === null && splitText.length > 1) {
64 result = (count === 1) ? splitText[0] : splitText[1];
67 if (result === null) result = splitText[0];
68 return this.performReplacements(result, replacements);
72 * Fetched translation text from the store for the given key.
74 * @returns {String|Object}
77 let splitKey = key.split('.');
78 let value = splitKey.reduce((a, b) => {
79 return a !== undefined ? a[b] : a;
82 if (value === undefined) {
83 console.log(`Translation with key "${key}" does not exist`);
91 * Perform replacements on a string.
92 * @param {String} string
93 * @param {Object} replacements
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]);
110 module.exports = Translator;