Le but de cette classe est de pouvoir importer/exporter un fichier .PO (fichier de traduction utilisé avec gettext !)Manuel pour les fichiers PO

Elle prend un fichier po … le transforme en tableau, pour que vous puissiez le traiter à vonlonter avec vôtre script. (je vous recommande l'utilisation de ADODB pour le stockage dans une base de données.) A la fin elle peut restituer un fichier po prêt à être utiliser dans un projet qui supporte gettext !

Actuellement elle supporte toute les options principales de ces fichiers, sauf les “context” (dont je n'ai pas compris le fonctionnement ! Alors expliquer moi si vous savez !)

Voici le code de la classe ! (Changelog)

<?php
# ***** BEGIN LICENSE BLOCK ***** 
#
# This file is part of PO-php-traitement.
# PO-php-traitement is the legal property of Sebastien Piguet <devel@sebseb01.net>
# Copyright (c) 2007 Sebastien Piguet
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation
#
# 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
#
# ***** END LICENSE BLOCK *****.
 
class PO{
 
	/**
	 * Cette fonction lit un Fichier .po de bout en bout
	 * et le retourne sous forme de tableau (2D)
	 *
	 * @param sting $file
	 * @param string $separator
	 * @return array
	 */
	function Import($file,$separator="||"){
		if (!file_exists($file)) die('Impossible d\'ouvrire le fichier en lecture');
		else $po_file = file($file);
 
		// on initialise !
		$return = array();
		$c_line = 0;
		$nb_line = count($po_file);
	    $msgid=$msgidplural=$msgstr=$msgref=$msgtranscomm=$msgextcomm=$msgflagcomm=$msgrefcomm=$msgprevcomm="";
		$id_return=0;
 
		//lecture du fichier ligne par ligne
		while( $c_line <= $nb_line ){
 
			//Si c'est un # un regarde se qu'il contient
        	if (stripos($po_file[$c_line],'#')===0){
 
        		switch (substr($po_file[$c_line],1,1)) {
        			case '.'://extracted-comments
        				$msgextcomm.=$separator.substr($po_file[$c_line],2,strlen($po_file[$c_line])-3);
        				break;
        			case ':'://reference
        				$msgrefcomm.=$separator.substr($po_file[$c_line],2,strlen($po_file[$c_line])-3);
        				break;
        			case ','://flag
        				$msgflagcomm.=$separator.substr($po_file[$c_line],2,strlen($po_file[$c_line])-3);
        				break;
        			case '|'://msgid previous-untranslated-string
 
        				$msgprevcomm.=$separator.substr($po_file[$c_line],2,strlen($po_file[$c_line])-3);
        				break;
        			default: //Si c'est autre chose on stock dans commantaire pour traducteur
        				$msgtranscomm.=$separator.substr($po_file[$c_line],1,strlen($po_file[$c_line])-2);
        				break;
        		}
 
        	}
 
	        	//////////Si c'est un msgid ... on n'interprete ! (sans context)
	        	if (stripos($po_file[$c_line],'msgid ')===0){ //si c'est un msgid
	        		if ((stripos($po_file[$c_line],'"')+1)==strrpos($po_file[$c_line],'"')) {
	        			if (stripos($po_file[$c_line+1],'"')===false) $msgid.=$separator;
	        		// Si les 2 " sont l'un a cote de l'autre ... c'est une string sur plusieurs lignes
	        			while (stripos($po_file[$c_line+1],'"')===0) //Si la ligne suivante commence par "
	        			{
	        				$c_line++; // on va voir la ligne suivante
	        				$msgid.=$separator.substr($po_file[$c_line],stripos($po_file[$c_line],'"')+1,((strrpos($po_file[$c_line],'"'))-1));// On extrait le texte entre les ""
	        			} 
	        		}
	        		else $msgid=substr($po_file[$c_line],stripos($po_file[$c_line],'"')+1,((strrpos($po_file[$c_line],'"'))-7));// On extrait le texte entre les ""
	        	}
				//////////Si c'est un msgid_plural ... on n'interprete ! (sans context)
	        	if (stripos($po_file[$c_line],'msgid_plural ')===0){ //si c'est un msgid
	        		if ((stripos($po_file[$c_line],'"')+1)==strrpos($po_file[$c_line],'"')) {
	        			if (stripos($po_file[$c_line+1],'"')===false) $msgidplural.=$separator;
	        		// Si les 2 " sont l'un a cote de l'autre ... c'est une string sur plusieurs lignes
	        			while (stripos($po_file[$c_line+1],'"')===0) //Si la ligne suivante commence par "
	        			{
	        				$c_line++; // on va voir la ligne suivante
	        				$msgidplural.=$separator.substr($po_file[$c_line],stripos($po_file[$c_line],'"')+1,((strrpos($po_file[$c_line],'"'))-1));// On extrait le texte entre les ""
	        			} 
	        		}
	        		else $msgidplural=substr($po_file[$c_line],stripos($po_file[$c_line],'"')+1,((strrpos($po_file[$c_line],'"'))-7));// On extrait le texte entre les ""
	        	}
	        	/////////////////////////////
 
	         	//////////Si c'est un msgstr ... on n'interprete ! (sans context)
	        	if (stripos($po_file[$c_line],'msgstr ')===0){ //si c'est un msgid
 
	        		if ((stripos($po_file[$c_line],'"')+1)==strrpos($po_file[$c_line],'"')) {
	        		// Si les 2 " sont l'un a cote de l'autre ... c'est une string sur plusieurs lignes
	        			if (stripos($po_file[$c_line+1],'"')===false) $msgstr.=$separator;
		        		while (stripos($po_file[$c_line+1],'"')===0) //Si la ligne suivante commence par "
		        			{
	        				$c_line++; // on va voir la ligne suivante
	        				$msgstr.=$separator.substr($po_file[$c_line],stripos($po_file[$c_line],'"')+1,(strrpos($po_file[$c_line],'"')-1));// On extrait le texte entre les ""
	        			}
	        		}
	        		else $msgstr=substr($po_file[$c_line],stripos($po_file[$c_line],'"')+1,((strrpos($po_file[$c_line],'"'))-8));// On extrait le texte entre les ""
	        	}
	        	////////////////////////////
 
 
	        	// Si le msgid et le msgstr son oki on prend en compte
	        	if ($msgid!=="" && $msgstr !==""){
	        			$return[$id_return] = array("msgid"=>$msgid,"msgstr"=>$msgstr,"msgcomm"=>$msgtranscomm,"msgextcomm"=>$msgextcomm,"msgflagcomm"=>$msgflagcomm,"msgrefcomm"=>$msgrefcomm,"msgprevcomm"=>$msgprevcomm); 
	        			$id_return++;
					    $msgid=$msgstr=$msgref=$msgtranscomm=$msgextcomm=$msgflagcomm=$msgprevcomm=$msgrefcomm="";
	        	}
        	$c_line++;
    	}
    	return $return;
	}
	function export($array,$separator="||"){
		$return="";//on initialise
		if (!is_array($array)) die ('Wrong parametre');
 
		for ($i=0;$i<count($array);$i++){// on traite tableau par tableau
 
			//Ajoute tout les type de commantaire
			if ($array[$i]['msgcomm']!="") $return.=str_replace($separator,"\n#",$array[$i]['msgcomm']);
			if ($array[$i]['msgextcomm']!="") $return.=str_replace($separator,"\n#.",$array[$i]['msgextcomm']);
			if ($array[$i]['msgrefcomm']!="") $return.=str_replace($separator,"\n#:",$array[$i]['msgrefcomm']);
			if ($array[$i]['msgflagcomm']!="") $return.=str_replace($separator,"\n#,",$array[$i]['msgflagcomm']); 
			if ($array[$i]['msgprevcomm']!="") $return.=str_replace($separator,"\n#,",$array[$i]['msgprevcomm']);
 
			// Affiche le msgid (sur 1 ligne ... vide ... sur plusieurs ligne)
			if (stripos($array[$i]['msgid'],$separator)===false)$return.="\nmsgid \"".$array[$i]['msgid'].'"';
			elseif (stripos($array[$i]['msgid'],$separator)+2==strlen($array[$i]['msgid'])) $return.="\nmsgid \"\"";
			else $return.="\nmsgid \"".str_replace($separator,"\"\n\"",$array[$i]['msgid'])."\"";
 
			// Affiche le msgid_plurarl (sur 1 ligne ... vide ... sur plusieurs ligne)
			if (stripos($array[$i]['msgidplural'],$separator)===false)$return.="\nmsgid_plural \"".$array[$i]['msgidplural'].'"';
			elseif (stripos($array[$i]['msgidplural'],$separator)+2==strlen($array[$i]['msgidplural'])) $return.="\nmsgid_plural \"\"";
			else $return.="\nmsgid_plural \"".str_replace($separator,"\"\n\"",$array[$i]['msgidplural'])."\"";
 
			// Affiche le msgstr (sur 1 ligne ... vide ... sur plusieurs ligne)
			if (stripos($array[$i]['msgstr'],$separator)===false)$return.="\nmsgstr \"".$array[$i]['msgstr'].'"';
			elseif (stripos($array[$i]['msgstr'],$separator)+2==strlen($array[$i]['msgstr'])) $return.="\nmsgstr \"\"";
			else $return.="\nmsgstr \"".str_replace($separator,"\"\n\"",$array[$i]['msgstr'])."\"";
 
			//pour rendre un peut plus lisibile le fichier exporter !
			$return.="\n";
		}
		$return = substr($return,1,strlen($return)); //supprime les 1er retour a la ligne
		return $return;
	}
}
?>

Cette classe est actuellement utilisée dans Codingteam une forge pour le dévoleppement (genre sourceforge mais gratuit.)

Merci de m'en envoyer un mail si vous utilisez cette classe .. Merci

php/mes_classe/po.txt · Last modified: 2008/06/10 23:03 by 85.0.24.55
Recent changes RSS feed Creative Commons License Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki