parfumix / laravel-localization
Laravel的本地化
v1.0
2015-08-25 11:41 UTC
Requires
- php: >=5.4.0
- laravel/framework: ~5.0
This package is auto-updated.
Last update: 2024-09-19 21:59:46 UTC
README
##介绍
Laravel本地化是一个组件,它将帮助你通过各种检测器(见下文)轻松地检测你的区域设置。第一个返回检测器的检测器将被用作找到的区域设置。
安装
您可以使用composer
包管理器进行安装。在控制台中运行
$ php composer.phar require parfumix/laravel-localization "v1.0"
或在您的composer.json文件中添加
"parfumix/laravel-meta": "v1.0"
您必须使用以下命令发布包文件
$ php artisan vendor:publish
配置
要注册包,您必须遵循注册serviceProvider类的标准程序。打开位于config/app.php的配置文件,并查找提供者数组
'providers' => [ // Add that line at the end of array .. 'Localization\LocaleServiceProvider' ]
##基本用法 要编辑您的本地化组件,您必须首先使用以下命令发布您的配置文件
$ php artisan vendor:publish
如果您想添加或删除某些区域设置,可以轻松地打开您的配置文件并编辑该部分
locales: en: title: Enlgish country: US active: true ro: title: Romanian country: RO active: false ru: title: Russian active: false
组件如何尝试检测区域设置。它遍历配置文件中声明的所有检测器,如果某个类的类返回一个有效的区域设置,它将被用作找到的区域设置并设置到应用程序中
detectors: - Localization\Detectors\Request - Localization\Detectors\Browser - Localization\Detectors\System
如您所见,这里有一个类列表,因此您可以轻松更改它们的顺序或添加新的检测器。下面是如何添加新的检测器类。
###扩展 您可以将您的检测器放在任何地方,但最重要的规则是实现Localization\Detectable接口,这将迫使您实现一个detect()函数
检测器的示例
<?php namespace Your/Namespace; use Localization\Detectable; class YourDetector implements Detectable { public function detect($request) { // return founded locale . } }
并将该类添加到您的配置文件中
# here will be described class detectors, they will be called in the same order detectors: - Your\Namespace\YourDetector - Localization\Detectors\Browser - Localization\Detectors\System
如您所见,您的检测器将被首先调用。
##Laravel集成 要使用该包与Laravel路由包一起使用,您必须编辑您的配置文件并排除您不需要的检测器
# That mean Localization will walk through all detectors in the same order from the top to bottom and first locale which will be detected will be set as active locale App::locale('active_locale') detectors: request: Localization\Detectors\Request // will detect locale from first url segment site.com/ru - ru will browser: Localization\Detectors\Browser // will detect locale from request headers * optional system: Localization\Detectors\System // will detect locale from system * optional
对于细分,您必须编辑您的routes.php文件并遵循以下结构
// Here we will try detect and set as prefix first url segment . if not founded will be set null and will try to detect from below detectors . Route::group(['prefix' => Localization\get_detector_locale('request')], function() { Route::get('/', function() { return view('welcome.blade.php') }) });
###助手
默认情况下,您可以使用一些助手,它们将帮助您轻松地处理区域设置。让我描述一下这些助手。
Localization\get_locales() // return all registered locales in config file Localization\get_default_locale() // will return default locale Localization\get_active_locale() // will return active detected locale Localization\format_locale() // format your locale and return formatter instance