Source for file Translator.php
Documentation is available at Translator.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: nobody <nobody@localhost> |
// +----------------------------------------------------------------------+
// $Id: Translator.php 334846 2014-09-12 04:50:56Z alan_k $
// Controller Type Class providing translation faciliites
$t = new HTML_Template_Flexy_Translator(array(
'targetLangs' => array('es','fr','zh'),
'appURL' => '/admin/translate.php',
$t->process(isset($_GET ? $_GET : array(),isset($_POST ? $_POST : array()); // read data.. etc.
// you can replace this pretty easily with your own templates..
$t->outputDefautTemplate();
class HTML_Template_Flexy_Translator {
* Options for Translator tool.
'baseLang' => 'en', // the language the templates are in.
'targetLangs' => array ('fr'), // the language the templates are being translated to.
'templateDir' => '', // these are read from global config if not set.
'url_rewrite' => '', // for image rewriting.. -- needs better thinking through!
'appURL' => '', // url to translation too : eg. /admin/translator.php
'driver' => 'dataobjectsimple',
'options' => 'translations'
* app URL (copied from above)
var $languages = array ();
* Array of templates and the words found in each one.
* Array of objects with name, md5's, has it been set, the translation etc.
var $translate = ''; // language being displayed /edited.
* Just set options (no checking done)
* @param array see options array in file.
function HTML_Template_Flexy_Translator ($options= array ()) {
foreach($options as $k=> $v) {
if (!in_array($this->options['baseLang'], $this->options['targetLangs'])) {
$this->options['targetLangs'][] = $this->options['baseLang'];
$o = PEAR5 ::getStaticProperty ('HTML_Template_Flexy','options');
$o = PEAR ::getStaticProperty ('HTML_Template_Flexy','options');
if (!strlen($this->options['templateDir'])) {
$this->options['templateDir'] = $o['templateDir'];
if (!strlen($this->options['compileDir'])) {
$this->options['compileDir'] = $o['compileDir'];
if (!strlen($this->options['url_rewrite'])) {
$this->options['url_rewrite'] = $o['url_rewrite'];
if (empty ($this->options['Translation2'])) {
$this->options['Translation2'] = $o['Translation2'];
$this->appURL = $this->options['appURL'];
$this->languages = $this->options['targetLangs'];
* @param array $_GET; (translate = en)
* @param array $_POST; (translate = en, en[{md5}] = translation)
function process ($get,$post)
//DB_DataObject::debugLevel(1);
$displayLang = isset ($get['translate']) ? $get['translate'] :
(isset ($post['translate']) ? $post['translate'] : false );
if ($displayLang === false ) {
require_once 'Translation2/Admin.php';
$driver = $this->options['Translation2']['driver'];
$options = $this->options['Translation2']['options'];
$usingGT = ($driver == 'gettext');
$usingDO = ($driver == 'dataobjectsimple');
$trd = &Translation2_Admin ::factory ($driver, $options);
//$trd->setDecoratedLang('en');
foreach($this->options['targetLangs'] as $l) {
// back to parent if no language selected..
if (!in_array($displayLang, $this->options['targetLangs'] )) {
return $p->raiseError ('Unknown Language :' . $displayLang);
$this->translate = $displayLang;
if (isset ($post['_apply'])) {
$this->clearTemplateCache ($displayLang);
require_once 'Translation2.php';
$tr = &Translation2 ::factory ($driver, $options);
$tr->setLang ($displayLang);
$suggestions = &Translation2 ::factory ($driver, $options);
$suggestions->setLang ($displayLang);
//$tr->setPageID('test.html');
// delete them after we have compiled them!!
if (isset ($post['_apply'])) {
$this->clearTemplateCache ($displayLang);
//DB_DataObject::debugLevel(1);
$this->loadTranslations ();
$this->loadTranslations ($displayLang);
$displayLangClean = str_replace('.', '_', $displayLang);
foreach($this->words as $page=> $words) {
$status[$page] = array ();
if (isset ($post['_clear']) && !PEAR ::isError ($p = $trd->getPage ($page, $displayLang))) {
foreach ($diff as $string) {
$trd->remove ($string, $page);
foreach ($words as $word) {
$md5 = md5($page. ':'. $word);
$value = $usingDO ? $this->getTranslation ($page,$word,$displayLang) : $tr->get ($word);
if (isset ($post[$displayLangClean][$md5])) {
// eak we shouldnt really deal with magic_quotes!!!
$post[$displayLangClean][$md5]);
$trd->add ($word,$page,array ($displayLang=> $nval));
// try the old gettext...
$trd->add ($word,$page,array ($displayLang=> $old[addslashes($word)]));
if (!$add->to || ($add->from == $add->to )) {
$add->untranslated = true;
$add->suggest = implode(', ', $this->getSuggestions ($word, $displayLang));
$suggest = $suggestions->get ($word);
if ($suggest && ($suggest != $word)) {
$add->suggest = $suggest;
// show big or small text entry..
$add->short = (bool) (strlen($add->from ) < 30 && strstr($add->from , "\n") === false );
//printf("Built All in %0.2fs<BR>",$total);
var $translations = array ();
var $translationMap = array ();
* LoadTranslations - load all the translations from the database
* into $this->translations[{lang}][{id}] = $translation;
function loadTranslations ($lang= false ) {
$d = DB_DataObject ::factory ('translations');
$d->lang = ($lang == false ) ? '-' : $lang;
$this->translations[$d->lang ] = array ();
$this->translations[$d->lang ][$d->string_id ] = $d->translation;
$this->translationMap[$d->page ][$d->translation ] = $d->string_id;
function getSuggestions ($string,$lang) {
//echo '<PRE>';print_r($this->translationMap);
foreach($this->translationMap as $page=> $map) {
if (isset ($map[$string])) {
//echo '<PRE>';print_r(array($string,$lang,$ids,$this->translations[$lang]));
if (isset ($this->translations[$lang][$id])) {
$ret[] = $this->translations[$lang][$id];
// echo '<PRE>';print_r($ret);
function getTranslation ($page,$word,$lang)
if (!isset ($this->translationMap[$page][$word])) {
//echo "No string id for $page : $word\n";
if (!isset ($this->translations[$lang][$this->translationMap[$page][$word]])) {
return $this->translations[$lang][$this->translationMap[$page][$word]];
* compile all the templates in a specified folder.
* @param string subdirectory of templateDir or empty
function compileAll ($d= '')
$dname = $d ? $this->options['templateDir'] . '/'. $d : $this->options['templateDir'];
//echo "Open $dname<BR>";
require_once 'HTML/Template/Flexy.php';
$o['fatalError'] = PEAR_ERROR_RETURN;
while (($name = readdir($dh)) !== false ) {
$fname = $d ? $d . '/'. $name : $name;
if (is_dir($this->options['templateDir'] . '/'. $fname)) {
$this->compileAll ($fname);
$oo = $o; // $oo['debug'] = 1;
$x = new HTML_Template_Flexy ( $oo );
$r = $x->compile ($fname);
//printf(" %0.3fs : $fname<BR>", $time);
echo " compile failed on $fname<BR>";
$this->words[$fname] = file_exists($x->getTextStringsFile ) ?
//echo '<PRE>';print_R($words);exit;
* delete all the compiled templates in a specified language
* @param string subdirectory of templateDir or empty
function clearTemplateCache ($lang= 'en',$d = '') {
$dname = $d ? $this->options['templateDir'] . '/'. $d : $this->options['templateDir'];
while (($name = readdir($dh)) !== false ) {
$fname = $d ? $d . '/'. $name : $name;
if (is_dir($this->options['templateDir'] . '/'. $fname)) {
$this->clearTemplateCache ($lang,$fname);
$file = "{ $this->options['compileDir']}/{ $fname}.{ $lang}.php";
* output the default template with the editing facilities.
function outputDefaultTemplate () {
'compileDir' => ini_get('session.save_path') . '/HTML_Template_Flexy_Translate',
'templateDir' => dirname(__FILE__ ). '/templates'
$x = new HTML_Template_Flexy ( $o );
$x->compile ('translator.html');
Documentation generated on Mon, 11 Mar 2019 16:00:04 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|