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 splitKey = key.split('.');
24 let value = splitKey.reduce((a, b) => {
25 return a != undefined ? a[b] : a;
28 if (value === undefined) {
29 console.log(`Translation with key "${key}" does not exist`);
33 if (replacements === undefined) return value;
35 let replaceMatches = value.match(/:([\S]+)/g);
36 if (replaceMatches === null) return value;
37 replaceMatches.forEach(match => {
38 let key = match.substring(1);
39 if (typeof replacements[key] === 'undefined') return;
40 value = value.replace(match, replacements[key]);
47 export default Translator