Recent Topics

1 Mar 31, 2006 15:37    

Subj ?

2 pvasili Apr 01, 2006 20:54

pvasili

pvasili wrote:

Subj ?

Imported ;)

1) Copy the po_to_php.php in to you "blogs/locales/you_lang file/" folder.
2) Make messages.po file in poEdit
3) Open po_to_php.php in you browser _global.php appear. Enjoy :)

Thank you all

po_to_php.php

<?php

// Get PO file for that edit_locale:
$po_file = dirname(__FILE__).'/LC_MESSAGES/messages.po';
if( ! is_file( $po_file ) )
{
	echo 'No file: '.$po_file.' :( <br>';
}

$outfile = dirname(__FILE__).'/_global.php';
if( !is_writable($outfile) )
{
	echo $outfile.' Read Only or not fiund :( <br>';
}

// File exists:
// Get PO file for that edit_locale:
$lines = @file( $po_file);
$lines[] = '';	// Adds a blank line at the end in order to ensure complete handling of the file
$all = 0;
$fuzzy=0;
$untranslated=0;
$translated=0;
$status='-';
$matches = array();
$sources = array();
$loc_vars = array();
$ttrans = array();
foreach ($lines as $line)
{

	// echo 'LINE:', $line, '<br />';

	if(trim($line) == '' )
	{ // Blank line, go back to base status:
		if( $status == 't' )
		{ // ** End of a translation **:
			if( $msgstr == '' )
			{
				$untranslated++;
				// echo 'untranslated: ', $msgid, '<br />';
			}
			else
			{
				$translated++;

				// Inspect where the string is used
				$sources = array_unique( $sources );
				// echo '<p>sources: ', implode( ', ', $sources ), '</p>';
				foreach( $sources as $source )
				{
					if( !isset( $loc_vars[$source]  ) ) $loc_vars[$source] = 1;
					else $loc_vars[$source] ++;
				}

				// Save the string
				// $ttrans[] = "\n\t'".str_replace( "'", "\'", str_replace( '\"', '"', $msgid ))."' => '".str_replace( "'", "\'", str_replace( '\"', '"', $msgstr ))."',";
				// $ttrans[] = "\n\t\"$msgid\" => \"$msgstr\",";
				$ttrans[] = "\n\t'".str_replace( "'", "\'", str_replace( '\"', '"', $msgid ))."' => \"".str_replace( '$', '\$', $msgstr)."\",";

			}
		}
		$status = '-';
		$msgid = '';
		$msgstr = '';
		$sources = array();
	}
	elseif( ($status=='-') && preg_match( '#^msgid "(.*)"#', $line, $matches))
	{ // Encountered an original text
		$status = 'o';
		$msgid = $matches[1];
		// echo 'original: "', $msgid, '"<br />';
		$all++;
	}
	elseif( ($status=='o') && preg_match( '#^msgstr "(.*)"#', $line, $matches))
	{ // Encountered a translated text
		$status = 't';
		$msgstr = $matches[1];
		// echo 'translated: "', $msgstr, '"<br />';
	}
	elseif( preg_match( '#^"(.*)"#', $line, $matches))
	{ // Encountered a followup line
		if ($status=='o')
			$msgid .= $matches[1];
		elseif ($status=='t')
			$msgstr .= $matches[1];
	}
	elseif( ($status=='-') && preg_match( '@^#:(.*)@', $line, $matches))
	{ // Encountered a source code location comment
		// echo $matches[0],'<br />';
		$sourcefiles = preg_replace( '@\\\\@', '/', $matches[1] );
		// $c = preg_match_all( '@ ../../../([^:]*):@', $sourcefiles, $matches);
		$c = preg_match_all( '@ ../../../([^/:]*/?)@', $sourcefiles, $matches);
		for( $i = 0; $i < $c; $i++ )
		{
			$sources[] = $matches[1][$i];
		}
		// echo '<br />';
	}
	elseif(strpos($line,'#, fuzzy') === 0)
		$fuzzy++;

  }


$fp = @fopen( $outfile, 'w+' );
@fwrite( $fp, "<?php\n" );
@fwrite( $fp, "/*\n" );
@fwrite( $fp, " * Global lang file\n" );
@fwrite( $fp, " * This file was generated automatically from messages.po\n" );
@fwrite( $fp, " */\n" );
@fwrite( $fp, "if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );" );
@fwrite( $fp, "\n\n" );

$edit_locale = substr(realpath("."),count(realpath("."))-3,2);
$edit_locale = strtolower($edit_locale)."_".strtoupper($edit_locale);

fwrite( $fp, "\n\$trans['".$edit_locale."'] = array(" );

foreach( $ttrans as $line )
{
	//echo htmlspecialchars( $line );
@	fwrite( $fp, $line );
}
@  fwrite( $fp, "\n);\n ?>" );
@	 fclose( $fp );

Echo 'Finish !';
?>


Form is loading...