Revert r49880, r49883, r49885 - add uniwiki/CreatePage extension to core
[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 * @file
10 * @ingroup MaintenanceLanguage
11 */
12
13 $optionsWithArgs[] = 'lang';
14
15 /** This is a command line script */
16 require_once(dirname(__FILE__).'/../commandLine.inc');
17 require_once(dirname(__FILE__).'/languages.inc');
18
19 define('ALL_LANGUAGES', true);
20 define('XGETTEXT_BIN', 'xgettext');
21 define('MSGMERGE_BIN', 'msgmerge');
22
23 // used to generate the .pot
24 define('XGETTEXT_OPTIONS', '-n --keyword=wfMsg --keyword=wfMsgForContent --keyword=wfMsgHtml --keyword=wfMsgWikiHtml ');
25 define('MSGMERGE_OPTIONS', ' -v ');
26
27 define('LOCALE_OUTPUT_DIR', $IP.'/locale');
28
29
30 if( isset($options['help']) ) { usage(); wfDie(); }
31 // default output is WikiText
32 if( !isset($options['lang']) ) { $options['lang'] = ALL_LANGUAGES; }
33
34 function usage() {
35 print <<<END
36 Usage: php lang2po.php [--help] [--lang=<langcode>] [--stdout]
37 --help: this message.
38 --lang: a lang code you want to generate a .po for (default: all languages).
39
40 END;
41 }
42
43
44 /**
45 * Return a dummy header for later edition.
46 * @return string A dummy header
47 */
48 function poHeader() {
49 return
50 '# SOME DESCRIPTIVE TITLE.
51 # Copyright (C) 2005 MediaWiki
52 # This file is distributed under the same license as the MediaWiki package.
53 # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
54 #
55 #, fuzzy
56 msgid ""
57 msgstr ""
58 "Project-Id-Version: PACKAGE VERSION\n"
59 "Report-Msgid-Bugs-To: bugzilllaaaaa\n"
60 "POT-Creation-Date: 2005-08-16 20:13+0200\n"
61 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
62 "Last-Translator: VARIOUS <nobody>\n"
63 "Language-Team: LANGUAGE <nobody>\n"
64 "MIME-Version: 1.0\n"
65 "Content-Type: text/plain; charset=UTF-8\n"
66 "Content-Transfer-Encoding: 8bit\n"
67 ';
68 }
69
70 /**
71 * generate and write a file in .po format.
72 *
73 * @param string $langcode Code of a language it will process.
74 * @param array &$messages Array containing the various messages.
75 * @return string Filename where stuff got saved or false.
76 */
77 function generatePo($langcode, $messages) {
78 $data = poHeader();
79
80 // Generate .po entries
81 foreach($messages['all'] as $identifier => $content) {
82 $data .= "msgid \"$identifier\"\n";
83
84 // Escape backslashes
85 $tmp = str_replace('\\', '\\\\', $content);
86 // Escape doublelquotes
87 $tmp = preg_replace( "/(?<!\\\\)\"/", '\"', $tmp);
88 // Rewrite multilines to gettext format
89 $tmp = str_replace("\n", "\"\n\"", $tmp);
90
91 $data .= 'msgstr "'. $tmp . "\"\n\n";
92 }
93
94 // Write the content to a file in locale/XX/messages.po
95 $dir = LOCALE_OUTPUT_DIR.'/'.$langcode;
96 if( !is_dir($dir) ) { mkdir( $dir, 0770 ); }
97 $filename = $dir.'/fromlanguagefile.po';
98
99 $file = fopen( $filename , 'wb' );
100 if( fwrite( $file, $data ) ) {
101 fclose( $file );
102 return $filename;
103 } else {
104 fclose( $file );
105 return false;
106 }
107 }
108
109 function generatePot() {
110 global $IP;
111 $curdir = getcwd();
112 chdir($IP);
113 exec( XGETTEXT_BIN
114 .' '.XGETTEXT_OPTIONS
115 .' -o '.LOCALE_OUTPUT_DIR.'/wfMsg.pot'
116 .' includes/*php'
117 );
118 chdir($curdir);
119 }
120
121 function applyPot($langcode) {
122 $langdir = LOCALE_OUTPUT_DIR.'/'.$langcode;
123
124 $from = $langdir.'/fromlanguagefile.po';
125 $pot = LOCALE_OUTPUT_DIR.'/wfMsg.pot';
126 $dest = $langdir.'/messages.po';
127
128 // Merge template and generate file to get final .po
129 exec(MSGMERGE_BIN.MSGMERGE_OPTIONS." $from $pot -o $dest ");
130 // delete no more needed file
131 // unlink($from);
132 }
133
134 // Generate a template .pot based on source tree
135 echo "Getting 'gettext' default messages from sources:";
136 generatePot();
137 echo "done.\n";
138
139
140 $langTool = new languages();
141
142 if( $options['lang'] === ALL_LANGUAGES ) {
143 $codes = $langTool->getLanguages();
144 } else {
145 $codes = array( $options['lang'] );
146 }
147
148 // Do all languages
149 foreach ( $codes as $langcode) {
150 echo "Loading messages for $langcode:\n";
151 if( ! generatePo($langcode, $langTool->getMessages($langcode) ) ) {
152 echo "ERROR: Failed to write file.\n";
153 } else {
154 echo "Applying template:";
155 applyPot($langcode);
156 }
157 }
158