Skip to content

Commit 401e184

Browse files
committed
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.
1 parent b39d11c commit 401e184

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace App\Providers;
4+
5+
use App\Services\SnipeTranslator;
6+
use Illuminate\Translation\TranslationServiceProvider;
7+
8+
class SnipeTranslationServiceProvider extends TranslationServiceProvider
9+
{
10+
/**
11+
* Register services.
12+
*
13+
* @return void
14+
*/
15+
public function register()
16+
{
17+
//This is almost an *EXACT* carbon-copy of the TranslationServiceProvider, except with a modified Translator
18+
$this->registerLoader();
19+
20+
$this->app->singleton('translator', function ($app) {
21+
$loader = $app['translation.loader'];
22+
23+
// When registering the translator component, we'll need to set the default
24+
// locale as well as the fallback locale. So, we'll grab the application
25+
// configuration so we can easily get both of these values from there.
26+
$locale = $app['config']['app.locale'];
27+
28+
$trans = new SnipeTranslator($loader, $locale); //the ONLY changed line
29+
30+
$trans->setFallback($app['config']['app.fallback_locale']);
31+
32+
return $trans;
33+
});
34+
}
35+
}

app/Services/SnipeTranslator.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace App\Services;
4+
5+
use Illuminate\Translation\Translator;
6+
7+
/***************************************************************
8+
* This is just a very, very light modification to the default Laravel Translator.
9+
* The only difference it has is that it modifies the $locale
10+
* value when the pluralizations are done so we can switch over from en-US to en_US (for example).
11+
*
12+
* This means our translation directories can stay where they are (en-US), but the
13+
* pluralization code can get executed against a locale of en_US
14+
* (Which is required by Symfony, for some inexplicable reason)
15+
*
16+
* This method is called by the trans_choice() helper, which we *do* use a lot.
17+
***************************************************************/
18+
class SnipeTranslator extends Translator {
19+
20+
//This is copied-and-pasted (almost) verbatim from Illuminate\Translation\Translator
21+
public function choice($key, $number, array $replace = [], $locale = null)
22+
{
23+
$line = $this->get(
24+
$key, $replace, $locale = $this->localeForChoice($locale)
25+
);
26+
27+
// If the given "number" is actually an array or countable we will simply count the
28+
// number of elements in an instance. This allows developers to pass an array of
29+
// items without having to count it on their end first which gives bad syntax.
30+
if (is_array($number) || $number instanceof Countable) {
31+
$number = count($number);
32+
}
33+
34+
$replace['count'] = $number;
35+
36+
$underscored_locale = str_replace("-","_",$locale); // OUR CHANGE.
37+
return $this->makeReplacements( // BELOW - that $underscored_locale is the *ONLY* modified part
38+
$this->getSelector()->choose($line, $number, $underscored_locale), $replace
39+
);
40+
}
41+
42+
}

config/app.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,8 @@
277277
Illuminate\Redis\RedisServiceProvider::class,
278278
Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
279279
Illuminate\Session\SessionServiceProvider::class,
280-
Illuminate\Translation\TranslationServiceProvider::class,
280+
// Illuminate\Translation\TranslationServiceProvider::class, //replaced on next line
281+
App\Providers\SnipeTranslationServiceProvider::class, //we REPLACE the default Laravel translator with our own
281282
Illuminate\Validation\ValidationServiceProvider::class,
282283
Illuminate\View\ViewServiceProvider::class,
283284
Barryvdh\DomPDF\ServiceProvider::class,

0 commit comments

Comments
 (0)