Reading strings from Wesnoth MO files

Discuss and coordinate development of mainline and user-made content translations.

Moderators: Forum Moderators, Developers

Post Reply
User avatar
Viliam
Translator
Posts: 1341
Joined: January 30th, 2004, 11:07 am
Location: Bratislava, Slovakia
Contact:

Reading strings from Wesnoth MO files

Post by Viliam »

I tried to make a script that could read translated strings directly from MO files installed with Wesnoth. It was a bit difficult, and my choice of PHP as a scripting language on MS Windows probably contributed to it. So I want to share the solution, if anyone would like to do the same thing.


1) Setting up PHP 5

I use PHP 5 on MS Windows. In "php.ini" file (located in "C:\Windows") I had to uncomment the line:

Code: Select all

extension=php_gettext.dll
But then PHP complained that is cannot find the DLL. Well, the EXE installer (which I used) does not include extensions. They must be downloaded from the "www.php.net" site in a ZIP distribution, and the file "php_gettext.dll" must be copied to the PHP 5 main directory.

Now, PHP 5 is ready to use gettext.


2) The script

Code: Select all

<?

bindtextdomain('wesnoth', 'C:\\Program Files\\Wesnoth\\po'); 
textdomain('wesnoth');

$languages = array(
	"af", "ca", "cs", "de", "en", "es", "fi", "fr", "hu", "it", "ja", "la", "nl", "pl", "ru", "sk", "sv", "tr"
);

echo "\xef\xbb\xbf\r\n";

for ($i = 0; $i < count($languages); $i++) {
	$lang = $languages[$i];

	putenv('LANG=' . $lang);
	echo "[" . $lang . "] " . gettext('Battle for Wesnoth') . "\r\n"; 

}

?>
and it is started by a BAT file:

Code: Select all

C:\php\php-cgi.exe -f test.php > out.txt
I decided to put output to file, because the texts are in UTF-8, so writing them to command line would not produce the desired effect. Notepad is sometimes able to autodetect UTF-8 encoding in TXT files, but mostly it fails. The bytes ef bb bf at the beginning are the byte order mark, which is always recognized.

So the result is text file containing translations of "Battle for Wesnoth" in many languages.
ettin
Lord of Glamdrol
Posts: 369
Joined: August 17th, 2003, 2:05 pm
Contact:

Re: Reading strings from Wesnoth MO files

Post by ettin »

Viliam wrote:I tried to make a script that could read translated strings directly from MO files installed with Wesnoth. It was a bit difficult, and my choice of PHP as a scripting language on MS Windows probably contributed to it. So I want to share the solution, if anyone would like to do the same thing.
Hmm, why not simply convert .mo files back to .po files? (See msgunfmt.)
User avatar
Viliam
Translator
Posts: 1341
Joined: January 30th, 2004, 11:07 am
Location: Bratislava, Slovakia
Contact:

Re: Reading strings from Wesnoth MO files

Post by Viliam »

ettin wrote:Hmm, why not simply convert .mo files back to .po files?
1) Too easy? :twisted:

2) You need some script (and maybe library) to read strings from PO files too. The resulting program will not be much easier.

But, I found that my solution does not work correctly. It returns strings in different encodings, e.g. it returns Hungarian accented letters in some 8-bit encoding, but Russian letters in UTF-8.
Post Reply