Created
July 6, 2012 10:18
-
-
Save sasezaki/3059380 to your computer and use it in GitHub Desktop.
locale "**_**" detector for using in ZF2 module/Application/config/module.config.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // // 'locale' => 'en_US', | |
| // 'locale' => include __DIR__.'/locale.php', | |
| return call_user_func(function () { | |
| // borrowed from ZF1 Zend_Locale::getBrowser | |
| $localer = function () { | |
| $httplanguages = getenv('HTTP_ACCEPT_LANGUAGE'); | |
| if (empty($httplanguages) && array_key_exists('HTTP_ACCEPT_LANGUAGE', $_SERVER)) { | |
| $httplanguages = $_SERVER['HTTP_ACCEPT_LANGUAGE']; | |
| } | |
| $languages = array(); | |
| if (empty($httplanguages)) { | |
| return $languages; | |
| } | |
| $accepted = preg_split('/,\s*/', $httplanguages); | |
| foreach ($accepted as $accept) { | |
| $match = null; | |
| $result = preg_match('/^([a-z]{1,8}(?:[-_][a-z]{1,8})*)(?:;\s*q=(0(?:\.[0-9]{1,3})?|1(?:\.0{1,3})?))?$/i', | |
| $accept, $match); | |
| if ($result < 1) { | |
| continue; | |
| } | |
| if (isset($match[2]) === true) { | |
| $quality = (float) $match[2]; | |
| } else { | |
| $quality = 1.0; | |
| } | |
| $countrys = explode('-', $match[1]); | |
| $region = array_shift($countrys); | |
| $country2 = explode('_', $region); | |
| $region = array_shift($country2); | |
| foreach ($countrys as $country) { | |
| $languages[$region . '_' . strtoupper($country)] = $quality; | |
| } | |
| foreach ($country2 as $country) { | |
| $languages[$region . '_' . strtoupper($country)] = $quality; | |
| } | |
| if ((isset($languages[$region]) === false) || ($languages[$region] < $quality)) { | |
| $languages[$region] = $quality; | |
| } | |
| } | |
| return $languages; | |
| }; | |
| foreach ($localer() as $locale => $match) { | |
| if (strlen($locale) === 5) return $locale; | |
| } | |
| // fallback | |
| return 'en_US'; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment