Laravel 在 AppServiceProvider 中获取 getCurrentLocale()
Laravel get getCurrentLocale() in AppServiceProvider
我正在尝试在 Laravel AppServiceProvider
class 的 boot()
方法中获取 LaravelLocalization::getCurrentLocale()
,虽然我的默认语言环境是 pt 我总是得到 en。我使用的包是 mcamara/laravel-localization
。我有代码:
public function boot()
{
Schema::defaultStringLength(191);
// Twitter view share
$twitter = Twitter::getUserTimeline(['screen_name' => env('TWITTER_USER'), 'count' => 3, 'format' => 'object']);
view()->share('twitter', $twitter);
// Current language code view share
$language = LaravelLocalization::getCurrentLocale();
view()->share('lang', $language);
// Practice Areas
view()->share('practice_areas', \App\Models\PracticeArea::with('children')->orderBy('area_name')->where(['parent_id' => 0, 'language' => $language])->get());
}
我可能把它放在了错误的地方,因为当我尝试共享 practice_areas
变量时,它总是将其设置为 en,即使切换语言也是如此.
我可能做错了什么?
提前感谢您的帮助
来自包文档 Usage 部分:
Laravel Localization uses the URL given for the request. In order to achieve this purpose, a route group should be added into the routes.php file. It will filter all pages that must be localized.
您需要在路由组定义中设置本地化:
Route::group(['prefix' => LaravelLocalization::setLocale()], function()
{
/** ADD ALL LOCALIZED ROUTES INSIDE THIS GROUP **/
Route::get('/', function()
{
return View::make('hello');
});
Route::get('test',function(){
return View::make('test');
});
});
在尝试解决这个问题几个小时后,我决定不在这里使用 view()->share()
和 mcamara/laravel-localization 包方法。原因似乎是在 AppServiceProvider::class
boot()
方法中,程序包尚未获得请求的语言字符串。
无论如何,谢谢大家的帮助!
遇到了完全相同的问题,通过使用专用服务提供商和视图编辑器 class 解决了,像这样:
<?php
namespace App\Providers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class LocalizationServiceProvider extends ServiceProvider
{
public function boot() {
View::composer(
'*', 'App\Http\ViewComposers\LocalizationComposer'
);
}
}
然后在 LocalizationComposer class:
<?php
namespace App\Http\ViewComposers;
use Illuminate\View\View;
use LaravelLocalization;
class LocalizationComposer {
public function compose(View $view)
{
$view->with('currentLocale', LaravelLocalization::getCurrentLocale());
$view->with('altLocale', config('app.fallback_locale'));
}
}
currentLocale 和 altLocale 将在您的应用程序的所有视图中可用
我正在尝试在 Laravel AppServiceProvider
class 的 boot()
方法中获取 LaravelLocalization::getCurrentLocale()
,虽然我的默认语言环境是 pt 我总是得到 en。我使用的包是 mcamara/laravel-localization
。我有代码:
public function boot()
{
Schema::defaultStringLength(191);
// Twitter view share
$twitter = Twitter::getUserTimeline(['screen_name' => env('TWITTER_USER'), 'count' => 3, 'format' => 'object']);
view()->share('twitter', $twitter);
// Current language code view share
$language = LaravelLocalization::getCurrentLocale();
view()->share('lang', $language);
// Practice Areas
view()->share('practice_areas', \App\Models\PracticeArea::with('children')->orderBy('area_name')->where(['parent_id' => 0, 'language' => $language])->get());
}
我可能把它放在了错误的地方,因为当我尝试共享 practice_areas
变量时,它总是将其设置为 en,即使切换语言也是如此.
我可能做错了什么?
提前感谢您的帮助
来自包文档 Usage 部分:
Laravel Localization uses the URL given for the request. In order to achieve this purpose, a route group should be added into the routes.php file. It will filter all pages that must be localized.
您需要在路由组定义中设置本地化:
Route::group(['prefix' => LaravelLocalization::setLocale()], function()
{
/** ADD ALL LOCALIZED ROUTES INSIDE THIS GROUP **/
Route::get('/', function()
{
return View::make('hello');
});
Route::get('test',function(){
return View::make('test');
});
});
在尝试解决这个问题几个小时后,我决定不在这里使用 view()->share()
和 mcamara/laravel-localization 包方法。原因似乎是在 AppServiceProvider::class
boot()
方法中,程序包尚未获得请求的语言字符串。
无论如何,谢谢大家的帮助!
遇到了完全相同的问题,通过使用专用服务提供商和视图编辑器 class 解决了,像这样:
<?php
namespace App\Providers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class LocalizationServiceProvider extends ServiceProvider
{
public function boot() {
View::composer(
'*', 'App\Http\ViewComposers\LocalizationComposer'
);
}
}
然后在 LocalizationComposer class:
<?php
namespace App\Http\ViewComposers;
use Illuminate\View\View;
use LaravelLocalization;
class LocalizationComposer {
public function compose(View $view)
{
$view->with('currentLocale', LaravelLocalization::getCurrentLocale());
$view->with('altLocale', config('app.fallback_locale'));
}
}
currentLocale 和 altLocale 将在您的应用程序的所有视图中可用