Locale.php

Go to the documentation of this file.
00001 <?php
00003 // {{{ license
00004 
00005 // +----------------------------------------------------------------------+
00006 // | FastFrame Application Framework                                      |
00007 // +----------------------------------------------------------------------+
00008 // | Copyright (c) 2002-2006 The Codejanitor Group                        |
00009 // +----------------------------------------------------------------------+
00010 // | This source file is subject to the GNU Lesser Public License (LGPL), |
00011 // | that is bundled with this package in the file LICENSE, and is        |
00012 // | available at through the world-wide-web at                           |
00013 // | http://www.fsf.org/copyleft/lesser.html                              |
00014 // | If you did not receive a copy of the LGPL and are unable to          |
00015 // | obtain it through the world-wide-web, you can get it by writing the  |
00016 // | Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
00017 // | MA 02111-1307, USA.                                                  |
00018 // +----------------------------------------------------------------------+
00019 // | Authors: The Horde Team <http://www.horde.org>                       |
00020 // |          Jason Rust <jrust@codejanitor.com>                          |
00021 // +----------------------------------------------------------------------+
00022 
00023 // }}}
00024 // {{{ includes
00025 
00026 require_once dirname(__FILE__) . '/Registry.php';
00027 require_once dirname(__FILE__) . '/Auth.php';
00028 
00029 // }}}
00030 // {{{ class FF_Locale
00031 
00041 // }}}
00042 class FF_Locale {
00043     // {{{ setLang()
00044 
00053     function setLang($in_lang = null)
00054     {
00055         if (is_null($in_lang) || !FF_Locale::isValid($in_lang)) {
00056             $in_lang = FF_Locale::selectLang();
00057         }
00058 
00059         // First try language with the current charset
00060         $lang_charset = $in_lang . '.' . FF_Locale::getCharset();
00061         if ($lang_charset != setlocale(LC_ALL, $lang_charset)) {
00062             // Next try language with its default charset
00063             $a_nls = FF_Locale::getNLSData();
00064             $o_registry =& FF_Registry::singleton();
00065             $lang_charset = $in_lang . '.' . 
00066                 (!empty($a_nls['charsets'][$in_lang]) ? $a_nls['charsets'][$in_lang] : 
00067                  $o_registry->getConfigParam('language/charset'));
00068 
00069             if ($lang_charset != setlocale(LC_ALL, $lang_charset)) {
00070                 // Finally, try language solely.
00071                 $lang_charset = $in_lang;
00072                 setlocale(LC_ALL, $lang_charset);
00073             }
00074         }
00075 
00076         @putenv('LANG=' . $lang_charset);
00077         @putenv('LANGUAGE=' . $lang_charset);
00078     }
00079 
00080     // }}}
00081     // {{{ setTextdomain()
00082 
00094     function setTextdomain($in_app, $in_directory, $in_charset)
00095     {
00096         bindtextdomain($in_app, $in_directory);
00097         textdomain($in_app);
00098 
00099         // The existence of this function depends on the platform.
00100         if (function_exists('bind_textdomain_codeset')) {
00101            bind_textdomain_codeset($in_app, $in_charset);
00102         }
00103 
00104         if (!headers_sent()) {
00105             header('Content-Type: text/html; charset=' . $in_charset);
00106         }
00107     }
00108 
00109     // }}}
00110     // {{{ isValid()
00111 
00120     function isValid($in_language)
00121     {
00122         $a_nls = FF_Locale::getNLSData();
00123         return !empty($a_nls['languages'][$in_language]);
00124     }
00125 
00126     // }}}
00127     // {{{ getCharset()
00128 
00136     function getCharset()
00137     {
00138         $lang_charset = setlocale(LC_ALL, 0);
00139         if (!strstr($lang_charset, ';')) {
00140             $lang_charset = explode('.', $lang_charset);
00141             if ((count($lang_charset) == 2) && !empty($lang_charset[1])) {
00142                 return $lang_charset[1];
00143             }
00144         }
00145 
00146         $a_nls = FF_Locale::getNLSData();
00147         if (!empty($a_nls['charsets'][FF_Auth::getCredential('language')])) {
00148             return $a_nls['charsets'][FF_Auth::getCredential('language')];
00149         }
00150         else {
00151             $o_registry =& FF_Registry::singleton();
00152             return $o_registry->getConfigParam('language/charset');
00153         }
00154     }
00155 
00156     // }}}
00157     // {{{ getNLSData()
00158 
00165     function getNLSData()
00166     {
00167         static $a_nls;
00168 
00169         if (!isset($a_nls)) {
00170             $o_registry =& FF_Registry::singleton();
00171             $pth_lang = $o_registry->getRootFile('nls.php', 'config');
00172             if (@file_exists($pth_lang)) {
00173                 include_once $pth_lang; 
00174             } 
00175             else {
00176                 include_once $pth_lang . '.dist'; 
00177             }
00178         }
00179 
00180         return $a_nls;
00181     }
00182 
00183     // }}}
00184     // {{{ selectLang()
00185 
00192     function selectLang()
00193     {
00194         $o_registry =& FF_Registry::singleton();
00195         $s_lang = $o_registry->getConfigParam('language/default');
00196         // See if we have the language stored in session
00197         if (!is_null(FF_Auth::getCredential('language'))) {
00198             $s_lang = FF_Auth::getCredential('language'); 
00199         }
00200         elseif (empty($s_lang) && !empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
00201             // The browser supplies a list, so return the first valid one.
00202             $a_nlss = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
00203             foreach ($a_nlss as $tmp_lang) {
00204                 $tmp_lang = FF_Locale::_map(trim($tmp_lang));
00205                 if (FF_Locale::isValid($tmp_lang)) {
00206                     $s_lang = $tmp_lang;
00207                     break;
00208                 } 
00209                 elseif (FF_Locale::isValid(FF_Locale::_map(substr($tmp_lang, 0, 2)))) {
00210                     $s_lang = FF_Locale::_map(substr($tmp_lang, 0, 2));
00211                     break;
00212                 }
00213             }
00214         }
00215 
00216         return basename($s_lang);
00217     }
00218 
00219     // }}}
00220     // {{{ _map
00221 
00233     function _map($in_language)
00234     {
00235         $a_nls = FF_Locale::getNLSData();
00236         // First check if the untranslated language can be found
00237         if (isset($a_nls['aliases'][$in_language])) {
00238             return $a_nls['aliases'][$in_language];
00239         }
00240 
00241         // Translate the $language to get broader matches.
00242         // (eg. de-DE should match de_DE)
00243         $in_language = str_replace('-', '_', $in_language);
00244         $lang_parts = explode('_', $in_language);
00245         $in_language = strtolower($lang_parts[0]);
00246         if (isset($lang_parts[1])) {
00247             $in_language .= '_' . strtoupper($lang_parts[1]);
00248         }
00249 
00250         // First check if the untranslated language can be found
00251         if (isset($a_nls['aliases'][$in_language])) {
00252             return $a_nls['aliases'][$in_language];
00253         }
00254 
00255         // If we get that far down, the language cannot be found.
00256         return $in_language;
00257     }
00258 
00259     // }}}
00260 }
00261 ?>

Generated on Fri Jun 23 11:38:16 2006 for FastFrame by  doxygen 1.4.4