Localisation updates for core messages from translatewiki.net
[mediawiki.git] / maintenance / language / lang2po.php
blob1009ed6c780837ff94770cb7bff070da7c18f003
1 <?php
2 /**
3 * Convert Language files to .po files !
5 * Todo:
6 * - generate .po header
7 * - fix escaping of \
9 * @file
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; }
34 function usage() {
35 print <<<END
36 Usage: php lang2po.php [--help] [--lang=<langcode>] [--stdout]
37 --help: this message.
38 --lang: a lang code you want to generate a .po for (default: all languages).
40 END;
44 /**
45 * Return a dummy header for later edition.
46 * @return string A dummy header
48 function poHeader() {
49 return
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.
55 #, fuzzy
56 msgid ""
57 msgstr ""
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"
64 "MIME-Version: 1.0\n"
65 "Content-Type: text/plain; charset=UTF-8\n"
66 "Content-Transfer-Encoding: 8bit\n"
70 /**
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) {
78 $data = poHeader();
80 // Generate .po entries
81 foreach($messages['all'] as $identifier => $content) {
82 $data .= "msgid \"$identifier\"\n";
84 // Escape backslashes
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 ) ) {
101 fclose( $file );
102 return $filename;
103 } else {
104 fclose( $file );
105 return false;
109 function generatePot() {
110 global $IP;
111 $curdir = getcwd();
112 chdir($IP);
113 exec( XGETTEXT_BIN
114 .' '.XGETTEXT_OPTIONS
115 .' -o '.LOCALE_OUTPUT_DIR.'/wfMsg.pot'
116 .' includes/*php'
118 chdir($curdir);
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
131 // unlink($from);
134 // Generate a template .pot based on source tree
135 echo "Getting 'gettext' default messages from sources:";
136 generatePot();
137 echo "done.\n";
140 $langTool = new languages();
142 if( $options['lang'] === ALL_LANGUAGES ) {
143 $codes = $langTool->getLanguages();
144 } else {
145 $codes = array( $options['lang'] );
148 // Do all languages
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";
153 } else {
154 echo "Applying template:";
155 applyPot($langcode);