Location: PHPKode > projects > OpenBizMap > obm2007.1/myecho/myecho.php
<?php
/*
myecho - php internalisation package
Copyright (C) 2005 Nawara

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/


if(!isset($cfg_lang) or $cfg_lang=='')
  $cfg_lang=$_GET['lang'];

if($cfg_lang=='')
{
  $browser_lang=getenv('HTTP_ACCEPT_LANGUAGE');

  if(strpos($browser_lang,','))
    $cfg_lang=substr($browser_lang,0,strpos($browser_lang,','));
  elseif(strlen($browser_lang)>0)
    $cfg_lang=$browser_lang;
  else
    $cfg_lang='en';
}

if($cfg_lang=='de-de')
  $cfg_lang='de';

//echo $myecho_path;
if(!isset($myecho_path) or $myecho_path=='')
  $myecho_path=getcwd();
  
//echo $myecho_path; exit;

define('cfg_source_lang', 'en');
define('cfg_myecho_path', $myecho_path);
define('cfg_lang_file', $cfg_lang);
define('cfg_lang_path', $myecho_path.'/lang/'.cfg_lang_file);
define('cfg_translate',1);


function myecho($text,$var1='',$var2='',$var3='',$var4='',$var5='',$var6='',$var7='',$var8='',$var9='')
{
  $myecho_mode=cfg_myecho_mode;

  if($var1=='TRANSLATE')
    $myecho_mode=2;

  if($var1=='RETURN')
    $return=1;
  else
    $return=0;

  $text=str_replace('"','',$text);

  if(strlen(trim($text))>0)
  {
    global $translation_array;

    $use_mode=$myecho_mode;
    $lang_path=cfg_lang_path;
    $source=$text;

    if(cfg_source_lang==cfg_lang_file)
      $use_mode=1;
    else
      if(!isset($translation_array[$lang_path]))
        $translation_array=myecho_load($lang_path);

    if(isset($translation_array[$lang_path][str_replace('\'','',$source)]))
      $destination=$translation_array[$lang_path][str_replace('\'','',$source)];
    elseif($myecho_mode==1 and !isset($translation_array[$lang_path][$source]))
    {
      myecho_save($source,$source);// WARNING
      $destination=$source;
    }
    else
      $destination=$source;



    if($use_mode==3) //developer mode - translating when no translation
    {
      if(!isset($translation_array[$lang_path][$source]) and $source!=strtoupper($source))
        $use_mode=2;
      else
        $use_mode=1;
    }

    if($use_mode==2) //translating mode
    {
      global $myecho_counter;
      if(!isset($myecho_counter))
      {
        echo '<script type="text/javascript" src="myecho/myecho.js"></script>';
        $myecho_counter=1;
      }
      else
        $myecho_counter++;

      if(!isset($translation_array[$lang_path][$source]) or $translation_array[$lang_path][$source]==$source)
      {
        $show_edit="display:inline";
        $show_text="display:none";
      }
      else
      {
        $show_edit="display:none";
        $show_text="display:inline";
      }
      echo '<span id="myecho'.$myecho_counter.'a" style="border-bottom:#0a0 1px solid;'.$show_text.'" onclick="myecho_edit('.$myecho_counter.');">'.$destination.'</span>
      <input style="border:#f00 1px solid;'.$show_edit.'" title="'.$source.'" id="myecho'.$myecho_counter.'b" onclick="return(false);" onkeyup="myecho_key();" onchange="myecho_save(\''.str_replace('\'','',$source).'\',this.value,\''.cfg_lang_file.'\','.$myecho_counter.');" value="'.$destination.'" />';
      $destination='';

      //$destination='<a href="myecho.php?lang='.rawurlencode(cfg_lang_file).'&amp;source='.rawurlencode($source).'">'.$destination.'</a>';
    }
    elseif($use_mode==1) //production mode
    {
      if(strlen($var1)>0)
      {
        for($ii=1;$ii<10;$ii++)
        {
          $varname='var'.$ii;
          $var=$$varname;
          $destination=str_replace('%'.$ii,$var,$destination);
        }
      }
    }
    else //passthrough
      $destination=$source;

    if($return)
      return($destination);
    else
      echo $destination;
  }

}

//reversing translation - problem
function myecho_reverse($search,$lang_path='') //:$search
{
  if(strlen($lang_path)==0)
    $lang_path=cfg_lang_path;

  $translation_array=myecho_load($lang_path);

  $replace_array=array();
  reset($translation_array);
  while(list($source,$destination)=each($translation_array[$lang_path]))
  {
    if(strpos('_'.strtolower($search),strtolower($destination)))
      $replace_array[$source]=strlen($destination);
  }

  arsort($replace_array);

  reset($replace_array);
  while(list($source,$tmp)=each($replace_array))
    $search=str_replace(strtolower($translation_array[$lang_path][$source]),$source,strtolower($search));

  return $search;
}

function myecho_save($source,$destination,$lang_path='') //:1 success | :0 error
{
  if(strlen($lang_path)==0)
    $lang_path=cfg_lang_path;

  $translation_array=myecho_load($lang_path);

  if(get_magic_quotes_gpc())
    $destination=stripcslashes($destination);

  $translation_array[$lang_path][$source]=$destination;
  
  if($source!=$destination)
  {
    //real translation
    //echo 'TRANSLATED';
    ksort($translation_array[$lang_path]);
    $tmp_content=var_export($translation_array[$lang_path],true);

    myecho_path($lang_path);
    $fp=fopen($lang_path,'wb');
    if(fwrite($fp,'<'.'?php $myecho='.$tmp_content.'; ?'.'>') === FALSE)
    {
    	fclose($fp);
      return 'Cannot save translation. Check available disk space.';
    }
    else
    {
      fclose($fp);
    }
    
    $fp=fopen(cfg_myecho_path.'/lang/.lastmod','wb');
    fwrite($fp,date('Ymd H:i:s'));
    fclose($fp);
  }
  else
  {
      //words to translate
      //echo 'NEW';
      $fp=fopen($lang_path.'-new','ab');
      if(fwrite($fp,'<'.'?php $myecho_new["'.$source.'"]="'.$destination.'"; ?'.'>'."\r\n") === FALSE)
      {
      	fclose($fp);
        return 'Cannot save translation. Check available disk space.';
      }
      else
      {
        fclose($fp);
      }
  }
  
  return('OK');
}

function myecho_load($lang_path='') //:translation_array[$lang_path][$source]=$destination
{
  global $translation_array;

  if(strlen($lang_path)==0)
    $lang_path=cfg_lang_path;

  $myecho=array();
  $myecho_new=array();

  if(file_exists($lang_path.'-new'))
  {
    include($lang_path.'-new');
  }

  if(file_exists($lang_path))
  {
    include($lang_path);
  }
  elseif(file_exists($lang_path.'-dist'))
  {
    include($lang_path.'-dist');
  }

  $myecho=array_merge($myecho_new,$myecho);

  $translation_array[$lang_path]=$myecho;

  return($translation_array);
}

function myecho_path($lang_path) //:created lang_path
{
	if(!file_exists(dirname($lang_path)))
    mkdir(myecho_path(dirname($lang_path)),0777);
  return($lang_path);
}

//---------------------------------------------------------------
if(basename($_SERVER["PHP_SELF"])=='myecho.php')
{
  $destination=$_GET['destination'];
  $source=$_GET['source'];

  if(strlen($destination)>0)
  {
    echo myecho_save($source,$destination);
  }
}
?>
Return current item: OpenBizMap