3 * Convert Language files to .po files !
6 * - generate .po header
10 $optionsWithArgs[] = 'lang';
12 /** This is a command line script */
13 require_once(dirname(__FILE__
).'/../commandLine.inc');
14 require_once(dirname(__FILE__
).'/languages.inc');
16 define('ALL_LANGUAGES', true);
17 define('XGETTEXT_BIN', 'xgettext');
18 define('MSGMERGE_BIN', 'msgmerge');
20 // used to generate the .pot
21 define('XGETTEXT_OPTIONS', '-n --keyword=wfMsg --keyword=wfMsgForContent --keyword=wfMsgHtml --keyword=wfMsgWikiHtml ');
22 define('MSGMERGE_OPTIONS', ' -v ');
24 define('LOCALE_OUTPUT_DIR', $IP.'/locale');
27 if( isset($options['help']) ) { usage(); wfDie(); }
28 // default output is WikiText
29 if( !isset($options['lang']) ) { $options['lang'] = ALL_LANGUAGES
; }
33 Usage: php lang2po.php [--help] [--lang=<langcode>] [--stdout]
35 --lang: a lang code you want to generate a .po for (default: all languages).
42 * Return a dummy header for later edition.
43 * @return string A dummy header
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.
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"
62 "Content-Type: text/plain; charset=UTF-8\n"
63 "Content-Transfer-Encoding: 8bit\n"
68 * generate and write a file in .po format.
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.
74 function generatePo($langcode, $messages) {
77 // Generate .po entries
78 foreach($messages['all'] as $identifier => $content) {
79 $data .= "msgid \"$identifier\"\n";
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);
88 $data .= 'msgstr "'. $tmp . "\"\n\n";
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';
96 $file = fopen( $filename , 'wb' );
97 if( fwrite( $file, $data ) ) {
106 function generatePot() {
111 .' '.XGETTEXT_OPTIONS
112 .' -o '.LOCALE_OUTPUT_DIR
.'/wfMsg.pot'
118 function applyPot($langcode) {
119 $langdir = LOCALE_OUTPUT_DIR
.'/'.$langcode;
121 $from = $langdir.'/fromlanguagefile.po';
122 $pot = LOCALE_OUTPUT_DIR
.'/wfMsg.pot';
123 $dest = $langdir.'/messages.po';
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
131 // Generate a template .pot based on source tree
132 echo "Getting 'gettext' default messages from sources:";
137 $langTool = new languages();
139 if( $options['lang'] === ALL_LANGUAGES
) {
140 $codes = $langTool->getLanguages();
142 $codes = array( $options['lang'] );
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";
151 echo "Applying template:";