rayamedia/yima-locali

此包的最新版本(dev-master)没有可用的许可证信息。

通过不同的策略检测地区,包括多语言数据库(可翻译字段)的功能,以及CLDR数据,用于数据的完整系统本地化,以及许多预定义的有用地区助手。

安装: 12

依赖项: 0

建议者: 0

安全: 0

星星: 1

关注者: 2

分支: 0

开放问题: 0

类型:yima-core-module

dev-master 2015-01-04 12:31 UTC

This package is auto-updated.

Last update: 2024-09-24 04:05:29 UTC


README

此模块是Yima应用框架的一部分

[zf2模块] 通过不同的策略检测地区。

配置

这是模块的默认配置,每个键的描述作为注释。

return array (
	'yimaLocali' => array(
        # Locale Detector Strategies, Implemented DetectorInterface
		'detector' => array(
            #  Used By Default AggregateStrategyFactory (yimaLocali.Detector.Strategy)
            'aggregate' => array(
                'strategies' => array(
                    //'Registered\Service\Or\ClassName' => -10,
                    # or
                    /*
                    array(
                        'object'   => new StrategyObject(),
                        'priority' => -10
                    ),
                    */

                    // default ordered strategies
                    'yimaLocali\Detector\UriPathStrategy' => 90,
                    'yimaLocali\Detector\CookieStrategy'  => 80,
                    array(
                        'object'   => new \yimaLocali\Detector\RestrictLocaleStrategy(),
                        'priority' => -1000
                    ),
                ),
            ),
        ),

        # content of this key will pass to LocaleSupport(class) on Bootstrap
        'available_locales' => array(
            'default'    => 'en_US',
            'locales'  => array(
                'en_US',
                'fa_IR',
            ),
            'aliases'    => array(
                'en'    => 'en_US',
                'fa'    => 'fa_IR',
                'farsi' => 'fa',
            ),
        ),

		# if you need detected locale before continue of running application
		'throw_exceptions' => false,
	),

如何获取检测到的地区?

从服务管理器

class LocaleNeededService implement
    ServiceLocatorAwareInterface
{
    public function getLocale()
    {
        // get serviceManager
        $sm = $this->getServiceLocator();

        // we can get current locale from service manager
        return $sm->get('locale.detected');
    }

    // ...
}

从地区Intl扩展

如果我们在服务器上安装了Intl扩展,并在检测到地区后,它将设置为“地区”类的默认Locale

Locale::getDefault();

从视图助手

echo $this->locale()->getLocale(); // exp. "en_US"

echo $this->locale(); // exp. "en_US"

echo $this->locale()->getLanguage(); // en
echo $this->locale()->getRegion();   // US

从控制器插件

use Zend\Mvc\Controller\AbstractActionController;

class IndexController extends AbstractActionController
{
    public function indexAction()
    {
        echo $this->locale()->getLocale(); // exp. "en_US"

        echo $this->locale(); // exp. "en_US"

        echo $this->locale()->getLanguage(); // en
        echo $this->locale()->getRegion();   // US
    }
}

从聚合默认策略检测到的地区,其他怎么办?

如果您想使用自己完整的不同策略进行地区检测,请按照以下步骤操作。

创建您的检测策略类

use yimaLocali\Detector\DetectorInterface;

class MyLocaleStrategy implements
    DetectorInterface
{
    /**
     * Get locale from configs default setting
     *
     * @return string
     */
    public function getLocale()
	{
	    // get Locale from default locale module config
	    return LocaleSupport::getDefaultLocale();
	}
}

使其成为独家默认检测服务

您必须注册一个名为“yimaLocali.Detector.Strategy”的服务,它将替换默认的AggregateDetector

将策略注册到默认聚合

请参阅配置设置

地区(控制器/视图)助手插件

我们可以使用地区助手的pluginManager定义一些插件或使用默认插件。

创建新的插件并使用它!

您可以通过配置服务从插件中受益,并使用yima_locali_plugins合并的配置密钥定义您的插件。

return array(
'yima_locali_plugins' => array(
        'factories'  => array(
            'test' => function ($sm) {
                return 'This is locale plugin with test name';
            }
        ),
        // 'invokables' => array(),
        // etc..
    ),
);

您还可以从视图或控制器中捕获此内容

$this->locale()->test();
// or
$this->locale()->plugin('test', $parameters);

安装

Composer安装

在您的composer.json中添加require rayamedia/yima-locali

或克隆到模块文件夹

在应用程序配置中启用模块

支持

要报告错误或请求功能,请访问问题跟踪器

请随时通过新问题、请求和代码修复或新功能来贡献。