Laravel trans 辅助函数没有选择正确的文件
Laravel trans helper function not picking the right file
我正在 Laravel 建立网站,
我让它获取浏览器语言并相应地设置网站的语言。
问题是,Registrar.php seems to get translated in English or Dutch
中什么都没有
谁能帮帮我?
谢谢,
g3
要完成这项工作,您需要使用中间件来设置语言环境:
在 app/http/middleware
目录中创建一个 LocaleMiddleware.php
//LocaleMiddleware.php
<?php namespace App\Http\Middleware;
use Closure;
use Illuminate\Session\Store as Session;
use Illuminate\Contracts\Auth\Guard as Auth;
class LocaleMiddleware {
public function __construct(Session $session)
{
$this->session = $session;
}
//Languages available in your resources/lang
protected $languages = ['en','es', 'nl-be'];
public function handle($request, Closure $next)
{
$langlist = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
// We just want the main language
$lang = substr($langlist,0,2);
if(isset($this->languages[$lang])){
app()->setLocale($lang);
}else{
//You may log this here
}
return $next($request);
}
}
然后在$middleware
数组中注册app\httpe.kernel.php
中的中间件
/**
* The application's global HTTP middleware stack.
*
* @var array
*/
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
.........
\App\Http\Middleware\LocaleMiddleware::class,
];
正在创建语言文件
您需要为您想要支持的所有语言创建单独的文件夹,并相应地翻译其中的文件,例如 nl-be\validation.php
。
您可以先将 resources/lang/en to resources/lang/nl-be, then translate the contents of
nl-be\validation.php` 的内容翻译成荷兰语
我正在 Laravel 建立网站, 我让它获取浏览器语言并相应地设置网站的语言。
问题是,Registrar.php seems to get translated in English or Dutch
中什么都没有谁能帮帮我? 谢谢, g3
要完成这项工作,您需要使用中间件来设置语言环境:
在 app/http/middleware
目录中创建一个 LocaleMiddleware.php
//LocaleMiddleware.php
<?php namespace App\Http\Middleware;
use Closure;
use Illuminate\Session\Store as Session;
use Illuminate\Contracts\Auth\Guard as Auth;
class LocaleMiddleware {
public function __construct(Session $session)
{
$this->session = $session;
}
//Languages available in your resources/lang
protected $languages = ['en','es', 'nl-be'];
public function handle($request, Closure $next)
{
$langlist = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
// We just want the main language
$lang = substr($langlist,0,2);
if(isset($this->languages[$lang])){
app()->setLocale($lang);
}else{
//You may log this here
}
return $next($request);
}
}
然后在$middleware
数组中注册app\httpe.kernel.php
中的中间件
/**
* The application's global HTTP middleware stack.
*
* @var array
*/
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
.........
\App\Http\Middleware\LocaleMiddleware::class,
];
正在创建语言文件
您需要为您想要支持的所有语言创建单独的文件夹,并相应地翻译其中的文件,例如 nl-be\validation.php
。
您可以先将 resources/lang/en to resources/lang/nl-be, then translate the contents of
nl-be\validation.php` 的内容翻译成荷兰语