我不明白为什么LaravelLocalization不能翻译我在routes.php (Modules/ModuleName/Http/routes.php)文件中声明的路由,这个模块是我用nwidart laravel-modules包为laravel创建的,'localize‘(\Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRoutes::class)中间件也存在于应用的Kernel.php和路由组中,如here所述。路由不会被翻译,并且显示为/en/booking::all.companyies.index而不是/en/companies (或/ru/kompanii):
Route::group(
[
'prefix' => LaravelLocalization::setLocale(),
'middleware' => ['web', 'auth', 'localize', 'optimizeImages'],
'namespace' => 'Modules\Booking\Http\Controllers',
],
function() {
Route::get(LaravelLocalization::transRoute('booking::all.companies.index'), 'CompanyController@index')->name('booking.companies.index');
});但是当从转换字符串(LaravelLocalization::transRoute('all.companies.index')而不是LaravelLocalization::transRoute('booking::all.companies.index'))中删除模块命名空间前缀"booking::“时,它可以转换路由。
请帮我解决这个问题,谢谢。
(我的安装: Laravel Framework 5.5.43,"mcamara/laravel-localization":"1.3","nwidart/laravel-modules":"2.7“。除了mcamara/laravel- localization之外,没有安装其他本地化包)
发布于 2020-01-24 16:50:54
我也有同样的问题,但我没有在网上找到解决方案。下面是我是如何解决这个问题的:
在Modules\[MyModule]\Providers\RouteServiceProvider中
/**
* Register translations.
*
* @return void
*/
protected function registerTranslations()
{
$module_path = 'MyModule';
$module_slug = 'my-module';
$langPath = resource_path('lang/modules/'.$module_slug);
if (is_dir($langPath)) {
$this->loadTranslationsFrom($langPath, $module_slug);
} else {
$this->loadTranslationsFrom(module_path(module_path, 'Resources/lang'),$module_slug);
}
}
/**
* Define the routes for the application.
*
* @return void
*/
public function map()
{
$this->registerTranslations();
$this->mapApiRoutes();
$this->mapWebRoutes();
}https://stackoverflow.com/questions/54269537
复制相似问题