Mercurial > SimpleWebPresenter
changeset 56:0f1e08cdfff2
Move accept-language to Language to reflect new class name.
| author | Tom Fredrik "BFG" Klaussen <bfg@blenning.no> |
|---|---|
| date | Thu, 11 Oct 2012 01:07:17 +0200 |
| parents | 88482c6636c4 |
| children | fd3dd497eba6 ba5afd9ff24e |
| files | Language.inc accept-language.inc flag.php index.php |
| diffstat | 4 files changed, 58 insertions(+), 58 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Language.inc Thu Oct 11 01:07:17 2012 +0200 @@ -0,0 +1,56 @@ +<?php +/** + * @file + * Functionality for determining language use + */ +class Language { + /** + * Extracts the accepted languages from the GET query, sorted by + * preference(q-value). + * + * @return associative array of language codes with q value + */ + function accepted() { + $langs = array(); + + if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { + // break up string into pieces (languages and q factors) + preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse); + + if (count($lang_parse[1])) { + // create a list like "en" => 0.8 + $langs = array_combine($lang_parse[1], $lang_parse[4]); + + // set default to 1 for any without q factor + foreach ($langs as $lang => $val) { + if ($val === '') $langs[$lang] = 1; + } + + // sort list based on value + arsort($langs, SORT_NUMERIC); + } + } + return $langs; + } + + /** + * From the list of desired languages, pick the best which we can serve. + * + * @param $default what to choose if no match could be found. + */ + static function prefer($default) + { + $language = $default; + $langs = self::accepted(); + if ($langs) { + foreach ($langs as $l => $val) { + if (file_exists($l)) { + $language = $l; + break; + } + } + } + return $language; + } +} +?> \ No newline at end of file
--- a/accept-language.inc Thu Oct 11 01:04:23 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,56 +0,0 @@ -<?php -/** - * @file - * Functionality for determining language use - */ -class Language { - /** - * Extracts the accepted languages from the GET query, sorted by - * preference(q-value). - * - * @return associative array of language codes with q value - */ - function accepted() { - $langs = array(); - - if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { - // break up string into pieces (languages and q factors) - preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse); - - if (count($lang_parse[1])) { - // create a list like "en" => 0.8 - $langs = array_combine($lang_parse[1], $lang_parse[4]); - - // set default to 1 for any without q factor - foreach ($langs as $lang => $val) { - if ($val === '') $langs[$lang] = 1; - } - - // sort list based on value - arsort($langs, SORT_NUMERIC); - } - } - return $langs; - } - - /** - * From the list of desired languages, pick the best which we can serve. - * - * @param $default what to choose if no match could be found. - */ - static function prefer($default) - { - $language = $default; - $langs = self::accepted(); - if ($langs) { - foreach ($langs as $l => $val) { - if (file_exists($l)) { - $language = $l; - break; - } - } - } - return $language; - } -} -?> \ No newline at end of file
--- a/flag.php Thu Oct 11 01:04:23 2012 +0200 +++ b/flag.php Thu Oct 11 01:07:17 2012 +0200 @@ -8,7 +8,7 @@ include_once 'CacheTimeCheck.inc'; $cache = CacheTimeCheck::instance(__FILE__); -$cache->includeOnce('accept-language.inc'); +$cache->includeOnce('Language.inc'); $cache->includeOnce('common-functions.inc'); $active = $_GET['active'];
--- a/index.php Thu Oct 11 01:04:23 2012 +0200 +++ b/index.php Thu Oct 11 01:07:17 2012 +0200 @@ -12,8 +12,8 @@ $baseDir = dirname(__FILE__); $cache = CacheTimeCheck::instance(__FILE__); +$cache->includeOnce('Language.inc', $baseDir); $cache->includeOnce('Options.inc', $baseDir); -$cache->includeOnce('accept-language.inc', $baseDir); $cache->includeOnce('common-functions.inc', $baseDir); $cache->includeOnce('filters.inc', $baseDir); $cache->includeOnce('inputParser.inc', $baseDir);
