waterloomatt/translation

根据当前控制器/操作动态处理翻译。

1.0.5 2016-02-05 17:20 UTC

This package is auto-updated.

Last update: 2024-09-12 06:51:47 UTC


README

允许您的应用程序轻松使用动态翻译

安装

Composer

运行以下命令以通过Composer包含此插件:

composer require waterloomatt/translation

Laravel 5 配置

通过在config/app.php中添加以下行将服务提供者注册到您的应用程序:

'providers' => [
  Waterloomatt\Translation\Providers\TranslationServiceProvider::class

这就完成了!您已经可以使用了。

用法

在视图中,像平常一样使用Laravel的翻译助手。这里不需要做任何更改。

{{ trans('messages.pageTitle') }}

现在,有趣的部分来了!在这个示例中,您可以通过指定翻译应应用于哪个控制器或操作(或两者)来覆盖'pageTitle'翻译。

在您的messages.php翻译文件中,通过指定控制器或操作(或两者)来覆盖'pageTitle'翻译。

return [
  'pageTitle'                                   => 'My Application!'  // Applies to all pages
  'controller:search_key:pageTitle'             => 'Search',          // Applies to /search/{any_action}
  'action:index_key:pageTitle'                  => 'All Index Pages!',// Applies to /{any_controller}/index
  'controller:user_action:update_key:pageTitle' => 'Update User',     // Applies to user/update
];

如你所见,您可以通过指定控制器操作或两者来控制使用哪个翻译。将使用与当前路由最匹配的最具体翻译。

  • 每个组件使用冒号(:)将名称与其值分开,例如controller:usercontroller:payment
  • 每个组件使用下划线(_)将其与其他组件分开,例如controller:user_action:updatecontroller:payment_action:decline_key:pageTitle
  • 第三个组件是,它简单地定义了翻译键。
  • 控制器和操作都是可选的

重要!翻译是按照最具体到最不具体的顺序进行搜索的。

  1. controller:name_action:name_key:name
  2. action:name_key:name
  3. controller:name_key:name
  4. name

在以下示例中,假设您的当前路由是https:///public/search/index

  • 示例 1
return [
  'controller:search_key:pageTitle'               => 'Search',    
  'controller:search_action:index_key:pageTitle'  => 'Search',    // this one!
];
  • 示例 2
return [
  'action:index_key:pageTitle'                    => 'Search',  
  'pageTitle'                                     => 'Search',    // this one!
];
  • 示例 3
return [
  'controller:search_action:result_key:pageTitle' => 'Search',    
  'controller:search_action:index_key:pageTitle'  => 'Search',    // this one!
];
  • 示例 4
return [
  'controller:search_key:pageTitle'               => 'Search',    
  'action:index_key:pageTitle'                    => 'Search',    // this one!
];