63 lines
2.1 KiB
PHP
63 lines
2.1 KiB
PHP
<?php namespace App\Providers;
|
|
|
|
use Illuminate\Database\QueryException;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\ServiceProvider;
|
|
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
|
|
|
|
class LaravelLocalizationServiceProvider extends ServiceProvider
|
|
{
|
|
public function register()
|
|
{
|
|
try {
|
|
$languages = DB::table('languages')
|
|
->get();
|
|
} catch (QueryException $e) {
|
|
$supportedLocales[config('app.locale')] = [
|
|
'native' => env('LOCALE_NATIVE_NAME', config('app.locale')),
|
|
'script' => env('LOCALE_SCRIPT', 'Latn'),
|
|
'name' => env('LOCALE_TRANSLATED_NAME', config('app.locale')),
|
|
'code' => config('app.locale'),
|
|
];
|
|
$this->setSupportedLocales($supportedLocales);
|
|
return;
|
|
}
|
|
|
|
$supportedLocales = [];
|
|
if (empty($languages)) {
|
|
$supportedLocales[config('app.locale')] = [
|
|
'native' => env('LOCALE_NATIVE_NAME', config('app.locale')),
|
|
'script' => env('LOCALE_SCRIPT', 'Latn'),
|
|
'name' => env('LOCALE_TRANSLATED_NAME', config('app.locale')),
|
|
'code' => config('app.locale'),
|
|
];
|
|
// create default language
|
|
DB::table('languages')
|
|
->insert($supportedLocales[config('app.locale')]);
|
|
}
|
|
|
|
foreach ($languages as $language) {
|
|
$supportedLocales[$language->code] = [
|
|
'native' => $language->native,
|
|
'name' => $language->name,
|
|
'script' => $language->script
|
|
];
|
|
}
|
|
|
|
$this->setSupportedLocales($supportedLocales);
|
|
}
|
|
|
|
public function boot()
|
|
{
|
|
\App::setLocale(LaravelLocalization::getCurrentLocale());
|
|
}
|
|
|
|
private function setSupportedLocales($supportedLocales)
|
|
{
|
|
config([
|
|
'laravellocalization.supportedLocales' => $supportedLocales,
|
|
'laravellocalization.useAcceptLanguageHeader' => true,
|
|
'laravellocalization.hideDefaultLocaleInURL' => true
|
|
]);
|
|
}
|
|
} |