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 define('XGETTEXT_OPTIONS', '-n --keyword=wfMsg ');
20 define('LOCALE_OUTPUT_DIR', $IP.'/locale');
23 if( isset($options['help']) ) { usage(); die(); }
24 // default output is WikiText
25 if( !isset($options['lang']) ) { $options['lang'] = ALL_LANGUAGES
; }
29 Usage: php lang2po.php [--help] [--lang=<langcode>] [--stdout]
31 --lang: a lang code you want to generate a .po for (default: all languages).
38 * Return a dummy header for later edition.
39 * @return string A dummy header
43 '# SOME DESCRIPTIVE TITLE.
44 # Copyright (C) 2005 MediaWiki
45 # This file is distributed under the same license as the MediaWiki package.
46 # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
51 "Project-Id-Version: PACKAGE VERSION\n"
52 "Report-Msgid-Bugs-To: bugzilllaaaaa\n"
53 "POT-Creation-Date: 2005-08-16 20:13+0200\n"
54 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
55 "Last-Translator: VARIOUS <nobody>\n"
56 "Language-Team: LANGUAGE <nobody>\n"
58 "Content-Type: text/plain; charset=UTF-8\n"
59 "Content-Transfer-Encoding: 8bit\n"
64 * generate and write a file in .po format.
66 * @param string $langcode Code of a language it will process.
67 * @param array &$messages Array containing the various messages.
68 * @return string Filename where stuff got saved or false.
70 function generatePo($langcode, &$messages) {
73 // Generate .po entries
74 foreach($messages as $identifier => $content) {
75 $data .= "msgid \"$identifier\"\n";
78 $tmp = str_replace('\\', '\\\\', $content);
79 // Escape doublelquotes
80 $tmp = preg_replace( "/(?<!\\\\)\"/", '\"', $tmp);
81 // Rewrite multilines to gettext format
82 $tmp = str_replace("\n", "\"\n\"", $tmp);
84 $data .= 'msgstr "'. $tmp . "\"\n\n";
87 // Write the content to a file in locale/XX/messages.po
88 $dir = LOCALE_OUTPUT_DIR
.'/'.$langcode;
89 if( !is_dir($dir) ) { mkdir( $dir, 0770 ); }
90 $filename = $dir.'/fromlanguagefile.po';
92 $file = fopen( $filename , 'wb' );
93 if( fwrite( $file, $data ) ) {
102 function applyPot($langcode) {
103 $langdir = LOCALE_OUTPUT_DIR
.'/'.$langcode;
105 $from = $langdir.'/fromlanguagefile.po';
106 $pot = LOCALE_OUTPUT_DIR
.'/wfMsg.pot';
107 $dest = $langdir.'/messages.po';
109 // Merge template and generate file to get final .po
110 exec(MSGMERGE_BIN
." $from $pot -o $dest ");
111 // delete no more needed file
115 // Generate a template .pot based on source tree
116 echo "Getting 'gettext' default messages from sources:";
118 .' '.XGETTEXT_OPTIONS
119 .' -o '.$IP.'/locale/wfMsg.pot'
120 .' '.$IP.'/includes/*php'
125 $langTool = new languages();
128 foreach ( $langTool->getList() as $langcode) {
129 echo "Loading messages for $langcode:\t";
130 require_once( 'languages/Language' . $langcode . '.php' );
131 $arr = 'wgAllMessages'.$langcode;
132 if(!@is_array
($
$arr)) {
136 if( ! generatePo($langcode, $
$arr) ) {
137 echo "ERROR: Failed to wrote file.\n";
139 echo "Applying template:";