【问题标题】:LaravelLocalization failing to translate routes of nwidart laravel modulesLaravelLocalization 无法翻译 nwidart laravel 模块的路由
【发布时间】:2019-06-13 15:17:27
【问题描述】:

我无法弄清楚为什么 LaravelLocalization 无法翻译我在使用 nwidart laravel-modules 包为 laravel 创建的模块的 routes.php (Modules/ModuleName/Http/routes.php) 文件中声明的路由, 'localize' (\Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRoutes::class) 中间件也存在于应用程序的 Kernel.php 和路由组中,如here 所述。该路线未翻译并显示为 /en/booking::all.companies.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');
});

但是当模块命名空间前缀“booking::”从翻译字符串中移除时 (LaravelLocalization::transRoute('all.companies.index') 而不是 LaravelLocalization::transRoute('booking::all.companies.index') ) 它可以翻译路线。

请帮我解决问题,谢谢。

(如果有帮助,我的安装:Laravel Framework 5.5.43,“mcamara/laravel-localization”:“1.3”,“nwidart/laravel-modules”:“2.7”。除了 mcamara/laravel 之外,没有安装其他本地化包-本地化)

【问题讨论】:

  • 您找到解决方案了吗?
  • 很遗憾没有。我将路由翻译放在标准的 laravel 资源/lang 目录中。命名空间的路线名称(例如:“booking::”,例如放在 Modules/Booking/Resources/lang 目录中)没有被 mcamara/laravel-localization 包正确翻译。

标签: php module laravel-5.5 laravel-routing


【解决方案1】:

我遇到了同样的问题,但我没有在网上找到解决方案。以下是我解决问题的方法:

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();
}

【讨论】:

  • 我可以确认它正在工作,谢谢@apalette。我已经接受了答案。
猜你喜欢
  • 1970-01-01
  • 2020-07-14
  • 2017-04-15
  • 2018-09-02
  • 2016-12-02
  • 2021-10-21
  • 2021-11-10
  • 2016-03-03
相关资源
最近更新 更多