3 * Convert Language files to .po files !
6 * - generate .po header
10 /** This is a command line script */
11 require_once('commandLine.inc');
12 require_once('languages.inc');
14 define('ALL_LANGUAGES', true);
15 define('XGETTEXT_BIN', 'xgettext');
16 define('MSGMERGE_BIN', 'msgmerge');
18 // used to generate the .pot
19 define('XGETTEXT_OPTIONS', '-n --keyword=wfMsg --keyword=wfMsgForContent --keyword=wfMsgHtml --keyword=wfMsgWikiHtml ');
20 define('MSGMERGE_OPTIONS', ' -v ');
22 define('LOCALE_OUTPUT_DIR', $IP.'/locale');
25 if( isset($options['help']) ) { usage(); wfDie(); }
26 // default output is WikiText
27 if( !isset($options['lang']) ) { $options['lang'] = ALL_LANGUAGES
; }
31 Usage: php lang2po.php [--help] [--lang=<langcode>] [--stdout]
33 --lang: a lang code you want to generate a .po for (default: all languages).
40 * Return a dummy header for later edition.
41 * @return string A dummy header
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.
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"
60 "Content-Type: text/plain; charset=UTF-8\n"
61 "Content-Transfer-Encoding: 8bit\n"
66 * generate and write a file in .po format.
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.
72 function generatePo($langcode, &$messages) {
75 // Generate .po entries
76 foreach($messages as $identifier => $content) {
77 $data .= "msgid \"$identifier\"\n";
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);
86 $data .= 'msgstr "'. $tmp . "\"\n\n";
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';
94 $file = fopen( $filename , 'wb' );
95 if( fwrite( $file, $data ) ) {
104 function generatePot() {
109 .' '.XGETTEXT_OPTIONS
110 .' -o '.LOCALE_OUTPUT_DIR
.'/wfMsg.pot'
116 function applyPot($langcode) {
117 $langdir = LOCALE_OUTPUT_DIR
.'/'.$langcode;
119 $from = $langdir.'/fromlanguagefile.po';
120 $pot = LOCALE_OUTPUT_DIR
.'/wfMsg.pot';
121 $dest = $langdir.'/messages.po';
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
129 // Generate a template .pot based on source tree
130 echo "Getting 'gettext' default messages from sources:";
135 $langTool = new languages();
138 foreach ( $langTool->getList() as $langcode) {
139 echo "Loading messages for $langcode:\t";
140 require_once( 'languages/Language' . $langcode . '.php' );
141 $arr = 'wgAllMessages'.$langcode;
142 if(!@is_array
($
$arr)) {
146 if( ! generatePo($langcode, $
$arr) ) {
147 echo "ERROR: Failed to wrote file.\n";
149 echo "Applying template:";