[pLog-svn] r4890 - plog/branches/lifetype-1.2/tools

reto at devel.lifetype.net reto at devel.lifetype.net
Tue Feb 27 17:49:36 EST 2007


Author: reto
Date: 2007-02-27 17:49:35 -0500 (Tue, 27 Feb 2007)
New Revision: 4890

Added:
   plog/branches/lifetype-1.2/tools/translate.php
   plog/branches/lifetype-1.2/tools/translator.php
   plog/branches/lifetype-1.2/tools/translator.tpl
Log:
tool to translate locales. This is a very early alpha version, barely tested. pleas use with care!
documentation will follow. open translate.php in an editor and adjust the path to the locale file as needed. strings not available in the master locale will be removed and arrays are not supported (but left untouched for later editing).



Added: plog/branches/lifetype-1.2/tools/translate.php
===================================================================
--- plog/branches/lifetype-1.2/tools/translate.php	                        (rev 0)
+++ plog/branches/lifetype-1.2/tools/translate.php	2007-02-27 22:49:35 UTC (rev 4890)
@@ -0,0 +1,24 @@
+<?php
+include_once("./translator.php");
+
+$translate = new Translator();
+
+// do nothing else but set the path (test environment)
+/*
+$translate->setLocaleSlave('./admin/locale_de_DE.php');
+$translate->setLocaleMaster('./admin/locale_en_UK.php');
+*/
+$translate->setLocaleSlave('../locale/locale_de_DE.php');
+$translate->setLocaleMaster('../locale/locale_en_UK.php');
+
+if ( isset($_POST['save']) )
+{
+    $translate->write( $_POST['localeSlaveInput'] );
+    //var_dump($_POST);
+    //echo 'file written!';
+}
+
+$translate->render();
+    
+
+?>

