Remove "non-message checks" from the header, like for the body to avoid rows in the...
[lhc/web/wiklou.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 * @return string A dummy header
77 */
78 private function poHeader() {
79 return '# SOME DESCRIPTIVE TITLE.
80 # Copyright (C) 2005 MediaWiki
81 # This file is distributed under the same license as the MediaWiki package.
82 # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
83 #
84 #, fuzzy
85 msgid ""
86 msgstr ""
87 "Project-Id-Version: PACKAGE VERSION\n"
88 "Report-Msgid-Bugs-To: bugzilllaaaaa\n"
89 "POT-Creation-Date: 2005-08-16 20:13+0200\n"
90 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
91 "Last-Translator: VARIOUS <nobody>\n"
92 "Language-Team: LANGUAGE <nobody>\n"
93 "MIME-Version: 1.0\n"
94 "Content-Type: text/plain; charset=UTF-8\n"
95 "Content-Transfer-Encoding: 8bit\n"
96 ';
97 }
98
99 /**
100 * generate and write a file in .po format.
101 *
102 * @param string $langcode Code of a language it will process.
103 * @param array &$messages Array containing the various messages.
104 * @return string Filename where stuff got saved or false.
105 */
106 private function generatePo($langcode, $messages) {
107 $data = $this->poHeader();
108
109 // Generate .po entries
110 foreach( $messages['all'] as $identifier => $content ) {
111 $data .= "msgid \"$identifier\"\n";
112
113 // Escape backslashes
114 $tmp = str_replace('\\', '\\\\', $content);
115 // Escape doublelquotes
116 $tmp = preg_replace( "/(?<!\\\\)\"/", '\"', $tmp);
117 // Rewrite multilines to gettext format
118 $tmp = str_replace("\n", "\"\n\"", $tmp);
119
120 $data .= 'msgstr "'. $tmp . "\"\n\n";
121 }
122
123 // Write the content to a file in locale/XX/messages.po
124 $dir = LOCALE_OUTPUT_DIR.'/'.$langcode;
125 if( !is_dir($dir) ) { mkdir( $dir, 0770 ); }
126 $filename = $dir.'/fromlanguagefile.po';
127
128 $file = fopen( $filename , 'wb' );
129 if( fwrite( $file, $data ) ) {
130 fclose( $file );
131 return $filename;
132 } else {
133 fclose( $file );
134 return false;
135 }
136 }
137
138 private function generatePot() {
139 global $IP;
140 $curdir = getcwd();
141 chdir($IP);
142 exec( XGETTEXT_BIN
143 .' '.XGETTEXT_OPTIONS
144 .' -o '.LOCALE_OUTPUT_DIR.'/wfMsg.pot'
145 .' includes/*php'
146 );
147 chdir($curdir);
148 }
149
150 private function applyPot($langcode) {
151 $langdir = LOCALE_OUTPUT_DIR.'/'.$langcode;
152
153 $from = $langdir.'/fromlanguagefile.po';
154 $pot = LOCALE_OUTPUT_DIR.'/wfMsg.pot';
155 $dest = $langdir.'/messages.po';
156
157 // Merge template and generate file to get final .po
158 exec(MSGMERGE_BIN.MSGMERGE_OPTIONS." $from $pot -o $dest ");
159 // delete no more needed file
160 // unlink($from);
161 }
162 }
163
164 $maintClass = "Lang2Po";
165 require_once( DO_MAINTENANCE );