How can Laravel implement multi-language functionality?
In Laravel, implementing multilingual functionality mainly involves the following steps:
- app.php configuration file
'locale' => 'en',
'locales' => ['en', 'zh'],
- language resources
- Could you please repeat that in English?
- I will only provide one option natively in English.
- The file messages.php
// resources/lang/en/messages.php
return [
'welcome' => 'Welcome to our website!',
];
// resources/lang/zh/messages.php
return [
'welcome' => '欢迎来到我们的网站!',
];
- Could you help me with this task?
// 在视图文件中
{{ trans('messages.welcome') }}
// 在控制器中
return view('welcome')->with('message', trans('messages.welcome'));
- locality
app()->setLocale('zh');
By following the steps above, multi-language functionality can be implemented in Laravel.