Created
September 12, 2010 13:42
-
-
Save tkusano/576118 to your computer and use it in GitHub Desktop.
check-gnome-japo.pl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/perl | |
## simple ja.po checker for GNOME Japanese translation team. | |
## Useful if this script used with pofilter. | |
use strict; | |
use warnings; | |
use utf8; | |
use Getopt::Long; | |
use File::Basename; | |
use Text::CSV_XS; | |
use Unicode::Normalize; | |
binmode STDOUT, ':utf8'; | |
our $Before_Matches = sub { }; | |
our $Match_Func = sub { | |
my ( $file, $line_no, $orig, $trans, $msg ) = @_; | |
printf "%s:%d:%s:%s:%s\n", $file, $., $orig, $trans, $msg; | |
}; | |
our $After_Matches = sub { }; | |
our $Csv; | |
main(); | |
exit 0; | |
sub main { | |
my ( $help, $files_with_match ); | |
my $input_csv = 1; ## default | |
GetOptions( | |
'help' => \$help, | |
'files-with-matches|l' => \$files_with_match, | |
'csv' => \$input_csv, | |
) or usage(); | |
if ( @ARGV < 1 || $help ) { | |
usage(); | |
} | |
if ($files_with_match) { | |
my $matched; | |
$Before_Matches = sub { $matched = undef }; | |
$Match_Func = sub { $matched = $_[0]; }; | |
$After_Matches = sub { print $matched, "\n" if defined $matched }; | |
} | |
my @dirs; | |
foreach my $ent (@ARGV) { | |
if ( -d $ent ) { | |
push @dirs, $ent; | |
} | |
elsif ( -r $ent ) { | |
check_file( $ent, $input_csv ); | |
} | |
} | |
foreach my $subdir (@dirs) { | |
check_dir( $subdir, $input_csv ); | |
} | |
} | |
sub usage { | |
my $cmd = basename($0); | |
print STDERR "usage: $cmd [-l] [-h] [--csv] file_or_dir ...\n"; | |
exit 64; ## EX_USAGE | |
} | |
sub get_normal_line { | |
my $fh = shift; | |
} | |
sub check_dir { | |
my $dir = shift; | |
my $input_csv = shift; | |
opendir my $dh, $dir or die "Error: $dir: $!"; | |
my @dirs; | |
while ( defined( my $ent = readdir $dh ) ) { | |
next if $ent =~ /^\./; | |
my $path = "$dir/$ent"; | |
if ( ( !$input_csv && $ent =~ /\.po$/ ) | |
|| ( $input_csv && $ent =~ /\.csv$/ ) && -r $path ) | |
{ | |
check_file( $path, $input_csv ); | |
} | |
elsif ( -d $path ) { | |
push @dirs, $path; | |
} | |
} | |
closedir $dh; | |
foreach my $subdir (@dirs) { | |
check_dir( $subdir, $input_csv ); | |
} | |
} | |
sub _getline_row { | |
my $fh = shift; | |
my $line = <$fh>; | |
return ( '', $line ); | |
} | |
sub _getline_csv { | |
my $fh = shift; | |
my $csv = shift; | |
my $row = $csv->getline($fh); | |
return unless defined $row; | |
return ( $row->[1], $row->[2] ); | |
} | |
sub check_file { | |
my $file = shift; | |
my $input_csv = shift; | |
$Before_Matches->(); | |
my $csv; | |
my $getline; | |
if ($input_csv) { | |
$csv = Text::CSV_XS->new( { binary => 1 } ) or die; | |
$getline = \&_getline_csv; | |
} | |
else { | |
$getline = \&_getline_row; | |
} | |
open my $fh, '<:utf8', $file; | |
while ( my ( $orig, $trans ) = $getline->( $fh, $csv ) ) { | |
if ( $trans ne NFC($trans) ) { | |
$Match_Func->( $file, $., $orig, $trans, 'Not in NFC' ); | |
} | |
if ($trans =~ m/(?<match>\p{InHalfwidthAndFullwidthForms}+|カレンダー| | |
全て|(?:(?:書|読)込(?:み|む)?)|イメージ・ファイル| | |
メディアライブラリ|マウント・ポイント|データ・ディスク| | |
トラック・モード|ローカル・ファイル|パタン|レポジトリ| | |
メインウィドウ|エキスポート| | |
再生一覧|システム[の・]フォント|ユーザリスト| | |
ユーザーのリスト| | |
日本GNOMEユーザ会|プロクシ|ネットワーク.プロキシ| | |
再生一覧|楽曲一覧| | |
通知(?:エリア|スペース)|メモリスト|メモ一覧|MIMEタイプ|ミーティング| | |
リフレッシュ|最新の情報に更新|タスク一覧|メイリングリスト| | |
インタフェ(?:イ|ー)ス|インターフェイス| | |
ディスク・イメージ|シンボリック・リンク|下さい)/sx | |
) | |
{ | |
$Match_Func->( $file, $., $orig, $trans, "rule 1 '$+{match}'" ); | |
} | |
elsif ( $orig =~ m{draft folder}i ) { | |
if ( $trans =~ /ドラフト/ ) { | |
$Match_Func->( $file, $., $orig, $trans, 'rule 2' ); | |
} | |
} | |
elsif ( $orig =~ m{leave\s+full\s*screen mode}i ) { | |
if ( $trans | |
=~ m{フルスクリーン(?:.モード|表示)の解除} ) | |
{ | |
$Match_Func->( $file, $., $orig, $trans, 'rule 3' ); | |
} | |
elsif ( $trans !~ m{フルスクリーンモードの解除} ) { | |
$Match_Func->( $file, $., $orig, $trans, 'rule 4' ); | |
} | |
} | |
elsif ( $orig =~ m{leave\s+full\s*screen}i ) { | |
if ( $trans | |
=~ m{フルスクリーン(?:.?モード|表示)の解除} ) | |
{ | |
$Match_Func->( $file, $., $orig, $trans, 'rule 5' ); | |
} | |
elsif ( $trans !~ m{フルスクリーンの解除} ) { | |
$Match_Func->( $file, $., $orig, $trans, 'rule 6' ); | |
} | |
} | |
elsif ( $orig =~ m{full\s*screen mode}i ) { | |
if ( $trans !~ /フルスクリーンモード/ ) { | |
$Match_Func->( $file, $., $orig, $trans, 'rule 7' ); | |
} | |
} | |
elsif ( $orig =~ m{full\s*screen}i ) { | |
if ( $trans =~ /フル.スクリーン/ ) { | |
$Match_Func->( $file, $., $orig, $trans, 'rule 8' ); | |
} | |
elsif ( $trans =~ /フル.?スクリーン.?モード/ ) { | |
$Match_Func->( $file, $., $orig, $trans, 'rule 9' ); | |
} | |
elsif ( $trans =~ /フル.?スクリーン.?表示|全画面/ ) { | |
$Match_Func->( $file, $., $orig, $trans, 'rule 10' ); | |
} | |
} | |
elsif ( $trans =~ m/<$/ ) { | |
$Match_Func->( $file, $., $orig, $trans, 'rule 11' ); | |
} | |
# elsif ( $orig =~ /[^.]\.$/ && $trans !~ m/。$/) { | |
# $Match_Func->( $file, $., $orig, $trans, 'rule 12' ); | |
# } | |
elsif ( $trans =~ m/^[A-Za-z0-9_]+$/ && $trans =~ /_[a-zA-Z0-9]/ ) { | |
$Match_Func->( $file, $., $orig, $trans, 'rule 13' ); | |
} | |
} | |
close $fh; | |
$After_Matches->(); | |
} | |
# end of check script | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment