From 401e1842ee24f43f8d97cdc1de8df6803ebf9c41 Mon Sep 17 00:00:00 2001 From: Brady Wetherington Date: Fri, 26 Jan 2024 14:26:43 +0000 Subject: [PATCH] Fixed pluralization bug due to dashed-locale names instead of underscored Our locale directories are named things like 'en-US'. But the pluralization code used by Laravel (through Symfony) requires locale names to be in the format en_US. This change introduces a new Translator, SnipeTranslator, which is a tiny set of changes against the built-in one. It additionally adds a SnipeTranslationServiceProvider, which loads up the new Translator. --- .../SnipeTranslationServiceProvider.php | 35 ++++++++++++++++ app/Services/SnipeTranslator.php | 42 +++++++++++++++++++ config/app.php | 3 +- 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 app/Providers/SnipeTranslationServiceProvider.php create mode 100644 app/Services/SnipeTranslator.php diff --git a/app/Providers/SnipeTranslationServiceProvider.php b/app/Providers/SnipeTranslationServiceProvider.php new file mode 100644 index 000000000..f0ff33448 --- /dev/null +++ b/app/Providers/SnipeTranslationServiceProvider.php @@ -0,0 +1,35 @@ +registerLoader(); + + $this->app->singleton('translator', function ($app) { + $loader = $app['translation.loader']; + + // When registering the translator component, we'll need to set the default + // locale as well as the fallback locale. So, we'll grab the application + // configuration so we can easily get both of these values from there. + $locale = $app['config']['app.locale']; + + $trans = new SnipeTranslator($loader, $locale); //the ONLY changed line + + $trans->setFallback($app['config']['app.fallback_locale']); + + return $trans; + }); + } +} diff --git a/app/Services/SnipeTranslator.php b/app/Services/SnipeTranslator.php new file mode 100644 index 000000000..00107ede9 --- /dev/null +++ b/app/Services/SnipeTranslator.php @@ -0,0 +1,42 @@ +get( + $key, $replace, $locale = $this->localeForChoice($locale) + ); + + // If the given "number" is actually an array or countable we will simply count the + // number of elements in an instance. This allows developers to pass an array of + // items without having to count it on their end first which gives bad syntax. + if (is_array($number) || $number instanceof Countable) { + $number = count($number); + } + + $replace['count'] = $number; + + $underscored_locale = str_replace("-","_",$locale); // OUR CHANGE. + return $this->makeReplacements( // BELOW - that $underscored_locale is the *ONLY* modified part + $this->getSelector()->choose($line, $number, $underscored_locale), $replace + ); + } + +} \ No newline at end of file diff --git a/config/app.php b/config/app.php index 1b4f45e45..b2f14f378 100755 --- a/config/app.php +++ b/config/app.php @@ -277,7 +277,8 @@ return [ Illuminate\Redis\RedisServiceProvider::class, Illuminate\Auth\Passwords\PasswordResetServiceProvider::class, Illuminate\Session\SessionServiceProvider::class, - Illuminate\Translation\TranslationServiceProvider::class, +// Illuminate\Translation\TranslationServiceProvider::class, //replaced on next line + App\Providers\SnipeTranslationServiceProvider::class, //we REPLACE the default Laravel translator with our own Illuminate\Validation\ValidationServiceProvider::class, Illuminate\View\ViewServiceProvider::class, Barryvdh\DomPDF\ServiceProvider::class,