00001 <?php
00013 $optionsWithArgs[] = 'lang';
00014
00016 require_once(dirname(__FILE__).'/../commandLine.inc');
00017 require_once(dirname(__FILE__).'/languages.inc');
00018
00019 define('ALL_LANGUAGES', true);
00020 define('XGETTEXT_BIN', 'xgettext');
00021 define('MSGMERGE_BIN', 'msgmerge');
00022
00023
00024 define('XGETTEXT_OPTIONS', '-n --keyword=wfMsg --keyword=wfMsgForContent --keyword=wfMsgHtml --keyword=wfMsgWikiHtml ');
00025 define('MSGMERGE_OPTIONS', ' -v ');
00026
00027 define('LOCALE_OUTPUT_DIR', $IP.'/locale');
00028
00029
00030 if( isset($options['help']) ) { usage(); wfDie(); }
00031
00032 if( !isset($options['lang']) ) { $options['lang'] = ALL_LANGUAGES; }
00033
00034 function usage() {
00035 print <<<END
00036 Usage: php lang2po.php [--help] [--lang=<langcode>] [--stdout]
00037 --help: this message.
00038 --lang: a lang code you want to generate a .po for (default: all languages).
00039
00040 END;
00041 }
00042
00043
00048 function poHeader() {
00049 return
00050 '# SOME DESCRIPTIVE TITLE.
00051 # Copyright (C) 2005 MediaWiki
00052 # This file is distributed under the same license as the MediaWiki package.
00053 # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
00054 #
00055 #, fuzzy
00056 msgid ""
00057 msgstr ""
00058 "Project-Id-Version: PACKAGE VERSION\n"
00059 "Report-Msgid-Bugs-To: bugzilllaaaaa\n"
00060 "POT-Creation-Date: 2005-08-16 20:13+0200\n"
00061 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
00062 "Last-Translator: VARIOUS <nobody>\n"
00063 "Language-Team: LANGUAGE <nobody>\n"
00064 "MIME-Version: 1.0\n"
00065 "Content-Type: text/plain; charset=UTF-8\n"
00066 "Content-Transfer-Encoding: 8bit\n"
00067 ';
00068 }
00069
00077 function generatePo($langcode, $messages) {
00078 $data = poHeader();
00079
00080
00081 foreach($messages['all'] as $identifier => $content) {
00082 $data .= "msgid \"$identifier\"\n";
00083
00084
00085 $tmp = str_replace('\\', '\\\\', $content);
00086
00087 $tmp = preg_replace( "/(?<!\\\\)\"/", '\"', $tmp);
00088
00089 $tmp = str_replace("\n", "\"\n\"", $tmp);
00090
00091 $data .= 'msgstr "'. $tmp . "\"\n\n";
00092 }
00093
00094
00095 $dir = LOCALE_OUTPUT_DIR.'/'.$langcode;
00096 if( !is_dir($dir) ) { mkdir( $dir, 0770 ); }
00097 $filename = $dir.'/fromlanguagefile.po';
00098
00099 $file = fopen( $filename , 'wb' );
00100 if( fwrite( $file, $data ) ) {
00101 fclose( $file );
00102 return $filename;
00103 } else {
00104 fclose( $file );
00105 return false;
00106 }
00107 }
00108
00109 function generatePot() {
00110 global $IP;
00111 $curdir = getcwd();
00112 chdir($IP);
00113 exec( XGETTEXT_BIN
00114 .' '.XGETTEXT_OPTIONS
00115 .' -o '.LOCALE_OUTPUT_DIR.'/wfMsg.pot'
00116 .' includes/*php'
00117 );
00118 chdir($curdir);
00119 }
00120
00121 function applyPot($langcode) {
00122 $langdir = LOCALE_OUTPUT_DIR.'/'.$langcode;
00123
00124 $from = $langdir.'/fromlanguagefile.po';
00125 $pot = LOCALE_OUTPUT_DIR.'/wfMsg.pot';
00126 $dest = $langdir.'/messages.po';
00127
00128
00129 exec(MSGMERGE_BIN.MSGMERGE_OPTIONS." $from $pot -o $dest ");
00130
00131
00132 }
00133
00134
00135 echo "Getting 'gettext' default messages from sources:";
00136 generatePot();
00137 echo "done.\n";
00138
00139
00140 $langTool = new languages();
00141
00142 if( $options['lang'] === ALL_LANGUAGES ) {
00143 $codes = $langTool->getLanguages();
00144 } else {
00145 $codes = array( $options['lang'] );
00146 }
00147
00148
00149 foreach ( $codes as $langcode) {
00150 echo "Loading messages for $langcode:\n";
00151 if( ! generatePo($langcode, $langTool->getMessages($langcode) ) ) {
00152 echo "ERROR: Failed to write file.\n";
00153 } else {
00154 echo "Applying template:";
00155 applyPot($langcode);
00156 }
00157 }
00158