[ Index ]

PHP Cross Reference of phpwcms V1.4.3 _r380 (23.11.09)

title

Body

[close]

/include/inc_lib/ -> helper.inc.php (source)

   1  <?php
   2  /*************************************************************************************
   3     Copyright notice
   4     
   5     (c) 2002-2009 Oliver Georgi (oliver@phpwcms.de) // All rights reserved.
   6   
   7     This script is part of PHPWCMS. The PHPWCMS web content management system is
   8     free software; you can redistribute it and/or modify it under the terms of
   9     the GNU General Public License as published by the Free Software Foundation;
  10     either version 2 of the License, or (at your option) any later version.
  11    
  12     The GNU General Public License can be found at http://www.gnu.org/copyleft/gpl.html
  13     A copy is found in the textfile GPL.txt and important notices to the license 
  14     from the author is found in LICENSE.txt distributed with these scripts.
  15    
  16     This script is distributed in the hope that it will be useful, but WITHOUT ANY 
  17     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  18     PARTICULAR PURPOSE.  See the GNU General Public License for more details.
  19   
  20     This copyright notice MUST APPEAR in all copies of the script!
  21  *************************************************************************************/
  22  
  23  /**
  24   * Render @@Text@@ based on browser language and store in related language file
  25   * which allows easy translation at later time and when needed. The Text between
  26   * @@Default@@ will be taken as default text if no translation exists.
  27   *
  28   * Based on work and ideas of
  29   *   Dr.-Ing. Tobias Schittkowski (http://www.schittkowski.de/index.php?q=node/20)
  30   *   André Rabold (http://smarty.incutio.com/?page=SmartyMultilanguageSupport)
  31   **/
  32  
  33  // language which is selected by user (defined in the browser settings)
  34  function i18n_get_language($complex=false) {
  35      global $phpwcms;
  36      if(!empty($phpwcms['i18_lang'])) {
  37          return $phpwcms['i18_lang'];
  38      } elseif(empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
  39          $phpwcms['i18_lang'] = $phpwcms['default_lang'];
  40          return $phpwcms['i18_lang'];
  41      }
  42      $complex = isset($phpwcms['i18n_complex']) ? $phpwcms['i18n_complex'] : $complex;
  43      if($complex) {
  44          $lang = explode(';', trim($_SERVER['HTTP_ACCEPT_LANGUAGE']), 2);
  45          $lang = explode(',', $lang[0], 2);
  46          $phpwcms['i18_lang'] = preg_replace('/[^a-z0-9\-_\.]/', '', strtolower(trim($lang[0])));
  47      } else {
  48          $phpwcms['i18_lang'] = strtolower(substr( trim($_SERVER['HTTP_ACCEPT_LANGUAGE']) , 0, 2));
  49      }
  50      if(empty($phpwcms['i18_lang'])) {
  51          $phpwcms['i18_lang'] = $phpwcms['default_lang'];
  52      }
  53      return $phpwcms['i18_lang'];
  54  }
  55  // get the template file name
  56  function i18n_get_filename() {
  57      return PHPWCMS_TEMPLATE . 'template_lang/' . i18n_get_language(true) . '.php';
  58  }
  59  function i18n_get_file_open_text() {
  60      $text  = '<?php ' . PHP_EOL;
  61      $text .= '// phpwcms template language file "' . i18n_get_language(true) . '" (' . now('Y-m-d H:i:s') . ')' . PHP_EOL;
  62      $text .= '// ATTENTION! Never add the closing PHP tag "? >" at the end of this file!' . PHP_EOL . PHP_EOL;
  63      return $text;
  64  }
  65  // substitutes a single token
  66  function i18n_substitute_text_token($token) {
  67      global $i18n_tokens;    
  68      $a = trim($token[1]);
  69      if(isset($i18n_tokens[$a])) {
  70          return $i18n_tokens[$a];
  71      } else {        
  72          $f = i18n_get_filename();
  73          if(is_readable($f)) {
  74              include($f);
  75          } elseif($handle = fopen($f, 'ab')) {
  76              fwrite($handle, i18n_get_file_open_text() );
  77              fclose($handle);
  78          } else {
  79              return $a;
  80          }
  81          if(isset($i18n_tokens[$a])) {
  82              return $i18n_tokens[$a];
  83          }
  84          $i18n_tokens[$a] = $a;
  85          $as = str_replace("'", "\\'", $a);
  86          $s = '$i18n_tokens' . "['" . $as . "']" . " = '" . $as . "'; // NEW " . now('Y-m-d H:i:s') . PHP_EOL;
  87          if($handle = fopen($f, 'ab')) {
  88              fwrite($handle, $s);
  89              fclose($handle);
  90          }
  91      }
  92      return $a;
  93  }
  94  // all contents starting and ending with @@ are replaced
  95  function i18n_substitute_text($tpl_output) {
  96      global $i18n_tokens;        
  97      $f = i18n_get_filename();
  98      if(!isset($i18n_tokens)) {
  99          if(is_readable($f)) {
 100              include($f);
 101          } elseif($handle = fopen($f, 'ab')) {
 102              fwrite($handle, i18n_get_file_open_text() );
 103              fclose($handle);
 104          }
 105      }
 106      return preg_replace_callback('/@@(.+?)@@/', 'i18n_substitute_text_token', $tpl_output);    
 107  }
 108  ///////////////////////////////////////////////////////////////////////////////////////////////
 109  
 110  
 111  ?>


Generated: Wed Dec 30 05:55:15 2009 Cross-referenced by PHPXref 0.7