po-php.pl 1.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
#!/usr/bin/perl -w

# Takes a Mahara .po file generated by php-po.php, and converts it
# into a Mahara language pack containing help files (html) and string
# files (php).

# Assumes the .po reference comments contain either an html filename
# or a php filename followed by a space and a mahara language string
# key.

# po-php.pl /path/to/po/files/fr-1.3_STABLE.po /path/to/langpacks/fr.utf8 fr.utf8

13
use File::Path qw(mkpath);
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use File::Basename qw(fileparse);
use Locale::PO;

my ($inputfile, $outputdir, $lang) = @ARGV;

my $strings = Locale::PO->load_file_asarray($inputfile);

my %htmlfiles = ();
my %phpfiles = ();

foreach my $po (@$strings) {
    my $content = $po->msgstr();
    $content =~ s{\\n}{\n}g;
    my $reference = $po->reference();
28
    next if ( ! defined $reference );
29
    if ($reference =~ m{^(\S*lang/)\S+\.utf8(/\S+)\.html$}) {
30
31
32
        my $filename = $1 . $lang . $2 . '.html';
        $htmlfiles{$filename} = $po->dequote($content);
    }
33
    elsif ($reference =~ m{^(\S*lang/)\S+\.utf8(/\S+)\.php\s+(\S+)$}) {
34
35
36
37
38
39
40
41
42
        my $key = $3;
        my $filename = $1 . $lang . $2 . '.php';
        $phpfiles{$filename}->{$key} = $content;
    }
}

foreach my $htmlfile (keys %htmlfiles) {
    my ($filename, $subdir, $suffix) = fileparse($htmlfile);
    my $dir = $outputdir . '/' . $subdir;
43
    mkpath($dir);
44
45
46
47
48
49
50
51
    open(my $fh, '>', "$dir/$filename");
    print $fh $htmlfiles{$htmlfile};
    close $fh;
}

foreach my $phpfile (keys %phpfiles) {
    my ($filename, $subdir, $suffix) = fileparse($phpfile);
    my $dir = $outputdir . '/' . $subdir;
52
    mkpath($dir);
53
54
55
56
57
58
59
60
61
    open(my $fh, '>', "$dir/$filename");
    print $fh "<?php\n\ndefined('INTERNAL') || die();\n\n";
    foreach my $key (sort keys %{$phpfiles{$phpfile}}) {
        print $fh "\$string['$key'] = " . $phpfiles{$phpfile}->{$key} . ";\n";
    }
    close $fh;
}