Skip to content

Instantly share code, notes, and snippets.

@typester
Created March 4, 2011 06:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save typester/854276 to your computer and use it in GitHub Desktop.
Save typester/854276 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use Pod::Usage;
use Getopt::Long;
use Encode;
GetOptions(
\my %option,
qw/po=s strings=s output=s/
);
my $po = $option{po};
my $strings = $option{strings};
pod2usage(-1) unless $po and $strings;
use Locale::Maketext::Extract;
{
package Locale::Maketext::Extract::Plugin::ObjectiveC;
use base 'Locale::Maketext::Extract::Plugin::Base';
sub file_types { qw/m mm/ }
sub extract {
my $self = shift;
local $_ = shift;
my $line = 1;
while (/\G(.*?(?:L|NSLocalizedString(?:FromTable)?)\(\s*\@"((?:[^"\\]|\\.)*)"\s*(.*?)\))/gs) {
my ($str, $vars) = ($2, $3);
$line += ( () = ($1 =~ /\n/g) ); # cryptocontext!
$self->add_entry($str, $line, $vars);
}
}
}
$INC{'Locale/Maketext/Extract/Plugin/ObjectiveC.pm'} = 1; # fake
use Locale::Maketext::Extract;
my $ext = Locale::Maketext::Extract->new(
plugins => { 'Locale::Maketext::Extract::Plugin::ObjectiveC' => [] }
);
$ext->extract_file($_) for @ARGV;
$ext->read_po($po);
$ext->compile;
my $out = q[];
for my $msgid ($ext->msgids) {
$ext->normalize_space($msgid);
$out .= "\n";
(my $val = $ext->msg_variables($msgid)) =~ s!^#!//!;
$out .= $val;
(my $pos = $ext->msg_positions($msgid)) =~ s!^#!//!;
$out .= $pos;
(my $key = $msgid) =~ s/\\\\//g;
(my $value = $ext->msgstr($msgid)) =~ s/(?=[\\"])/\\/g;
$out .= sprintf qq["%s" = "%s";\n], $key, $value;
}
Encode::from_to($out, 'utf-8', 'utf-16');
open my $fh, '>', $option{strings} or die $!;
print $fh $out;
close $fh;
__END__
=head1 NAME
po2strings.pl - .po to .strings converter
=head1 SYNOPSIS
po2strings.pl --po=input.po --strings=output.strings src/*.m ...
=cut
#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long;
use Pod::Usage;
GetOptions(
\my %option,
qw/output=s help/,
);
pod2usage(0) if $option{help};
pod2usage(-1) unless $option{output};
use Locale::Maketext::Extract;
{
package Locale::Maketext::Extract::Plugin::ObjectiveC;
use base 'Locale::Maketext::Extract::Plugin::Base';
sub file_types { qw/m mm/ }
sub extract {
my $self = shift;
local $_ = shift;
my $line = 1;
while (/\G(.*?(?:L|NSLocalizedString(?:FromTable)?)\(\s*\@"((?:[^"\\]|\\.)*)"\s*(.*?)\))/gs) {
my ($str, $vars) = ($2, $3);
$line += ( () = ($1 =~ /\n/g) ); # cryptocontext!
$self->add_entry($str, $line, $vars);
}
}
}
$INC{'Locale/Maketext/Extract/Plugin/ObjectiveC.pm'} = 1; # fake
my $ext = Locale::Maketext::Extract->new(
plugins => {
'Locale::Maketext::Extract::Plugin::ObjectiveC' => ['*'],
},
);
$ext->extract_file($_) for @ARGV;
if (my $po = $option{output}) {
$ext->read_po($po) if -r $po and -s _;
$ext->compile;
$ext->write_po($po);
}
__END__
=head1 NAME
xgetext.pl - pseudo xgettext.pl for Objective-C NSLocalizedString
=head1 SYNOPSIS
xgettext.pl -o output.po src/*.m
=cut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment