Recent Topics

convert messages.po to _global.php

Started by on Apr 15, 2006 – Contents updated: Apr 15, 2006

Apr 15, 2006 21:44    

Copy this file into blogs\locales\you_localr\ folder and run

<?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 !';
?>

[/php]

Jan 03, 2008 22:48

pvasili wrote:

line 79: elseif( preg_match( '#: ', $line, $matches))

is giving me problems (about 100 times):
Warning: preg_match() [function.preg-match]: No ending delimiter '#' found in /home/www/onkologen.dk/locales/sv_SE/convert.php on line 79
Finish !

Could be that my input file is incomplete:
#, fuzzy
msgid "File Group"
msgstr "Grupp"

Maybe you could adapt the script to accept fuzzy lines.
I'm VBA coder, not PHP coder :-(

Jan 03, 2008 23:03

I haven't got a clue where it's about but the error is explained as follows:
The regex needs a starter equal to a finisher. In this case there is neither, but I am not sure. Either # *is* a starter and needs a finisher, like

elseif( preg_match( '#: #', $line, $matches))   //searches fo r : 


but more likely the sarchsruing is '#: ' in which case it need both a starter and a finisher like:

elseif( preg_match( '@#: @', $line, $matches))   //searches for #:


Where starter and finisher can be any symbol that is not searched foir in the expression.

Hope this helps either you or pvasili

Good luck

Jan 04, 2008 21:06

Afwas wrote:

...
Hope this helps either you or pvasili

Good luck

Thanx! but I think this script won't work with beta 2.3.0.
Lines like this:
#: ..\..\..\inc\CONTROL\collections\categories.php:229
are giving warnings.
(I'm using echo to see line processing).

Sep 23, 2008 06:45

This error is caused by a missing '#' in line 79 of the posted code. This should be '#: #' instead of '#: '
You can fix the code yourself after copying the above code in a PHP file and modify line 79 accordingly, or download the corrected PHP file here: http://gerardprins.com/download/you_localr.zip
Unzip the file and copy it into the root of your language folder (e.j.: ..locales/es_ES, ../locales/nl_NL), upload it, and then run it from your browser (e.j.: yoursite.com/locales/es_ES/you_localr.php)
It will look for messages.po in the LC_MESSAGES folder (make sure it is there and named correctly) and OVERWRITE the existing global.php. Make sure you have a copy_/backup in case anything goes wrong.
Download the resulting file onto your local system, and make any corrections necessary, using Dreamweaver or any other plain text editor.
The file typically will not include the array options (in my experience) so you might want to copy/paste those from an existing global.php file and modify.
Hope this helps.


Form is loading...

Open Source CMS – This forum is powered by b2evolution CMS, a complete engine for your website.