]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/language/lang2po.php
MediaWiki 1.17.0
[autoinstalls/mediawiki.git] / maintenance / language / lang2po.php
1 <?php
2 /**
3  * Convert Language files to .po files !
4  *
5  * Todo:
6  *   - generate .po header
7  *   - fix escaping of \
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  * http://www.gnu.org/copyleft/gpl.html
23  *
24  * @ingroup MaintenanceLanguage
25  */
26
27 /** This is a command line script */
28 require_once( dirname( __FILE__ ) . '/../Maintenance.php' );
29 require_once( dirname( __FILE__ ) . '/languages.inc' );
30
31 define( 'ALL_LANGUAGES',    true );
32 define( 'XGETTEXT_BIN',     'xgettext' );
33 define( 'MSGMERGE_BIN',     'msgmerge' );
34
35 // used to generate the .pot
36 define( 'XGETTEXT_OPTIONS', '-n --keyword=wfMsg --keyword=wfMsgForContent --keyword=wfMsgHtml --keyword=wfMsgWikiHtml ' );
37 define( 'MSGMERGE_OPTIONS', ' -v ' );
38
39 define( 'LOCALE_OUTPUT_DIR', $IP . '/locale' );
40
41 class Lang2Po extends Maintenance {
42         public function __construct() {
43                 parent::__construct();
44                 $this->mDescription = "";
45                 $this->addOption( 'lang', 'a lang code you want to generate a .po for (default: all langs)', false, true );
46         }
47
48         public function execute() {
49                 // Generate a template .pot based on source tree
50                 $this->output( "Getting 'gettext' default messages from sources:" );
51                 $this->generatePot();
52                 $this->output( "done.\n" );
53
54
55                 $langTool = new languages();
56                 if ( $this->getOption( 'lang', ALL_LANGUAGES ) === ALL_LANGUAGES ) {
57                         $codes = $langTool->getLanguages();
58                 } else {
59                         $codes = array( $this->getOption( 'lang' ) );
60                 }
61
62                 // Do all languages
63                 foreach ( $codes as $langcode ) {
64                         $this->output( "Loading messages for $langcode:\n" );
65                         if ( !$this->generatePo( $langcode, $langTool->getMessages( $langcode ) ) ) {
66                                 $this->error( "ERROR: Failed to write file." );
67                         } else {
68                                 $this->output( "Applying template:" );
69                                 $this->applyPot( $langcode );
70                         }
71                 }
72         }
73
74         /**
75          * Return a dummy header for later edition.
76          *
77          * @return String: a dummy header
78          */
79         private function poHeader() {
80                 return '# SOME DESCRIPTIVE TITLE.
81 # Copyright (C) 2005 MediaWiki
82 # This file is distributed under the same license as the MediaWiki package.
83 # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
84 #
85 #, fuzzy
86 msgid ""
87 msgstr ""
88 "Project-Id-Version: PACKAGE VERSION\n"
89 "Report-Msgid-Bugs-To: bugzilllaaaaa\n"
90 "POT-Creation-Date: 2005-08-16 20:13+0200\n"
91 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
92 "Last-Translator: VARIOUS <nobody>\n"
93 "Language-Team: LANGUAGE <nobody>\n"
94 "MIME-Version: 1.0\n"
95 "Content-Type: text/plain; charset=UTF-8\n"
96 "Content-Transfer-Encoding: 8bit\n"
97 ';
98         }
99
100         /**
101          * generate and write a file in .po format.
102          *
103          * @param $langcode String: code of a language it will process.
104          * @param $messages Array containing the various messages.
105          * @return string Filename where stuff got saved or false.
106          */
107         private function generatePo( $langcode, $messages ) {
108                 $data = $this->poHeader();
109
110                 // Generate .po entries
111                 foreach ( $messages['all'] as $identifier => $content ) {
112                         $data .= "msgid \"$identifier\"\n";
113
114                         // Escape backslashes
115                         $tmp = str_replace( '\\', '\\\\', $content );
116                         // Escape doublelquotes
117                         $tmp = preg_replace( "/(?<!\\\\)\"/", '\"', $tmp );
118                         // Rewrite multilines to gettext format
119                         $tmp = str_replace( "\n", "\"\n\"", $tmp );
120
121                         $data .= 'msgstr "' . $tmp . "\"\n\n";
122                 }
123
124                 // Write the content to a file in locale/XX/messages.po
125                 $dir = LOCALE_OUTPUT_DIR . '/' . $langcode;
126                 if ( !is_dir( $dir ) ) { mkdir( $dir, 0770 ); }
127                 $filename = $dir . '/fromlanguagefile.po';
128         
129                 $file = fopen( $filename , 'wb' );
130                 if ( fwrite( $file, $data ) ) {
131                         fclose( $file );
132                         return $filename;
133                 } else {
134                         fclose( $file );
135                         return false;
136                 }
137         }
138
139         private function generatePot() {
140                 global $IP;
141                 $curdir = getcwd();
142                 chdir( $IP );
143                 exec( XGETTEXT_BIN
144                   . ' ' . XGETTEXT_OPTIONS
145                   . ' -o ' . LOCALE_OUTPUT_DIR . '/wfMsg.pot'
146                   . ' includes/*php'
147                   );
148                 chdir( $curdir );
149         }
150         
151         private function applyPot( $langcode ) {
152                 $langdir = LOCALE_OUTPUT_DIR . '/' . $langcode;
153         
154                 $from = $langdir . '/fromlanguagefile.po';
155                 $pot = LOCALE_OUTPUT_DIR . '/wfMsg.pot';
156                 $dest = $langdir . '/messages.po';
157         
158                 // Merge template and generate file to get final .po
159                 exec( MSGMERGE_BIN . MSGMERGE_OPTIONS . " $from $pot -o $dest " );
160                 // delete no more needed file
161                 //      unlink($from);
162         }
163 }
164
165 $maintClass = "Lang2Po";
166 require_once( RUN_MAINTENANCE_IF_MAIN );