I'm wondering if it's possible to reuse Localization translations in Laravel 5.2. So something like this, or better real global accesable keys to direct use brand
, instead of typing the folder or file name (In this example global
).
我想知道是否可以在Laravel 5.2中重用本地化翻译。这样的东西,或更好的真实全局可访问键来直接使用品牌,而不是键入文件夹或文件名(在此示例中为全局)。
// resources/lang/en/global.php
return [
'brand' => '*',
'my' => 'My :attribute',
'my_brand' => trans('global.my', ['attribute' => trans('global.brand')])
];
Hope their is a way to reuse translations with Laravel.
希望他们能够通过Laravel重用翻译。
2 个解决方案
#1
0
You should be able to use a View Composer to inject your translations globally into all the views of the application. The steps you should follow are the following.
您应该能够使用View Composer将翻译全局注入应用程序的所有视图中。您应遵循的步骤如下。
- Create a view composer
- Register the view composer to a Service Provider
- Inject your translations into the composer and bind it to all views using the wildcard operator.
创建一个视图编辑器
将视图编辑器注册到服务提供商
将您的翻译注入作曲家并使用通配符操作符将其绑定到所有视图。
The composer class should look like this and it's namespace should be App\Http\ViewComposers (App can be different based on the namespace of your application)
composer类应该看起来像这样,它的命名空间应该是App \ Http \ ViewComposers(App可以根据应用程序的命名空间而有所不同)
class TranslationsComposer
{
protected $translations;
public function __construct()
{
$this->translations = [
'brand' => trans('global.brand'),
'my' => trans('global.my'),
];
}
public function compose(View $view)
{
$view->with('translations', $this->translations);
}
}
You could use a loop to get the translations but this is a simple example just to demonstrate the way.
您可以使用循环来获取翻译,但这是一个简单的示例,只是为了演示方式。
Now for the service provider you should use something like this
现在对于服务提供商,你应该使用这样的东西
namespace App\Http\ViewComposers;
use Illuminate\Contracts\View\View;
use Illuminate\Users\Repository as UserRepository;
class ComposerServiceProvider extends ServiceProvider
{
public function boot()
{
view()->composer(
'*','App\Http\ViewComposers\TranslationsComposer'
);
}
public function register()
{
//
}
}
After doing so you should be able to use these variables as {{translations.brand}} in your blade templates.
完成后,您应该能够在刀片模板中将这些变量用作{{translations.brand}}。
You can find more about view composers at the official Laravel documentation https://laravel.com/docs/5.1/views
您可以在Laravel官方文档https://laravel.com/docs/5.1/views上找到有关视图作曲家的更多信息。
#2
0
You can create your own helper file to resolve the lang variable.
您可以创建自己的帮助文件来解析lang变量。
Step 1: Create a file with .php extension in App\Helpers. Any file name (with .php extension) should load automatically by Helper ServiceProvider. In your HelperServiceProvider you should find some code as below:
步骤1:在App \ Helpers中创建扩展名为.php的文件。任何文件名(扩展名为.php)都应由Helper ServiceProvider自动加载。在你的HelperServiceProvider中,你应该找到一些代码如下:
public function register()
{
foreach (glob(app_path().'/Helpers/*.php') as $filename){
require_once($filename);
}
}
Step 2: Create a function in your new helper file as below:
第2步:在新的帮助文件中创建一个函数,如下所示:
if(! function_exists('my_trans')){
function my_trans($key){
// load all the lang variable as an array here which should look like as below
$lang = [
'key1'=>'value1',
'key2'=>'value2',
'key3'=>'value3',
'key4'=>'value4',
'key5'=>'value5'
];
return $lang[$key];
}
}
Step 3: Call the function with key from your blade file as below:
步骤3:使用刀片文件中的密钥调用该功能,如下所示:
{{my_trans('key2')}}
This is working example and should help you to extend your idea.
这是一个有效的例子,可以帮助您扩展您的想法。
#1
0
You should be able to use a View Composer to inject your translations globally into all the views of the application. The steps you should follow are the following.
您应该能够使用View Composer将翻译全局注入应用程序的所有视图中。您应遵循的步骤如下。
- Create a view composer
- Register the view composer to a Service Provider
- Inject your translations into the composer and bind it to all views using the wildcard operator.
创建一个视图编辑器
将视图编辑器注册到服务提供商
将您的翻译注入作曲家并使用通配符操作符将其绑定到所有视图。
The composer class should look like this and it's namespace should be App\Http\ViewComposers (App can be different based on the namespace of your application)
composer类应该看起来像这样,它的命名空间应该是App \ Http \ ViewComposers(App可以根据应用程序的命名空间而有所不同)
class TranslationsComposer
{
protected $translations;
public function __construct()
{
$this->translations = [
'brand' => trans('global.brand'),
'my' => trans('global.my'),
];
}
public function compose(View $view)
{
$view->with('translations', $this->translations);
}
}
You could use a loop to get the translations but this is a simple example just to demonstrate the way.
您可以使用循环来获取翻译,但这是一个简单的示例,只是为了演示方式。
Now for the service provider you should use something like this
现在对于服务提供商,你应该使用这样的东西
namespace App\Http\ViewComposers;
use Illuminate\Contracts\View\View;
use Illuminate\Users\Repository as UserRepository;
class ComposerServiceProvider extends ServiceProvider
{
public function boot()
{
view()->composer(
'*','App\Http\ViewComposers\TranslationsComposer'
);
}
public function register()
{
//
}
}
After doing so you should be able to use these variables as {{translations.brand}} in your blade templates.
完成后,您应该能够在刀片模板中将这些变量用作{{translations.brand}}。
You can find more about view composers at the official Laravel documentation https://laravel.com/docs/5.1/views
您可以在Laravel官方文档https://laravel.com/docs/5.1/views上找到有关视图作曲家的更多信息。
#2
0
You can create your own helper file to resolve the lang variable.
您可以创建自己的帮助文件来解析lang变量。
Step 1: Create a file with .php extension in App\Helpers. Any file name (with .php extension) should load automatically by Helper ServiceProvider. In your HelperServiceProvider you should find some code as below:
步骤1:在App \ Helpers中创建扩展名为.php的文件。任何文件名(扩展名为.php)都应由Helper ServiceProvider自动加载。在你的HelperServiceProvider中,你应该找到一些代码如下:
public function register()
{
foreach (glob(app_path().'/Helpers/*.php') as $filename){
require_once($filename);
}
}
Step 2: Create a function in your new helper file as below:
第2步:在新的帮助文件中创建一个函数,如下所示:
if(! function_exists('my_trans')){
function my_trans($key){
// load all the lang variable as an array here which should look like as below
$lang = [
'key1'=>'value1',
'key2'=>'value2',
'key3'=>'value3',
'key4'=>'value4',
'key5'=>'value5'
];
return $lang[$key];
}
}
Step 3: Call the function with key from your blade file as below:
步骤3:使用刀片文件中的密钥调用该功能,如下所示:
{{my_trans('key2')}}
This is working example and should help you to extend your idea.
这是一个有效的例子,可以帮助您扩展您的想法。