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