summaryrefslogtreecommitdiff
path: root/php/admin/locale/extract_messages
blob: 7ca5dfbd01b96a34bb23ed5a75fc379d82b02134 (plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/kolab/bin/perl
# This script extracts messages from the TPL files to pass along with the PHP files to xgettext
# It also manages the i18n files and directory structure
# 2000 by Romain Pokrzywka <romain@kdab.net>

#
# Check for usage message
#
if($ARGV[0] eq "-h" || $ARGV[0] eq "--help") {
    print "Usage: extract_messages [create|update] languages...\n\n";
    print "This scripts extracts messages from the .php and .tpl files into the file messages.pot. ";
    print "Then it copies this file to every language subdirectory, where appropriate translations can be inserted.\n\n";
    print "There are two working modes : create and update. In create mode, the file messages.po is simply copied from the template file (messages.pot) in each language subdirectory (which is created if needed). In update mode, the already existing file messages.po is merged with the new template file, which may contain new messages, modified and even deleted ones. Note that using create mode on languages with existing messages.po files will fail, just like using update mode on languages without messages.po files.\n";
    print "If you don't specify any mode, then the script will automatically chose the appropriate mode for each language, depending on the existence of the file messages.po.\n\n";
    print "You can specify as many languages as you want, using their locale code, e.g. : fr, de, sv... You can also specify sublanguage codes for finer translations, e.g. : en_GB, en_US, pt_BR... For every language specified, a corresponding subdirectory is created. This directory contains a directory LC_MESSAGES, in which the file messages.po is created. This is also where the file messages.mo (the binary version actually used by xgettext) should be put.\n";
    print "If you don't specify any language, then the script will use all the already existing languages, based on the existing subdirectories.\n";
    exit(0);
}

#
# Retrieve command line arguments
#
$action = "auto";
if($ARGV[0] eq "update" || $ARGV[0] eq "create") {
    $action = $ARGV[0];
    shift(@ARGV);
}
@languages = @ARGV;
#print "languages : " . join(" ", @languages) . "\n";

$templatesDir = "../templates";
$phpDir = "../../../www/admin";
$includeDir = "../include";

#
# Extract messages from the templates
#
$tplMessagesFile = "tpl_messages.php";
open(OUTFILE, ">", "$tplMessagesFile") or die("unable to open output file $tplMessagesFile");
if ( $^V ge v5.8.0 ) {
  binmode(OUTFILE,":utf8");
} else {
  warn ("NOT using utf8! Upgrade to perl >= 5.8.0.\n");
}

print OUTFILE "<?php\n";

@tplFiles = `find $templatesDir -name "*.tpl" -o -name "*.tpl.in" 2> /dev/null`;

foreach $tpl (@tplFiles)
{
        chomp($tpl);
        #print "opening file $tpl...\n";
        open(INFILE, "<", "$templatesDir/$tpl") or die("unable to open file $tpl");
        if ( $^V ge v5.8.0 ) {
            binmode(INFILE,":utf8");
        } else {
            warn ("NOT using utf8! Upgrade to perl >= 5.8.0.\n");
        }
        
        while($line = <INFILE>)
        {
            chomp $line;
            while($line =~ /\{tr.*?msg="(.*?[^\\])".*?\}/g) {
                #print "found message $1\n";
                print OUTFILE "_(\"$1\");\n";
            }
        }
        close(INFILE);
}

print OUTFILE "?>\n";
close(OUTFILE);

#
# Get all the php files recursively in www/admin and in the includes
#
@phpFiles = `find $phpDir -name "*.php" -o -name "*.php.in" 2> /dev/null`;
foreach (@phpFiles) { chomp; }
@includeFiles = `find $includeDir -name "*.php" -o -name "*.php.in" 2> /dev/null`;
foreach (@includeFiles) { chomp; }
#print join(" ", @phpFiles);
#print join(" ", @includeFiles);

#
# Call xgettext on all the php files (including the one generated from templates)
#
$allFiles = "$tplMessagesFile " . join(" ", @phpFiles) . " " . join(" ",@includeFiles);
#print "$allFiles\n";
print `xgettext --language=PHP -o messages.pot $allFiles`;

#
# If no language is specified on the command line, get all the already installed languages
#
if(@languages == 0) {
    @languages = `find . -maxdepth 1 -type d -not -name "." -and -not -name "CVS" 2> /dev/null`;
    foreach (@languages) { chomp; }
    print "languages : " . join(" ", @languages) . "\n";
}

#
# Create directory structure (if needed) then do the proper action
#
foreach $lang (@languages)
{
    `mkdir -p $lang/LC_MESSAGES`;
    #print "current language : $lang\n";
    $initialaction = $action;
    
    if($action eq "auto") {
        if(-e "$lang/LC_MESSAGES/messages.po") {
            $action = "update";
        } else {
            $action = "create";
        }
        print "Using $action mode for language $lang\n";
    }
    
    if($action eq "create") {
        if(-e "$lang/LC_MESSAGES/messages.po") {
            print "messages.po already exists for language $lang. Skipping. (did you intend to update ?)\n";
        } else {
            print `cp messages.pot $lang/LC_MESSAGES/messages.po`;
        }
    } else {
        if( ! -e "$lang/LC_MESSAGES/messages.po" ) {
            print "messages.po doesn't exist for language $lang. Skipping. (did you intend to create ?)\n";
        } else {
            print `msgmerge --update --backup=simple $lang/LC_MESSAGES/messages.po messages.pot`;
        }
    }
    $action = $initialaction;
}

#
# Cleanup
#
print `rm -r tpl_messages.php`;