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