yimaLocali
this module is part of Yima Application Framework, (*1)
[zf2 module] Detection of locale through different strategies., (*2)
Configuration
This is most default configuration of module with description of each key as comment., (*3)
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,
),
How To Get Detected Locale?
From Service Manager
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');
}
// ...
}
From Locale Intl Extension
if we have Intl extension installed on server after detection of locale from strategies it will set to "Locale" class as defaultLocale, (*4)
Locale::getDefault();
From View Helper
echo $this->locale()->getLocale(); // exp. "en_US"
echo $this->locale(); // exp. "en_US"
echo $this->locale()->getLanguage(); // en
echo $this->locale()->getRegion(); // US
From Controller Plugin
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
}
}
Locale Detected From Aggregate Default Strategy, What About Else ?
If you want using your own complete different strategy for Locale Detection do the same as below., (*5)
Make your detection strategy class
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();
}
}
Make it exclusive default detection service
you must register a service with name "yimaLocali.Detector.Strategy" it will replaced with default AggregateDetector, (*6)
Register The Strategy To Default Aggregate
see configuration settings, (*7)
Plugins for locale(Controller/View)Helper
we can use pluginManager of locale helper and define some plugin or use default plugins., (*8)
Make new plugin and use it!
you can take benefit of configuration service for plugins and define your plugin with yima_locali_plugins
merged config key., (*9)
return array(
'yima_locali_plugins' => array(
'factories' => array(
'test' => function ($sm) {
return 'This is locale plugin with test name';
}
),
// 'invokables' => array(),
// etc..
),
);
and you can catch this from within View or Controller:, (*10)
$this->locale()->test();
// or
$this->locale()->plugin('test', $parameters);
Installation
Composer installation:, (*11)
require rayamedia/yima-locali
in your composer.json
, (*12)
Or clone to modules folder, (*13)
Enable module in application config, (*14)
Support
To report bugs or request features, please visit the Issue Tracker., (*15)
Please feel free to contribute with new issues, requests and code fixes or new features., (*16)