MediaWiki
REL1_19
|
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 // used to generate the .pot 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 // Generate a template .pot based on source tree 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 // Do all languages 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 00079 private function poHeader() { 00080 return '# SOME DESCRIPTIVE TITLE. 00081 # Copyright (C) 2005 MediaWiki 00082 # This file is distributed under the same license as the MediaWiki package. 00083 # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. 00084 # 00085 #, fuzzy 00086 msgid "" 00087 msgstr "" 00088 "Project-Id-Version: PACKAGE VERSION\n" 00089 "Report-Msgid-Bugs-To: bugzilllaaaaa\n" 00090 "POT-Creation-Date: 2005-08-16 20:13+0200\n" 00091 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" 00092 "Last-Translator: VARIOUS <nobody>\n" 00093 "Language-Team: LANGUAGE <nobody>\n" 00094 "MIME-Version: 1.0\n" 00095 "Content-Type: text/plain; charset=UTF-8\n" 00096 "Content-Transfer-Encoding: 8bit\n" 00097 '; 00098 } 00099 00107 private function generatePo( $langcode, $messages ) { 00108 $data = $this->poHeader(); 00109 00110 // Generate .po entries 00111 foreach ( $messages['all'] as $identifier => $content ) { 00112 $data .= "msgid \"$identifier\"\n"; 00113 00114 // Escape backslashes 00115 $tmp = str_replace( '\\', '\\\\', $content ); 00116 // Escape doublelquotes 00117 $tmp = preg_replace( "/(?<!\\\\)\"/", '\"', $tmp ); 00118 // Rewrite multilines to gettext format 00119 $tmp = str_replace( "\n", "\"\n\"", $tmp ); 00120 00121 $data .= 'msgstr "' . $tmp . "\"\n\n"; 00122 } 00123 00124 // Write the content to a file in locale/XX/messages.po 00125 $dir = LOCALE_OUTPUT_DIR . '/' . $langcode; 00126 if ( !is_dir( $dir ) ) { mkdir( $dir, 0770 ); } 00127 $filename = $dir . '/fromlanguagefile.po'; 00128 00129 $file = fopen( $filename , 'wb' ); 00130 if ( fwrite( $file, $data ) ) { 00131 fclose( $file ); 00132 return $filename; 00133 } else { 00134 fclose( $file ); 00135 return false; 00136 } 00137 } 00138 00139 private function generatePot() { 00140 global $IP; 00141 $curdir = getcwd(); 00142 chdir( $IP ); 00143 exec( XGETTEXT_BIN 00144 . ' ' . XGETTEXT_OPTIONS 00145 . ' -o ' . LOCALE_OUTPUT_DIR . '/wfMsg.pot' 00146 . ' includes/*php' 00147 ); 00148 chdir( $curdir ); 00149 } 00150 00151 private function applyPot( $langcode ) { 00152 $langdir = LOCALE_OUTPUT_DIR . '/' . $langcode; 00153 00154 $from = $langdir . '/fromlanguagefile.po'; 00155 $pot = LOCALE_OUTPUT_DIR . '/wfMsg.pot'; 00156 $dest = $langdir . '/messages.po'; 00157 00158 // Merge template and generate file to get final .po 00159 exec( MSGMERGE_BIN . MSGMERGE_OPTIONS . " $from $pot -o $dest " ); 00160 // delete no more needed file 00161 // unlink($from); 00162 } 00163 } 00164 00165 $maintClass = "Lang2Po"; 00166 require_once( RUN_MAINTENANCE_IF_MAIN );