3 * Convert Language files to .po files !
6 * - generate .po header
10 * @ingroup MaintenanceLanguage
13 $optionsWithArgs[] = 'lang';
15 /** This is a command line script */
16 require_once(dirname(__FILE__
).'/../commandLine.inc');
17 require_once(dirname(__FILE__
).'/languages.inc');
19 define('ALL_LANGUAGES', true);
20 define('XGETTEXT_BIN', 'xgettext');
21 define('MSGMERGE_BIN', 'msgmerge');
23 // used to generate the .pot
24 define('XGETTEXT_OPTIONS', '-n --keyword=wfMsg --keyword=wfMsgForContent --keyword=wfMsgHtml --keyword=wfMsgWikiHtml ');
25 define('MSGMERGE_OPTIONS', ' -v ');
27 define('LOCALE_OUTPUT_DIR', $IP.'/locale');
30 if( isset($options['help']) ) { usage(); wfDie(); }
31 // default output is WikiText
32 if( !isset($options['lang']) ) { $options['lang'] = ALL_LANGUAGES
; }
36 Usage: php lang2po.php [--help] [--lang=<langcode>] [--stdout]
38 --lang: a lang code you want to generate a .po for (default: all languages).
45 * Return a dummy header for later edition.
46 * @return string A dummy header
50 '# SOME DESCRIPTIVE TITLE.
51 # Copyright (C) 2005 MediaWiki
52 # This file is distributed under the same license as the MediaWiki package.
53 # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
58 "Project-Id-Version: PACKAGE VERSION\n"
59 "Report-Msgid-Bugs-To: bugzilllaaaaa\n"
60 "POT-Creation-Date: 2005-08-16 20:13+0200\n"
61 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
62 "Last-Translator: VARIOUS <nobody>\n"
63 "Language-Team: LANGUAGE <nobody>\n"
65 "Content-Type: text/plain; charset=UTF-8\n"
66 "Content-Transfer-Encoding: 8bit\n"
71 * generate and write a file in .po format.
73 * @param string $langcode Code of a language it will process.
74 * @param array &$messages Array containing the various messages.
75 * @return string Filename where stuff got saved or false.
77 function generatePo($langcode, $messages) {
80 // Generate .po entries
81 foreach($messages['all'] as $identifier => $content) {
82 $data .= "msgid \"$identifier\"\n";
85 $tmp = str_replace('\\', '\\\\', $content);
86 // Escape doublelquotes
87 $tmp = preg_replace( "/(?<!\\\\)\"/", '\"', $tmp);
88 // Rewrite multilines to gettext format
89 $tmp = str_replace("\n", "\"\n\"", $tmp);
91 $data .= 'msgstr "'. $tmp . "\"\n\n";
94 // Write the content to a file in locale/XX/messages.po
95 $dir = LOCALE_OUTPUT_DIR
.'/'.$langcode;
96 if( !is_dir($dir) ) { mkdir( $dir, 0770 ); }
97 $filename = $dir.'/fromlanguagefile.po';
99 $file = fopen( $filename , 'wb' );
100 if( fwrite( $file, $data ) ) {
109 function generatePot() {
114 .' '.XGETTEXT_OPTIONS
115 .' -o '.LOCALE_OUTPUT_DIR
.'/wfMsg.pot'
121 function applyPot($langcode) {
122 $langdir = LOCALE_OUTPUT_DIR
.'/'.$langcode;
124 $from = $langdir.'/fromlanguagefile.po';
125 $pot = LOCALE_OUTPUT_DIR
.'/wfMsg.pot';
126 $dest = $langdir.'/messages.po';
128 // Merge template and generate file to get final .po
129 exec(MSGMERGE_BIN
.MSGMERGE_OPTIONS
." $from $pot -o $dest ");
130 // delete no more needed file
134 // Generate a template .pot based on source tree
135 echo "Getting 'gettext' default messages from sources:";
140 $langTool = new languages();
142 if( $options['lang'] === ALL_LANGUAGES
) {
143 $codes = $langTool->getLanguages();
145 $codes = array( $options['lang'] );
149 foreach ( $codes as $langcode) {
150 echo "Loading messages for $langcode:\n";
151 if( ! generatePo($langcode, $langTool->getMessages($langcode) ) ) {
152 echo "ERROR: Failed to write file.\n";
154 echo "Applying template:";