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