我想根据URL的i18N参数来更改某些表名,例如:https://localhost/fr,https://localhost/en,... 该表名将如下所示:tablename_fr,tablename_en 我想这样做是为了尽可能简化我的网站的i18n转换。我如何使用Laravel做到这一点?您看到此系统的性能问题吗?也许会打扰Laravel / Eloquent缓存吗?
答案 0 :(得分:0)
# resources/lang/en/tables
// English names for tables
return [
'cars' => 'cars'
'computers' => 'computers'
]
# resources/lang/fr/tables.php
// French names for tables
return [
'cars' => 'voitures'
'computers' => 'ordinateurs'
]
然后,在您的视图中,您可以使用url()
帮助程序来形成url。假设我们有一个$car
和一个id: 1
url(__('tables.cars').'/'.$car->id)
// if App::getLocale() === 'en', it returns /cars/1
// if App::getLocale() === 'fr', it returns /voitures/1
// if App::getLocale() === 'es', it returns /cars/1 because there's no 'es' lang file in this example and by default, 'en' is the fallback language.
您确实需要设置其他路由规则。
# routes/web.php
// You could group the routes to add a prefix, but the idea is the same
Route::get('cars/{car}', CarController@show)->name('en.car.show');
Route::get('voitures/{car}', CarController@show)->name('fr.car.show');
# in a view
route(App::getLocale().'.car.show', [$car->id])
// returns either cars/1 or voitures/1 depending on the locale