Added: plog/branches/lifetype-1.2/tools/translator.php
===================================================================
--- plog/branches/lifetype-1.2/tools/translator.php	                        (rev 0)
+++ plog/branches/lifetype-1.2/tools/translator.php	2007-02-27 22:49:35 UTC (rev 4890)
@@ -0,0 +1,222 @@
+<?php
+#doc
+#    classname:    ClassName
+#    scope:        PUBLIC
+#
+#/doc
+include_once("../class/template/smarty/Smarty.class.php");
+define(TRANSLATOR_ACTIVATED, true);
+set_magic_quotes_runtime ( 0 );
+
+class Translator
+{
+    var $_localeMaster = ''; 
+    var $_localeSlave = ''; 
+    
+    var $_blacklist = array('Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa','Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday','January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ); 
+
+
+    var $_blogMasterStrings = array();
+    var $_adminMasterStrings = array(); // not implemented
+    
+    var $_blogSlaveStrings = array();
+    var $_adminSlaveStrings = array(); // not implemented
+    
+    function translator() {
+    
+        if(TRANSLATOR_ACTIVATED === false)
+        die('you need to activate the translator class first!');
+        
+       
+    
+    }
+    
+    function render()
+    {
+        
+        $t = new Smarty();
+        
+        $t->template_dir = './';
+        $t->compile_dir = '../tmp';
+
+        
+        $localeSlaveMessages = $this->_getLocaleArray($this->_localeSlave);
+        $localeMasterMessages = $this->_getLocaleArray($this->_localeMaster);
+        
+        $localeSlaveMessages = (!$localeSlaveMessages) ? array() : $localeSlaveMessages;
+        
+        $localeSlaveMessages = $this->_cleanArray($localeSlaveMessages);
+        $localeMasterMessages = $this->_cleanArray($localeMasterMessages);
+    
+        
+        $t->assign('localemasterstrings', $localeMasterMessages);
+        
+        $t->assign('localeslavestrings', $this->_mergeLocales($localeMasterMessages,$localeSlaveMessages) );
+
+        $t->display('translator.tpl');
+    
+    
+    }
+    
+    function write($newStrings)
+    {
+        $mastertpl = file($this->_localeMaster);
+        @unlink($this->_localeSlave);
+        touch($this->_localeSlave);
+        
+        $handle = fopen($this->_localeSlave, "r+b");
+        
+        foreach($mastertpl as $line)
+        {
+            $match = false;
+            
+            foreach($newStrings as $key => $string)
+            {
+                $test = '$messages[\'' . $key . "']";
+                if (stristr($line, $test) && ($string != '+array+'))
+                {
+                    $newline = '$messages[\'' . $key . "'] = '" . $string . "';\n";
+                    fwrite($handle, $newline);
+                    $match = true;
+                    break;
+                }
+            }
+            if ($match === false) 
+            {
+                fwrite($handle, $line);
+            }
+                
+            
+        }
+            
+        fclose($handle);
+        return true;
+    }
+
+    
+    function _mergeLocales($master = array(), $slave = array())
+    {
+        // override all translated strings and leave the rest in the master language
+        
+        
+        $mergedLocale = array();
+        $mergedLocale = array_merge($master, $slave);
+  /*      
+        foreach($master as $stringId => $string )
+        {
+            if (array_key_exists($stringId, $slave))
+            {
+                $mergedLocale["$stringId"] = $slave["$stringId"];
+            }
+            else
+            {
+                if (is_array($string))
+                {
+                    $mergedLocale["$stringId"] = $master["+array+"];
+                }
+                else
+                {
+                    $mergedLocale["$stringId"] = $master["$stringId"];
+                }
+            }
+        }
+        */
+        
+        return $mergedLocale;
+        
+        /*$i = 0;
+        $max = count($string);
+        for ($i = 0; $i < $max; $i++ )
+        {
+            
+        
+        }*/
+    
+    }
+    function _cleanArray($messages)
+    {
+        foreach($messages as $msgId => $msg)
+        {
+            if (is_array($msg))
+            {
+                $messages["$msgId"] = '+array+';
+            }
+            if (in_array($msgId, $this->_blacklist) )
+            {
+                unset($messages["$msgId"]);
+                
+            }
+        }
+        //var_dump($messages);
+        return $messages;
+        
+    }
+    
+    function _getLocaleArray($path = '')
+    {
+        if( is_readable("$path") )
+        {
+            include_once("$path");
+            
+            if(!isset($messages))
+            {
+                $messages = array();
+            }
+            
+            return $messages;
+            
+        } else {
+            return false;
+        } 
+    }
+    function _getLocaleFile($path = '')
+    {
+        if( is_readable("$path") )
+        {
+            return file("$path");
+        } else {
+            return false;
+        } 
+    }
+    
+
+
+/*
+    function setAdminSlave ($path)
+    {
+        $this->_adminSlave = $path;
+        
+        return true;
+    }
+*/
+    
+    function setLocaleSlave ($path)
+    {
+        $this->_localeSlave = $path;
+        
+        return true;
+    }
+
+    
+ /*   
+    function setAdminMaster ($path)
+    {
+        $this->_adminMaster = $path;
+        
+        return true;
+    }
+*/
+    
+    function setLocaleMaster ($path)
+    {
+        $this->_localeMaster = $path;
+        
+        return true;
+    }
+
+
+}
+###
+
+
+?>

Added: plog/branches/lifetype-1.2/tools/translator.tpl
===================================================================
--- plog/branches/lifetype-1.2/tools/translator.tpl	                        (rev 0)
+++ plog/branches/lifetype-1.2/tools/translator.tpl	2007-02-27 22:49:35 UTC (rev 4890)
@@ -0,0 +1,61 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+
+<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
+<head>
+<title>Translator</title>
+
+{literal}
+<style type="text/css">
+
+table {
+    width: 100%;
+    border:none;
+}
+table th, table td {
+    width:50%;
+    text-align:left;
+    vertical-align:top;
+}
+
+textarea {
+    border: 1px solid #ccc;
+    width: 100%;
+    height: 10em;
+}
+</style>
+{/literal}
+</head>
+
+<body>
+
+<div id="header">
+ <h1>Translator v0.1</h1>
+</div>
+
+<form action="" method="post">
+<table summary="locale strings">
+ <tr>
+  <th>Master</th>
+  <th>Slave</th>
+</tr>
+ {foreach from=$localemasterstrings key=mstringId item=mstring} 
+<tr>
+  <td>
+    <code><strong>{$mstringId}</strong>:<br/>
+    {$mstring|escape:html}</code>
+  </td>
+  <td>
+    <code><strong>{$mstringId}</strong>:</code><br/>
+    <textarea name="localeSlaveInput[{$mstringId}]" {if $mstring == '+array+'}readonly="readonly"{/if}>{$localeslavestrings.$mstringId}</textarea>
+  </td>
+ </tr>
+ {/foreach}
+
+ 
+</table>
+<input type="submit" name="save" value="Write to File!" />
+</form>
+
+</body>
+</html>



More information about the pLog-svn mailing list