diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index ba93bfe65..d2c75f956 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -91,35 +91,6 @@ class HomeController extends Controller return view('common.home', $commonData); } - /** - * Get a js representation of the current translations - * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response - * @throws \Exception - */ - public function getTranslations() - { - $locale = app()->getLocale(); - $cacheKey = 'GLOBAL_TRANSLATIONS_' . $locale; - - if (cache()->has($cacheKey) && config('app.env') !== 'development') { - $resp = cache($cacheKey); - } else { - $translations = [ - // Get only translations which might be used in JS - 'common' => trans('common'), - 'components' => trans('components'), - 'entities' => trans('entities'), - 'errors' => trans('errors') - ]; - $resp = 'window.translations = ' . json_encode($translations); - cache()->put($cacheKey, $resp, 120); - } - - return response($resp, 200, [ - 'Content-Type' => 'application/javascript' - ]); - } - /** * Get custom head HTML, Used in ajax calls to show in editor. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index fa10d4874..e45a0be92 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -39,6 +39,14 @@ class AppServiceProvider extends ServiceProvider return ""; }); + Blade::directive('exposeTranslations', function($expression) { + return "startPush('translations'); ?>" . + "" . + '' . "\n" . + "" . + 'stopPush(); ?>'; + }); + // Allow longer string lengths after upgrade to utf8mb4 Schema::defaultStringLength(191); diff --git a/resources/assets/js/index.js b/resources/assets/js/index.js index c23615a88..e0c7b34e5 100644 --- a/resources/assets/js/index.js +++ b/resources/assets/js/index.js @@ -16,7 +16,7 @@ window.$events = eventManager; // Translation setup // Creates a global function with name 'trans' to be used in the same way as Laravel's translation system import Translations from "./services/translations" -const translator = new Translations(window.translations); +const translator = new Translations(); window.trans = translator.get.bind(translator); window.trans_choice = translator.getPlural.bind(translator); diff --git a/resources/assets/js/services/translations.js b/resources/assets/js/services/translations.js index 06b44a580..645286c08 100644 --- a/resources/assets/js/services/translations.js +++ b/resources/assets/js/services/translations.js @@ -10,7 +10,20 @@ class Translator { * @param translations */ constructor(translations) { - this.store = translations; + 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"]'); + for (let tag of translationMetaTags) { + const key = tag.getAttribute('key'); + const value = tag.getAttribute('value'); + this.store.set(key, value); + } } /** @@ -20,7 +33,7 @@ class Translator { * @returns {*} */ get(key, replacements) { - let text = this.getTransText(key); + const text = this.getTransText(key); return this.performReplacements(text, replacements); } @@ -33,26 +46,26 @@ class Translator { * @returns {*} */ getPlural(key, count, replacements) { - let text = this.getTransText(key); - let splitText = text.split('|'); + const text = this.getTransText(key); + const splitText = text.split('|'); + const exactCountRegex = /^{([0-9]+)}/; + const rangeRegex = /^\[([0-9]+),([0-9*]+)]/; let result = null; - let exactCountRegex = /^{([0-9]+)}/; - let rangeRegex = /^\[([0-9]+),([0-9*]+)]/; - for (let i = 0, len = splitText.length; i < len; i++) { - let t = splitText[i]; + for (const i = 0, len = splitText.length; i < len; i++) { + const t = splitText[i]; // Parse exact matches - let exactMatches = t.match(exactCountRegex); + const exactMatches = t.match(exactCountRegex); if (exactMatches !== null && Number(exactMatches[1]) === count) { result = t.replace(exactCountRegex, '').trim(); break; } // Parse range matches - let rangeMatches = t.match(rangeRegex); + const rangeMatches = t.match(rangeRegex); if (rangeMatches !== null) { - let rangeStart = Number(rangeMatches[1]); + const rangeStart = Number(rangeMatches[1]); if (rangeStart <= count && (rangeMatches[2] === '*' || Number(rangeMatches[2]) >= count)) { result = t.replace(rangeRegex, '').trim(); break; @@ -74,14 +87,10 @@ class Translator { * @returns {String|Object} */ getTransText(key) { - let splitKey = key.split('.'); - let value = splitKey.reduce((a, b) => { - return a !== undefined ? a[b] : a; - }, this.store); + const value = this.store.get(key); if (value === undefined) { - console.log(`Translation with key "${key}" does not exist`); - value = key; + console.warn(`Translation with key "${key}" does not exist`); } return value; @@ -95,10 +104,10 @@ class Translator { */ performReplacements(string, replacements) { if (!replacements) return string; - let replaceMatches = string.match(/:([\S]+)/g); + const replaceMatches = string.match(/:([\S]+)/g); if (replaceMatches === null) return string; replaceMatches.forEach(match => { - let key = match.substring(1); + const key = match.substring(1); if (typeof replacements[key] === 'undefined') return; string = string.replace(match, replacements[key]); }); diff --git a/resources/views/base.blade.php b/resources/views/base.blade.php index 367a2cd8b..f5459a717 100644 --- a/resources/views/base.blade.php +++ b/resources/views/base.blade.php @@ -13,14 +13,17 @@ - - - @yield('head') + + @include('partials.custom-styles') @include('partials.custom-head') @stack('head') + + + @stack('translations') +
diff --git a/resources/views/comments/comments.blade.php b/resources/views/comments/comments.blade.php index cfc89340d..99b21b9b2 100644 --- a/resources/views/comments/comments.blade.php +++ b/resources/views/comments/comments.blade.php @@ -1,4 +1,12 @@
{{ trans_choice('entities.comment_count', count($page->comments), ['count' => count($page->comments)]) }}
@if (count($page->comments) === 0) diff --git a/resources/views/components/image-manager.blade.php b/resources/views/components/image-manager.blade.php index 7c9084ad1..6781bca5f 100644 --- a/resources/views/components/image-manager.blade.php +++ b/resources/views/components/image-manager.blade.php @@ -1,4 +1,13 @@