hopefeda/cookielize

此包已被弃用且不再维护。未建议替代包。

此包通过cookies为您的laravel应用添加更改区域/语言功能。

v1.3 2018-11-16 23:59 UTC

This package is auto-updated.

Last update: 2021-05-15 01:48:16 UTC


README

该项目已被弃用且不再维护。

Cookielize

此包通过cookies为您的laravel应用添加更改区域/语言功能。

工作原理

当用户访问一个“触发”URL时,该包会发送一个包含请求的区域设置的cookie。然后,一个中间件检查这个cookie并根据需要更改应用的区域。

安装

安装很简单,设置类似于其他laravel包。

1. 通过composer安装

composer require hopefeda/cookielize

2. 定义服务提供者和别名
注意:如果您使用laravel 5.5及以上版本,可以跳过此步骤,因为此包支持“自动发现”。

如果您使用Laravel 5.0 - 5.4,则需要添加提供者和别名。在您的config/app.php文件中定义一个新的服务提供者。

'providers' => [
	//  other providers

	hopefeda\Cookielize\CookielizeServiceProvider::class,
];

然后我们想在同一个config/app.php文件中定义一个别名。

'aliases' => [
	// other aliases

	'Cookielize' => hopefeda\Cookielize\facades\CookielizeFacade::class,
];

3. 发布配置文件
配置文件允许您覆盖此包的默认设置以满足您的特定需求。它还允许您更改受支持的语言列表。

在终端中键入以下命令以生成配置文件

php artisan vendor:publish  --provider=hopefeda\Cookielize\CookielizeServiceProvider

这将在config/cookielize.php生成配置文件。

用法

此包非常易于使用。安装后,当用户访问“触发”URL(默认为www.yourwebsite.com/languages/en,<--其中en是语言代码。)时,如果它位于受支持的语言列表中,则将为他们设置应用程序区域。

一些示例
https://www.yourwebsite.com/languages/ar <-- 设置区域为阿拉伯语
https://www.yourwebsite.com/languages/fr <-- 设置区域为法语

函数

1. LocaleRoute() - 返回触发路径
此函数用于在视图中生成触发器(区域更改)URL。它使用“trigger_path”来完成此操作。对配置文件中“trigger_path”的任何更改都将自动反映。

// Example 1
{{Cookielize::LocaleRoute('en')}} //gives /languages/en

// Example 2
<a href="{{Cookielize::LocaleRoute('tr')}}">Turkish</a> //gives <a href="/languages/tr">Turkish</a>

2. LocaleSupported() - 检查区域是否受支持,返回布尔值
此函数用于检查区域是否在“supported_languages”列表中。它返回“true”或“false”。

// Example
@if (Cookielize::LocaleSupported('en'))
// do somethinng
@endif  

3. SupportedLocales() - 获取受支持区域的数组
此函数用于从config/cookielize.php文件中的“supported_locales”获取受支持区域的列表。

// Example
@foreach(Cookielize::SupportedLocales() as $locale)
	<a href="{{Cookielize::LocaleRoute($locale)}}">{{$locale}}</a>
@endforeach

// Gives
<a href="/languages/en">en</a>
<a href="/languages/ar">ar</a>

4. CurrentLocale() - 获取当前设置的应用程序区域
此函数用于获取当前应用程序区域。它等同于config('app.locale')

// Example
{{Cookielize::CurrentLocale()}} //Gives en

注意:如果您想在控制器中使用Cookielize函数,不要忘记在控制器开始时添加use Cookielize;

// Example
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Cookielize;

class TestController extends Controller
{
    public function something()
    {
		if (Cookielize::LocaleSupported('en')) {
			// do something
		}
        return Cookielize::LocaleRoute('en');
    }
}

可配置项

您可以从 config/cookielize.php 文件中配置各种属性。

/*
|--------------------------------------------------------------------------
| trigger_path
|--------------------------------------------------------------------------
|
| This is the path that will trigger the locale/language change. It should 
| contain a "{locale}" parameter which will be replaced by the locale code 
| while linking. This path starts from the root of your application url
| (APP_URL in your .env file).
|
| Example 1: '/languages/{locale}'
| When linked: http://www.yourApplicationURL.com/languages/en
| 
| Example 2: '/{locale}'
| When linked: https://www.yourApplicationURL.com/en
|
*/
'trigger_path' => '/languages/{locale}',





/*
|--------------------------------------------------------------------------
| redirect_after_change_to
|--------------------------------------------------------------------------
|
| This is the url to redirect to after changing the locale.
|
| 'back' : Redirect back to the page from which the request came
| '\' : Redirect back to your public root
| 
| You can also put any other url or path.
|
*/
'redirect_after_change_to' => 'back',





/*
|--------------------------------------------------------------------------
| fallback_path
|--------------------------------------------------------------------------
|
| Sometimes, when 'back' is selected above in 'redirect_after_change_to',
| the back url might be a link back to the trigger path. This will cause
| an endless loop and will eventually trigger a 'too many redirects' error.
| To prevent such a thing when this happens, the user will be redirected to 
| this path instead.
|
*/
'fallback_path' => '/',





/*
|--------------------------------------------------------------------------
| unsupported_language_action
|--------------------------------------------------------------------------
|
| What to do when the requested language is not supported? By default, it
| will throw a 404 page not found exception(When 404 is entered). 
| Alternatively, you can enter a path to redirect to.
| For example: '/pages/not-supported'
|
*/
'unsupported_language_action' => '404',





/*
|--------------------------------------------------------------------------
| supported_languages
|--------------------------------------------------------------------------
|
| This is an array of supported languages respresented by their 2 letter
| code. The application locale will be set to the selected one from these.
|
*/
'supported_languages' => ['en', 'ar'],

贡献

我鼓励您为此包做出贡献,以改进它并使其变得更好。即使您对编码或提交拉取请求(PR)感到不舒服,您仍然可以通过提交有关错误的报告或请求新功能来支持它,或者简单地帮助讨论现有问题,以向我们提供您的意见并塑造此包的进展。