menencia / locale-detector
Locale detector
This package's canonical repository appears to be gone and the package has been frozen as a result. Email us for help if needed.
Installs: 153
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 2
Open Issues: 0
pkg:composer/menencia/locale-detector
Requires (Dev)
- phpunit/phpunit: 3.8.*@dev
This package is not auto-updated.
Last update: 2021-08-01 23:56:49 UTC
README
Using this module, you can then get the locale detected for the user, based on what you want first.
To call this module :
$localeDetector = new Menencia\LocaleDetector\LocaleDetector();
This is the actual strategy order, but you can reorder this array or remove some items :
$localeDetector->setOrder(['TLD', 'Cookie', 'Header', 'NSession']); // optional
- TLD (Top-level domain): determining locale from
$_SERVER['SERVER_NAME']
- Cookie: determining locale from
$_COOKIE[$field]
- Header: determining locale from
$_SERVER['HTTP_ACCEPT_LANGUAGE']
- NSession (Session): determining locale from
$_SESSION[$field]
- IP (IP Address): determining locale from
$_SERVER['REMOTE_ADDR']
- By default, determining locale from
Locale::getDefault()
By default, $field = 'lang';
. This is how you can change that :
Cookie::$fieldName = 'newFieldName'; NSession::$fieldName = 'newFieldName';
Then, you just have to call the detect method and retrieve the locale :
$locale = $localeDetector->detect();
Advanced strategies
You have the possibility to custom your strategy like this :
$localeDetector->addCallback('MyCallback', function($a){ return collator_create($a); }, ['fr-FR']); $localeDetector->setOrder(['MyCallback']);
Maybe you want to extends the Strategy interface :
<?php class MyStrategy implements IStrategy { public function getName() { return 'MyStrategy'; } public function detect() { return collator_create('fr-FR'); } }
Then, you add to register :
$localeDetector->registerStrategy(new MyStrategy); $localeDetector->setOrder(['MyStrategy']);