Location: PHPKode > projects > Aukyla Platform > doc/html/a00219.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <title>Aukyla PHP Framework</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link rel="StyleSheet" href="auton.css" type="text/css" media="screen">
  </head>

  <body>
    <div id="mainmenu">
      <a href="http://www.auton.nl/" class="home">Home</a>
    </div>
    <div id="submenu">
      <a class="logo" href="http://www.auton.nl/"></a>

      <div class="head">Ads</div>
      <a href="http://sourceforge.net/"><img src="http://sourceforge.net/sflogo.php?group_id=116358&amp;type=1" alt="SourceForge.net Logo" style="padding: 2px 4px; width: 88px; height: 31px; border: 0px"></a>
      <a href="http://getfirefox.com/" title="Get Firefox - The Browser, Reloaded."><img src="http://www.mozilla.org/products/firefox/buttons/getfirefox_small.png" alt="Get Firefox" style="width: 110px; height: 32px; border: 0px"></a>
    </div>
    <div id="main">
<!-- Generated by Doxygen 1.3.9.1 -->
<div class="qindex"><a class="qindex" href="index.html">Main&nbsp;Page</a> | <a class="qindex" href="hierarchy.html">Class&nbsp;Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical&nbsp;List</a> | <a class="qindex" href="annotated.html">Class&nbsp;List</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="functions.html">Class&nbsp;Members</a> | <a class="qindex" href="pages.html">Related&nbsp;Pages</a></div>
<div class="nav">
<a class="el" href="dir_000000.html">base</a></div>
<h1>Locale.php</h1><div class="fragment"><pre class="fragment">00001 &lt;?php
00002 <span class="comment">/*</span>
00003 <span class="comment">     Locale.php, this module provides i18n support</span>
00004 <span class="comment">     Copyright (C) 2003 Arend van Beelen, Auton Rijnsburg</span>
00005 <span class="comment"></span>
00006 <span class="comment">     This program is free software; you can redistribute it and/or modify it</span>
00007 <span class="comment">     under the terms of the GNU General Public License as published by the Free</span>
00008 <span class="comment">     Software Foundation; either version 2 of the License, or (at your option)</span>
00009 <span class="comment">     any later version.</span>
00010 <span class="comment"></span>
00011 <span class="comment">     This program is distributed in the hope that it will be useful, but WITHOUT</span>
00012 <span class="comment">     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or</span>
00013 <span class="comment">     FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for</span>
00014 <span class="comment">     more details.</span>
00015 <span class="comment"></span>
00016 <span class="comment">     You should have received a copy of the GNU General Public License along</span>
00017 <span class="comment">     with this program; if not, write to the Free Software Foundation, Inc.,</span>
00018 <span class="comment">     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</span>
00019 <span class="comment"></span>
00020 <span class="comment">     For any questions, comments or whatever, you may mail me at: hide@address.com</span>
00021 <span class="comment">*/</span>
00022 
00023 require_once('Constants.php');
00024 
<a name="l00037"></a><a class="code" href="a00119.html">00037</a> <span class="keyword">class </span><a class="code" href="a00119.html">Locale</a>
00038 {
<a name="l00045"></a><a class="code" href="a00119.html#a0">00045</a>         <span class="keyword">public</span> function __construct()
00046         {
00047                 $this-&gt;TRANSLATIONS = array();
00048                 $this-&gt;currentLanguage = 'en';
00049         }
00050 
<a name="l00066"></a><a class="code" href="a00119.html#e0">00066</a>         <span class="keyword">public</span> <span class="keyword">static</span> function init($modulename, $language = '')
00067         {
00068                 global $Locale;
00069 
00070                 <span class="keywordflow">if</span>($language == '')
00071                 {
00072                         $language = $Locale-&gt;currentLanguage;
00073                 }
00074                 <span class="keywordflow">else</span>
00075                 {
00076                         <span class="keywordflow">if</span>($Locale-&gt;currentLanguage != $language)
00077                         {
00078                                 $Locale-&gt;TRANSLATIONS = array();
00079                         }
00080                 }
00081 
00082                 $Locale-&gt;currentLanguage = $language;
00083 
00084                 <span class="keywordflow">if</span>(file_exists(AUKYLA_DIR.<span class="stringliteral">"/locale/$modulename/$language.po"</span>) &amp;&amp;
00085                    $lines = file(AUKYLA_DIR.<span class="stringliteral">"/locale/$modulename/$language.po"</span>))
00086                 {
00087                         $multilinemsgid = <span class="keyword">false</span>;
00088                         $multilinemsgstr = <span class="keyword">false</span>;
00089 
00090                         <span class="comment">// import the PO file</span>
00091                         $msgid = '';
00092                         $msgstr = '';
00093                         foreach($lines as $line)
00094                         {
00095                                 $line = str_replace(<span class="charliteral">'\n'</span>, <span class="stringliteral">"\n"</span>, $line);
00096 
00097                                 <span class="keywordflow">if</span>($multilinemsgid == <span class="keyword">true</span>)
00098                                 {
00099                                         <span class="keywordflow">if</span>($line[0] == <span class="charliteral">'"'</span>)
00100                                         {
00101                                                 $msgid .= str_replace(strrchr($line, <span class="charliteral">'"'</span>), '', substr($line, 1));
00102                                         }
00103                                         <span class="keywordflow">else</span>
00104                                         {
00105                                                 $multilinemsgid = <span class="keyword">false</span>;
00106                                         }
00107                                 }
00108                                 <span class="keywordflow">else</span> <span class="keywordflow">if</span>($multilinemsgstr == <span class="keyword">true</span>)
00109                                 {
00110                                         <span class="keywordflow">if</span>($line[0] == <span class="charliteral">'"'</span>)
00111                                         {
00112                                                 $msgstr .= str_replace(strrchr($line, <span class="charliteral">'"'</span>), '', substr($line, 1));
00113                                         }
00114                                         <span class="keywordflow">else</span>
00115                                         {
00116                                                 <span class="keywordflow">if</span>($msgid != '' &amp;&amp; $msgstr != '')
00117                                                 {
00118                                                         $msgid = preg_replace(<span class="stringliteral">"/\\\\(.)/"</span>, <span class="stringliteral">"\$1"</span>, $msgid);
00119                                                         $msgstr = preg_replace(<span class="stringliteral">"/\\\\(.)/"</span>, <span class="stringliteral">"\$1"</span>, $msgstr);
00120                                                         $<a class="code" href="a00119.html">Locale</a>-&gt;TRANSLATIONS[$msgid] = $msgstr;
00121                                                 }
00122                                                 $msgid = '';
00123                                                 $msgstr = '';
00124                                                 $multilinemsgstr = <span class="keyword">false</span>;
00125                                         }
00126                                 }
00127                                 <span class="keywordflow">if</span>(substr($line, 0, 6) == 'msgid ')
00128                                 {
00129                                         $msgid = str_replace(strrchr($line, <span class="charliteral">'"'</span>), '', substr(strchr($line, <span class="charliteral">'"'</span>), 1));
00130                                         <span class="keywordflow">if</span>($msgid == '')
00131                                         {
00132                                                 $multilinemsgid = <span class="keyword">true</span>;
00133                                         }
00134                                 }
00135                                 <span class="keywordflow">else</span> <span class="keywordflow">if</span>(substr($line, 0, 7) == 'msgstr ')
00136                                 {
00137                                         $msgstr = str_replace(strrchr($line, <span class="charliteral">'"'</span>), '', substr(strchr($line, <span class="charliteral">'"'</span>), 1));
00138                                         <span class="keywordflow">if</span>($msgstr == '')
00139                                         {
00140                                                 $multilinemsgstr = <span class="keyword">true</span>;
00141                                         }
00142                                         <span class="keywordflow">else</span>
00143                                         {
00144                                                 <span class="keywordflow">if</span>($msgid != '' &amp;&amp; $msgstr != '')
00145                                                 {
00146                                                         $msgid = preg_replace(<span class="stringliteral">"/\\\\(.)/"</span>, <span class="stringliteral">"\$1"</span>, $msgid);
00147                                                         $msgstr = preg_replace(<span class="stringliteral">"/\\\\(.)/"</span>, <span class="stringliteral">"\$1"</span>, $msgstr);
00148                                                         $Locale-&gt;TRANSLATIONS[$msgid] = $msgstr;
00149                                                 }
00150                                                 $msgid = '';
00151                                                 $msgstr = '';
00152                                         }
00153                                 }
00154                         }
00155                         <span class="keywordflow">if</span>($msgid != '' &amp;&amp; $msgstr != '')
00156                         {
00157                                 $msgid = preg_replace(<span class="stringliteral">"/\\\\(.)/"</span>, <span class="stringliteral">"\$1"</span>, $msgid);
00158                                 $msgstr = preg_replace(<span class="stringliteral">"/\\\\(.)/"</span>, <span class="stringliteral">"\$1"</span>, $msgstr);
00159                                 $Locale-&gt;TRANSLATIONS[$msgid] = $msgstr;
00160                         }
00161 
00162                         <span class="keywordflow">return</span> <span class="keyword">true</span>;
00163                 }
00164 
00165                 <span class="keywordflow">if</span>($language != 'en')
00166                 {
00167                         error_log(<span class="stringliteral">"Couldn't open PO file."</span>);
00168                 }
00169                 <span class="keywordflow">return</span> <span class="keyword">false</span>;
00170         }
00171 
<a name="l00175"></a><a class="code" href="a00119.html#e1">00175</a>         <span class="keyword">public</span> <span class="keyword">static</span> function language()
00176         {
00177                 global $Locale;
00178 
00179                 <span class="keywordflow">return</span> $Locale-&gt;currentLanguage;
00180         }
00181 
<a name="l00189"></a><a class="code" href="a00119.html#e2">00189</a>         <span class="keyword">public</span> <span class="keyword">static</span> function availableLanguages()
00190         {
00191                 $lines = file(AUKYLA_DIR.'/locale/languages');
00192                 foreach($lines as $line)
00193                 {
00194                         list($code, $lang) = explode(<span class="charliteral">':'</span>, trim($line));
00195                         $languages[$code] = $lang;
00196                 }
00197                 <span class="keywordflow">return</span> $languages;
00198         }
00199 
00203         <span class="keyword">public</span> $TRANSLATIONS;
00204 
00205         <span class="keyword">private</span> $currentLanguage;
00206 }
00207 
00208 <span class="comment">// create one global instance of the class</span>
00209 global $Locale;
00210 $Locale = <span class="keyword">new</span> <a class="code" href="a00119.html">Locale</a>();
00211 
<a name="l00241"></a><a class="code" href="a00119.html#m0">00241</a> function i18n($string)
00242 {
00243         global $Locale;
00244 
00245         <span class="keywordflow">if</span>($string == '')
00246         {
00247                 <span class="keywordflow">return</span> '';
00248         }
00249 
00250         <span class="comment">// get the translation from the translation table</span>
00251         <span class="comment">// if no translation available, use the original text</span>
00252         $trstring = (isset($<a class="code" href="a00119.html">Locale</a>-&gt;TRANSLATIONS[$string]) ? $Locale-&gt;TRANSLATIONS[$string] : $string);
00253 
00254         <span class="comment">// get argument list</span>
00255         $args = func_get_args();
00256         unset($args[0]); <span class="comment">// the first argument was the translated string</span>
00257 
00258         <span class="comment">// convert all %x entities to the corresponding argument x</span>
00259         $num = 1;
00260         foreach($args as $arg)
00261         {
00262                 $trstring = str_replace(<span class="stringliteral">"%$num"</span>, $arg, $trstring);
00263                 $num++;
00264         }
00265 
00266         <span class="keywordflow">return</span> $trstring;
00267 }
00268 
00269 ?&gt;
</pre></div>    </div>
  </body>
</html>
Return current item: Aukyla Platform