#!/usr/bin/php
1 ) {
$root_dir = $argv[1];
}
$d = opendir( $root_dir );
if ( $d ) {
echo "calculating locale statistics...\n";
$outpath = $root_dir . '/' . 'locale_stats.txt';
$fh = fopen( $outpath, 'w' );
$ignore_dirs = [ '.', '..', 'CVS' ];
while ( false !== ( $file = readdir( $d ) ) ) {
if ( is_dir( $root_dir . '/' . $file ) && !in_array( $file, $ignore_dirs ) ) {
$stats = calcStats( $root_dir, $file );
$pct = $stats['pct_complete'];
$team = $stats['team'];
fwrite( $fh, "$file|$pct|$team\n" );
}
}
closedir( $d );
fclose( $fh );
echo "done. stats saved in $outpath\n";
}
function calcStats( $root_dir, $locale ) {
$messages = 0;
$translations = 0;
$fuzzy = 0;
$team = '';
$path = $root_dir . '/' . $locale . '/LC_MESSAGES/messages.po';
// echo "
$path";
if ( file_exists( $path ) ) {
$lines = file( $path );
$in_msgid = false;
$in_msgstr = false;
$found_translation = false;
$found_msg = false;
foreach ( $lines as $line ) {
// ignore comment lines
if ( $line[0] == '#' ) {
continue;
}
// Parse out the contributors.
if ( strstr( $line, '"Language-Team: ' ) ) {
$endpos = strpos( $line, '\n' );
if ( $endpos === false ) {
$endpos = strlen( $line ) - 2;
}
$len = strlen( '"Language-Team: ' );
$field = substr( $line, $len, $endpos - $len );
$names = explode( ',', $field );
foreach ( $names as $name ) {
if ( $name != 'none' ) {
if ( $team != '' ) {
$team .= ',';
}
$team .= trim( $name );
}
}
}
if ( strstr( $line, 'msgid "' ) ) {
$in_msgid = true;
$in_msgstr = false;
$found_msg = false;
$found_translation = false;
}
if ( $in_msgid && !$found_msg && strstr( $line, '"' ) && !strstr( $line, '""' ) ) {
// echo "msgid: $line";
$found_msg = true;
$messages++;
} else if ( strstr( $line, 'msgstr "' ) ) {
$in_msgstr = true;
$in_msgid = false;
}
if ( $in_msgstr && $found_msg && !$found_translation ) {
if ( strstr( $line, '"' ) && !strstr( $line, '""' ) ) {
// echo "msgstr: $line";
$translations++;
$found_translation = true;
}
} else if ( strstr( $line, '#, fuzzy' ) ) {
$fuzzy++;
}
}
}
$translations -= $fuzzy;
$pct_complete = $messages ? (int)( ( $translations / $messages ) * 100 ) : 0;
return [ 'pct_complete' => $pct_complete, 'team' => $team ];
}
?>