From 0fd3c9a31326c95a60d07ccc56e40ab236d1e127 Mon Sep 17 00:00:00 2001 From: Maicol Battistini Date: Tue, 9 Nov 2021 10:14:08 +0100 Subject: [PATCH] =?UTF-8?q?impr:=20=E2=9A=A1=20Migliorata=20i18n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Spostata funzione di traduzione JS all'interno di `utils.js` e poi portata in una variabile globale. Le traduzioni sono ora salvate nella cache. --- .eslintrc.yml | 2 + app/Http/Middleware/HandleInertiaRequests.php | 24 ++------- app/Providers/AppServiceProvider.php | 8 ++- resources/js/Components/Page.jsx | 44 ---------------- resources/js/Components/Pages/RecordsPage.jsx | 24 ++++----- resources/js/Components/extend.js | 2 - resources/js/Views/SetupPage.jsx | 52 +++++++++---------- resources/js/app.js | 3 ++ resources/js/utils.js | 44 ++++++++++++++++ resources/views/app.blade.php | 3 ++ resources/views/external.blade.php | 2 + .../views/layouts/translations.blade.php | 7 +++ 12 files changed, 109 insertions(+), 106 deletions(-) create mode 100644 resources/views/layouts/translations.blade.php diff --git a/.eslintrc.yml b/.eslintrc.yml index 1b70db625..55b42a2d6 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -1,3 +1,5 @@ root: true extends: - '@openstamanager' +globals: + __: true diff --git a/app/Http/Middleware/HandleInertiaRequests.php b/app/Http/Middleware/HandleInertiaRequests.php index 24619f183..daf3aed1a 100644 --- a/app/Http/Middleware/HandleInertiaRequests.php +++ b/app/Http/Middleware/HandleInertiaRequests.php @@ -4,7 +4,6 @@ namespace App\Http\Middleware; use Illuminate\Http\Request; use Inertia\Middleware; -use Nette\Utils\Json; class HandleInertiaRequests extends Middleware { @@ -17,41 +16,24 @@ class HandleInertiaRequests extends Middleware */ protected $rootView = 'app'; - /** - * Determines the current asset version. - * - * @see https://inertiajs.com/asset-versioning - */ - public function version(Request $request): string|null - { - return parent::version($request); - } - /** * Defines the props that are shared by default. * * @see https://inertiajs.com/shared-data */ - public function share(Request $request): array + final public function share(Request $request): array { return array_merge(parent::share($request), [ 'locale' => fn () => app()->getLocale(), - 'translations' => function () { - $json = resource_path('lang/'.app()->getLocale().'.json'); - if (!is_file($json)) { - return []; - } - - return Json::decode(file_get_contents($json)); - }, ]); } - public function rootView(Request $request): string + final public function rootView(Request $request): string { if (in_array($request->route()?->uri(), ['setup', 'login'], true)) { return 'external'; } + return $this->rootView; } } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 452e6b65b..721ddc584 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -3,6 +3,7 @@ namespace App\Providers; use Illuminate\Support\ServiceProvider; +use Nette\Utils\Json; class AppServiceProvider extends ServiceProvider { @@ -19,6 +20,11 @@ class AppServiceProvider extends ServiceProvider */ public function boot(): void { - // + cache()->rememberForever( + 'translations_' . app()->getLocale(), + fn () => Json::encode( + Json::decode(file_get_contents(resource_path('lang/'.app()->getLocale().'.json'))) + ) + ); } } diff --git a/resources/js/Components/Page.jsx b/resources/js/Components/Page.jsx index 355453a51..c9fedb228 100644 --- a/resources/js/Components/Page.jsx +++ b/resources/js/Components/Page.jsx @@ -1,7 +1,3 @@ -import {Vnode} from 'mithril'; -import {sync as render} from 'mithril-node-render'; - -import {containsHTML} from '../utils'; import Component from './Component.jsx'; /** @@ -14,48 +10,8 @@ export default class Page extends Component { component: string, locale: string, props: {...}, - translations: {...}, url: string, version: string, ... } = JSON.parse($('#app').attr('data-page')); - - /** - * Ritorna una traduzione - * - * @param {string|Vnode} key Stringa di cui prelevare la traduzione - * @param {Object|boolean} replace Eventuali parametri da rimpiazzare. - * Se il parametro è "true" (valore booleano), verrà ritornato il valore come stringa - * (stesso funzionamento del parametro dedicato (sotto ↓)) - * @param {boolean} returnAsString Se impostato a "true" vien ritornata una stringa invece di - * un Vnode di Mithril - * - * @returns {Vnode} - * - * @protected - */ - __( - key: string | Vnode, - replace: {...} | boolean = {}, - returnAsString: boolean = false - ): Vnode { - let translation = (this.page.translations && this.page.translations[key]) - ? this.page.translations[key] : key; - - // Ritorna la traduzione come stringa (senza sostituzione di parametri) - if ((typeof replace === 'boolean' && replace) || (replace.length === 0 && !containsHTML(translation))) { - return translation; - } - - for (const k of Object.keys(replace)) { - // `'attrs' in replace[k]` controlla se replace[k] è un vnode di Mithril - translation = translation.replace(`:${k}`, ((typeof replace[k] === 'object' && 'attrs' in replace[k]) ? render(replace[k]) : replace[k])); - } - - if (returnAsString || !containsHTML(translation)) { - return translation; - } - - return window.m.trust(translation); - } } diff --git a/resources/js/Components/Pages/RecordsPage.jsx b/resources/js/Components/Pages/RecordsPage.jsx index 252b0522b..a4e50f0a2 100644 --- a/resources/js/Components/Pages/RecordsPage.jsx +++ b/resources/js/Components/Pages/RecordsPage.jsx @@ -145,7 +145,7 @@ export default class RecordsPage extends Page { - {this.__('Non sono presenti dati')} + {__('Non sono presenti dati')} ); } @@ -192,7 +192,7 @@ export default class RecordsPage extends Page { this.rows.forget(instance.id); m.redraw(); - await showSnackbar(this.__('Record eliminato!'), 4000); + await showSnackbar(__('Record eliminato!'), 4000); }); loading.hide(); @@ -208,7 +208,7 @@ export default class RecordsPage extends Page { recordDialog() { return (