Index: system/handler.php =================================================================== RCS file: /usr/local/cvsroot/pgweb/portal/system/handler.php,v retrieving revision 1.4 diff -u -r1.4 handler.php --- system/handler.php 21 Apr 2004 14:50:44 -0000 1.4 +++ system/handler.php 15 Nov 2004 07:48:53 -0000 @@ -15,7 +15,7 @@ if (!empty($_GET['lang'])) { $language = addslashes($_GET['lang']); } else { - $language = $_SETTINGS['defaultlanguage']; + $language = language_from_accept_language($_SETTINGS['defaultlanguage']); } if (!empty($_GET['page'])) { @@ -26,11 +26,17 @@ $_LANGUAGE = language_set($language); +$language_direction = language_direction($language); + // Prepare the template $tpl =& new HTML_Template_Sigma('../template', '/tmp'); $tpl->setCallbackFunction('lang', 'gettext', true); $tpl->loadTemplateFile('common.html', true, true); $tpl->setGlobalVariable('current_page', $page); +$tpl->setGlobalVariable('current_lang', $language); +$tpl->setGlobalVariable('current_lang_dir', $language_direction); + +header("Content-Language: $language"); if (preg_match('!^docs/([^/]*)/([a-z]*)/([^/]*)$!', $page, $docsAry)) { require './docs.php'; @@ -50,4 +56,4 @@ )); $tpl->show(); -?> \ No newline at end of file +?> Index: system/global/functions.language.php =================================================================== RCS file: /usr/local/cvsroot/pgweb/portal/system/global/functions.language.php,v retrieving revision 1.3 diff -u -r1.3 functions.language.php --- system/global/functions.language.php 16 Mar 2004 22:53:48 -0000 1.3 +++ system/global/functions.language.php 15 Nov 2004 07:48:53 -0000 @@ -37,15 +37,16 @@ * Checks whether the given language exists * * @param string Language name, from the name of the requested page + * @param bool Only check the root language? * @return bool */ -function language_valid($lang) +function language_valid($lang, $at_root = true) { global $_LANGUAGES, $_LANGUAGE_ALIASES; if (isset($_LANGUAGES[$lang])) { return true; - } elseif (strpos($lang, '-')) { + } elseif ($at_root == true && strpos($lang, '-')) { $parts = explode('-', $lang); if (isset($_LANGUAGES[$parts[0]])) { return true; @@ -68,7 +69,7 @@ return $_LANGUAGE_ALIASES[$lang]; } if (false !== strpos($lang, '-')) { - $parts = explode('-', $parts); + $parts = explode('-', $lang); if (isset($_LANGUAGES[$lang])) { return $parts[0] . '_' . strtoupper($parts[1]); } @@ -82,4 +83,51 @@ } return $lang; } + +/** + * Return the root of the language, e.g. the root of en-us is en + * + * @param string Language name + * @return string Root of language name + */ +function language_root($lang) { + $parts = explode('-', $lang); + return $parts[0]; +} + + +/** + * Return the first valid language from HTTP_ACCEPT_LANGUAGE, else current_lang + * + * @param string Language name, the current language + * @return string Language name, either a matched ACCEPT_LANGUAGE or current_lang + */ +function language_from_accept_language($current_lang) { + if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { + $accepts_langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']); + foreach ($accepts_langs as $accepts_lang) { + $accepts_lang = explode(';', $accepts_lang); + $accepts_lang = $accepts_lang[0]; + if (language_valid($accepts_lang, false)) { + return $accepts_lang; + } + } + } + return $current_lang; +} + +/** + * Return the direction (ltr, rtl) of the language, else default to ltr + * + * @param string Language name + * @return string Language direction + */ +function language_direction($lang) { + $direction = $GLOBALS['_LANGUAGE_DIRECTION'][language_root($lang)]; + if (empty($direction)) { + return 'ltr'; + } + return $direction; +} + ?> Index: system/global/languages.php =================================================================== RCS file: /usr/local/cvsroot/pgweb/portal/system/global/languages.php,v retrieving revision 1.3 diff -u -r1.3 languages.php --- system/global/languages.php 8 Mar 2004 10:24:23 -0000 1.3 +++ system/global/languages.php 15 Nov 2004 07:48:53 -0000 @@ -8,7 +8,7 @@ $GLOBALS['_LANGUAGES'] = array( 'en' => 'English', // "de" => "Deutsch", - 'ru' => 'Русский' + 'ru' => 'Русский' ); // Aliases for languages with different browser and gettext codes @@ -17,4 +17,10 @@ 'en' => 'en_US', 'ru' => 'ru_RU' ); -?> \ No newline at end of file + +// Language directions +$GLOBALS['_LANGUAGE_DIRECTION'] = array( + 'en' => 'ltr', + 'ru' => 'ltr' +}; +?> Index: template/common.html =================================================================== RCS file: /usr/local/cvsroot/pgweb/portal/template/common.html,v retrieving revision 1.7 diff -u -r1.7 common.html --- template/common.html 13 Nov 2004 05:59:03 -0000 1.7 +++ template/common.html 15 Nov 2004 07:48:53 -0000 @@ -1,7 +1,8 @@ - + PostgreSQL<!-- BEGIN page_title_more -->: {page_title}<!-- END page_title_more --> +