/usr/bin/change-po-charset is in potool 0.16-3.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | #!/usr/bin/perl
# Changes the "charset" attribute value of the Content-Type header in a po file.
# Does NOT actually change the encoding of the file.
#
# Copyright 2007-2009 Marcin Owsiany
# This script is public domain.
use strict;
use warnings;
my $debug = 0;
my $charset = shift @ARGV;
die "Usage: change-po-charset <charset> [ file ] [ ... ]\n" unless defined $charset;
# States
my ($S_BLANK, $S_COMMENT, $S_MSGCTXT, $S_MSGID, $S_MSGSTR) = (1..10);
# Possible regexes
my %r = (
blank => qr{^\s*$}oi,
comment => qr{^#}oi,
msgctxt => qr{^msgctxt\s+"(.*)"$}oi,
msgid => qr{^msgid(_plural)?\s+".*"$}oi,
msgstr => qr{^msgstr(\[\w+\])?\s+".*"$}oi,
string => qr{^".*"$}oi,
);
my $subst = qr{^("Content-Type:.*charset=)(.*?)(( *;.*)?\\n")$}oi;
# Current line
my $l;
# Current line number
my $n = 0;
# Curerrent state
my $s = $S_BLANK;
# Whether we've done the job
my $done = 0;
while ($l = <>) {
chomp $l;
my $orig_s = $s;
$n++;
if ($done) {
print $l, "\n" or die "print failed: $!";
next;
} elsif ($s == $S_BLANK && $l =~ $r{blank}) {
} elsif (($s == $S_BLANK || $s == $S_COMMENT) && $l =~ $r{comment}) {
$s = $S_COMMENT;
} elsif (($s == $S_BLANK || $s == $S_COMMENT) && $l =~ $r{msgctxt}) {
$s = $S_MSGCTXT;
} elsif (($s == $S_BLANK || $s == $S_COMMENT || $s == $S_MSGCTXT) && $l =~ $r{msgid}) {
$s = $S_MSGID;
} elsif ($s == $S_MSGID && $l =~ $r{msgstr}) {
$s = $S_MSGSTR;
} elsif ($s == $S_MSGID && ($l =~ $r{string} or $l =~ $r{msgid})) {
} elsif ($s == $S_MSGSTR && ($l =~ $r{string} or $l =~ $r{msgstr})) {
} elsif ($s == $S_MSGSTR && $l =~ $r{blank}) {
$s = $S_BLANK;
} else {
die "Unrecognized line num. $n in state $s: [$l], exiting";
}
if ($s == $S_MSGSTR) {
$done = $l =~ s{$subst}{${1}${charset}${3}}i;
print STDERR "CHANGED\n" if $debug and $done;
}
print $l, "\n" or die "print failed: $!";
printf STDERR "%03d: %s->%s: [%s]\n", $n, $orig_s, $s, $l if $debug;
}
close(STDOUT) or die "close failed: $!";
exit($done ? 0 : 2);
|