[Linpha-cvs] SF.net SVN: linpha: [4419] trunk/linpha2
Status: Inactive
Brought to you by:
bzrudi
From: <bz...@us...> - 2006-03-19 17:59:33
|
Revision: 4419 Author: bzrudi Date: 2006-03-19 09:59:21 -0800 (Sun, 19 Mar 2006) ViewCVS: http://svn.sourceforge.net/linpha/?rev=4419&view=rev Log Message: ----------- language stuff enhancements Modified Paths: -------------- trunk/linpha2/ChangeLog trunk/linpha2/lib/classes/linpha.functions.php Added Paths: ----------- trunk/linpha2/lib/lang/language.php Modified: trunk/linpha2/ChangeLog =================================================================== --- trunk/linpha2/ChangeLog 2006-03-17 10:19:02 UTC (rev 4418) +++ trunk/linpha2/ChangeLog 2006-03-19 17:59:21 UTC (rev 4419) @@ -1,3 +1,13 @@ +2006-03-19 bzrudi <linpha2_AT_tuxpower_DOT_de> + * added new file: /lib/lang/language.php + + list already known phrases + + makes it possible to edit/create language files via webinterface + + added "autocollector" of new phrases to translate tr() function + (a file /var/tmp/lang.temp.php is created which automagically saves + all new added translations in any files using tr()function + So - as of now please make always use of tr()! + (still incomplete!) + 2006-03-17 bzrudi <linpha2_AT_tuxpower_DOT_de> * added basic language support (yet incomplete) Modified: trunk/linpha2/lib/classes/linpha.functions.php =================================================================== --- trunk/linpha2/lib/classes/linpha.functions.php 2006-03-17 10:19:02 UTC (rev 4418) +++ trunk/linpha2/lib/classes/linpha.functions.php 2006-03-19 17:59:21 UTC (rev 4419) @@ -3,25 +3,52 @@ /** * Take care of translation * - * This simple method takes care of all translation related stuff + * This simple method takes care of all translation related stuff. + * If option tr_learn is set - all new entries will be autolearned. + * @param string $text phrase/word to translate */ function tr($text) { - /** - * make language array global - */ - global $translate; + global $translate; + $tr_learn = true; //autolearn new language entries + + /** + * translation starts here + */ + if(true == isset($translate[$text])) + { + echo "$translate[$text]"; + } + else + { + echo "$text"; + } + + /** + * autolearn new language entries defined by tr() function. + */ + if($tr_learn == true) + { + $tmpfile = LINPHA_DIR."/var/tmp/lang.temp.txt"; + + if(false == file_exists("$tmpfile")) + { + if(false == touch("$tmpfile")) + { + echo "Unable to create ".$tmpfile." - please check permissions"; + } + } + + $filedata = file("$tmpfile"); + + if(false == in_array($text."\n", $filedata)) + { + $filedata[] = $text."\n"; + file_put_contents($tmpfile, $filedata); + } + } +} // end tr() - if(true==isset($translate[$text])) - { - echo "$translate[$text]"; - } - else - { - echo "$text"; - } - } - /** * print the main menu * Added: trunk/linpha2/lib/lang/language.php =================================================================== --- trunk/linpha2/lib/lang/language.php (rev 0) +++ trunk/linpha2/lib/lang/language.php 2006-03-19 17:59:21 UTC (rev 4419) @@ -0,0 +1,403 @@ +<?php +/* +* Copyright (c) 2006 Heiko Rutenbeck <bz...@tu...> +* +* 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., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ +if(!defined('LINPHA_DIR')) { define('LINPHA_DIR','../..'); } + +print_xhtml_header(); +print_menu_head(); + +(!isset($_GET['action'])) ? $_GET['action'] = "info": ''; + +/** + * switch page content + */ +switch($_REQUEST['action']) +{ + case "show_phrases": + print_phrases_list(); + break; + case "show_lang_files": + print_lang_file_select(); + break; + case "edit_lang": + edit_lang_files($_POST['language']); + break; + case "save_lang": + save_lang_files($_POST['language'], $_POST['phrase']); + break; + case "info": + echo "Please choose from the menu above"; + break; +} + +/** + * scan recursive for all php files in all subfolders + * @param string $dir + * @param int $counter default = 0 + */ +/* +function scanRecursive($dir, $counter) +{ + global $counter; + global $filelist; + if(!isset($counter)): $counter= 0; endif; + + $ignore = array (".", "..", ".svn", ".project", "albums", "install", "adodb", + "phpmeta", "graphics", "var"); + + if (is_dir($dir)) + { + $files = scandir($dir); + + foreach ($files as $file) + { + if(!in_array($file, array_values($ignore))) + { + $currentfile = $dir."/".$file; + $last_dir = ""; + $count = substr_count($currentfile, '/'); + $sub_count = substr_count($_SERVER['DOCUMENT_ROOT'], '/'); + $count -= ($sub_count + 2); + + if (is_dir($currentfile)) + { + scanRecursive($currentfile, $counter); + } + else + { + $filelist[$counter] = $currentfile; + $counter++; + } + } + } + } + return $filelist; // array() +} +*/ + +/** + * parse $tmpfile and output a list of all known phrases/words + * @param none + */ +function print_phrases_list() +{ + $tmpfile = LINPHA_DIR."/var/tmp/lang.temp.txt"; + + if(file_exists("$tmpfile")) + { + $phrases = file("$tmpfile"); + natcasesort($phrases); + foreach($phrases as $phrase) + { + echo $phrase."<br>"; + } + } + else + { + echo "Failed to open temporary lang file ($tmpfile)"; + } +} + +/** + * show all available language files an let user choose to edit/update + * @param none + */ +function print_lang_file_select() +{ + if(false == check_file_permissions()) + { + die("<strong>Please set right permissions first - aborting</strong>"); + } + + echo "<hr>Please choose language file to edit:<br />"; + echo "<form method='POST' action=".$_SERVER['PHP_SELF'].">"; + + build_lang_file_select(); + + echo " <input type=submit value='Go'>"; + echo " or create a <a href='#'><strong>new one</<strong></a>"; + echo "<input type='hidden' name='action' value='edit_lang'>"; + echo "</form>"; +} + +/** + * This function takes care right file permissions for language file stuff + * @param none + */ +function check_file_permissions() +{ + +$decperms = fileperms("".LINPHA_DIR."/lib/lang/"); +$octalperms = sprintf("%o",$decperms); +$dir_perms = (substr($octalperms,1)); + +echo "<strong>Checking write permissions of /lang folder...</strong><br>"; + +if($dir_perms != "0777") +{ + echo "<font color='#ff0000'>Warning: Please change /lang directory " . + "permissions from ".$dir_perms." to 0777!</font><br>"; + $permissions_ok = false; +} +else +{ + echo "<font color='#00FF00'>OK: /lang directory permissions are OK " . + "(".$dir_perms.")</font><br>"; + $permissions_ok = true; +} + /* parse all language files in dir*/ + $dir_handle = opendir("".LINPHA_DIR."/lib/lang/"); + $file_handle = readdir($dir_handle); + + while($file_handle != false) + { + $explode = explode(".",$file_handle); + if($explode[0] == "lang") + { + if(false == is_writeable("$file_handle")) + { + echo "<font color='#ff0000'>Warning: Seems that ".$file_handle."" . + " is NOT writeable, please set to (0666) first!</font><br>"; + $file_error = true; + }else + { + $file_error = false; + } + } + $file_handle = readdir($dir_handle); + } + + if(true == $permissions_ok AND false == $file_error) + { + echo "<font color='#00FF00'>OK: All language files are set " . + "writeable!</font><br>"; + return true; + } + else + { + return false; + } +} + + +/** + * return array of all available language files found + * in /lib/lang to be used in select field + * @param none + **/ +function build_lang_file_select() +{ + +$path = LINPHA_DIR."/lib/lang"; + +$file_handle = opendir($path); +$file = readdir($file_handle); +$options = array(); + + while($file != false) + { + $explode = explode(".",$file); + + /* Only files that starts with 'lang.' are language files. */ + if($explode[0] == "lang") + { + $options[$explode[1]] = $explode[1]; + } + $file = readdir($file_handle); + } + natcasesort($options); + + form_generate_select("language", "", $options); +} + + + +/** + * autogenrate select field for all available language files + * @param string $name name of select field + * @param string $size vsize of selectfield, e.g. height + * @param array $values array of all available language files + */ +function form_generate_select($name, $size, $values) +{ + print "<select name='".$name."' size='".$size."'>\n"; + foreach ($values as $key => $value) + { + echo "<option value='".$key."'>".$value."</option>\n"; + } + print "</select>"; +} + + + +/** + * edit language files + * @param string $langfile name of language file to edit + */ +function edit_lang_files($langfile) +{ + global $translate; + $langfile = LINPHA_DIR."/lib/lang/lang.".$langfile.".php"; + $tmpfile = LINPHA_DIR."/var/tmp/lang.temp.txt"; + + if(file_exists("$langfile") && file_exists("$tmpfile")) + { + include_once("$langfile"); + $collected = file("$tmpfile"); + } + else + { + die("Failed to include: ".$langfile.""); + } + + /** + * collect missing entries in langfile + */ + foreach($collected as $phrase) + { + if(false == array_key_exists(trim($phrase), $translate)) + { + $new_phrases[] = $phrase; + } + } + + if(false == isset($new_phrases)) + { + echo "FINE - Your language file is up to date"; + } + else + { + echo "<table width='80%' colspan='0' rowspan='0'>" . + "<tr><td colspan='2'>" . + "Please translate all missing entries" . + "</td></tr>" . + "<form method='POST' action=".$_SERVER['PHP_SELF'].">" . + "<tr>"; + + foreach($new_phrases as $phrase2tr) + { + echo "<td width='30%'>$phrase2tr</td>" . + "<td><input type='text' name='phrase[$phrase2tr][]'></td></tr>"; + } + + echo "<input type='hidden' name='action' value='save_lang'>" . + "<input type='hidden' name='language' value='$langfile'>" . + "<tr><td colspan='2'>" . + "<input type='submit' name='submit' value='submit'>" . + "</td></tr>" . + "</form></table>"; + } +} + + + +/** + * save translation to file + * @param string $langfile language filename incl. full path + * @param array $phrases array of all translated parts + */ +function save_lang_files($langfile, $phrases) +{ + + if(file_exists($langfile)) + { + include_once($langfile); + } + + /** + * merge already found translation phrases with the new ones + */ + while (list($phrase, $translation) = each($phrases)) + { + foreach($translation as $translated) + { + if(false == empty($translated)) + { + $translate[$phrase] = $translated; + } + } + } + + $last_key = end(array_keys($translate)); + reset($translate); + $last_value = array_pop($translate); + reset($translate); + + $file_data = "<?php\n"; + $file_data .= "\$translate = array (\n"; + + while(list($phrase , $translation) = each($translate)) + { + $file_data .= "\"".trim($phrase)."\""." => "."\"$translation\","."\n"; + } + + $file_data .= "\"".trim($last_key)."\""." => "."\"$last_value\"\n"; + $file_data .= ");\n"; + $file_data .= "?>"; + + + $fp = fopen("$langfile", "w+"); + fwrite($fp, $file_data); + fclose($fp); + //chmod($file_handle, 0666); + +} + + + +/** + * Prepare valid XHTML header + */ +function print_xhtml_header() +{ + ?> + <!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" xml:lang="en" lang="en-AU"> + + <head> + + <meta http-equiv="content-type" content="application/xhtml+xml; charset=UTF-8"/> + <meta name="author" content="The LinPHA Developers" /> + <meta name="keywords" content="Linpha" /> + <meta name="description" content="The LinPHA2 Photo Archive" /> + + <title>LinPHA - Translation Module</title> + + </head> + <body bgcolor="#808000"> + <?php +} + +/** + * Prepare site Menu + */ +function print_menu_head() +{ + echo "<strong>Welcome To LinPHA2 Translation Module"."</strong><hr /> "; + echo "<ul>"; + echo "<li><a href=".$_SERVER['PHP_SELF']."?action=show_phrases>" . + "Show list of already known phrases/words</a>"; + echo "<li><a href=".$_SERVER['PHP_SELF']."?action=show_lang_files>" . + "Edit/Update language files</a>"; + //echo "<li><a href=".$_SERVER['PHP_SELF']."?action=info>Dummy</a>"; + echo "</ul>"; + echo "<hr />"; +} +?> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |