Sunday, December 23, 2007

A Tutorial on GNU gettext

“Hello world” example – hello.c

#include <stdio.h>
#include <locale.h>
#include <libintl.h>
int main()
{
setlocale(LC_ALL, "");
textdomain("hello");
bindtextdomain("hello", "locale");
printf(gettext("Hello, world !\n"));
return 0;
}

Compiling

gcc hello.c –c hello

Extracting translatable strings

xgettext -o hello.po hello.c

Translating

We will try to make a Chinese (zh_TW.UTF-8) translation

$ cat hello.po

# SOME DESCRIPTIVE TITLE.

# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER

# This file is distributed under the same license as the PACKAGE package.

# FIRST AUTHOR , YEAR.

#

#, fuzzy

msgid ""

msgstr ""

"Project-Id-Version: PACKAGE VERSION\n"

"Report-Msgid-Bugs-To: \n"

"POT-Creation-Date: 2007-12-23 13:10+0800\n"

"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"

"Last-Translator: FULL NAME \n"

"Language-Team: LANGUAGE \n"

"MIME-Version: 1.0\n"

"Content-Type: text/plain; charset=CHARSET\n"

"Content-Transfer-Encoding: 8bit\n"

#: hello.cpp:19

#, c-format

msgid "Hello, world!\n"

msgstr ""

modifies hello.po as following

# SOME DESCRIPTIVE TITLE.

# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER

# This file is distributed under the same license as the PACKAGE package.

# FIRST AUTHOR , YEAR.

#

#, fuzzy

msgid ""

msgstr ""

"Project-Id-Version: 1.0\n"

"Report-Msgid-Bugs-To: \n"

"POT-Creation-Date: 2007-12-23 13:10+0800\n"

"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"

"Last-Translator: FULL NAME \n"

"Language-Team: LANGUAGE \n"

"MIME-Version: 1.0\n"

"Content-Type: text/plain; charset=UTF-8\n"

"Content-Transfer-Encoding: 8bit\n"

#: hello.cpp:19

#, c-format

msgid "Hello, world!\n"

msgstr "大家好!\n"

builds the MO file

mkdir -p locale/zh_TW/LC_MESSAGES

msgfmt hello.po -o locale/zh_TW/LC_MESSAGES/hello.mo

Locale Environment variable

$ ./hello

Hello, world!

$ export LANG=zh_TW.UTF-8

$ ./hello

大家好!

Bibliography

1. GNU gettext

http://www.gnu.org/software/gettext/

2. A tutorial on Native Language Support using GNU gettext

http://oriya.sarovar.org/docs/gettext/

3. Gettext

http://www.haypocalc.com/wiki/Gettext

Saturday, December 22, 2007

Cygwin: list filename in chinese

edit ~/.bashrc to allow ls command to display filename in chinese
alias ls='ls -hF --show-control-chars --color=tty'

Tuesday, December 11, 2007

Using mod_python apache module on Windows

Installation (Python 2.5.1 + mod_python 3.3.1 + Apache 2.2.6)

1. Install Python 2.5.1

2. Install Apache 2.2.6

We choose to install the Apache server as a system service.

At the end of installation process, the installer will create default configuration files, the firewall software may block that action, we’d better to shutdown the firewall application before installing Apache

3. Install mod_python 3.3.1

At the end of installation process, the installer will ask you the Apache directory, then it will install mod_python.so to the APACHE_ROOT/modules directory

Configuring & Testing

We will use the default document root directory (APACHE_ROOT/htdocs)

1. Following the direction of mod_python manual, add following line to Apache configuration file (APACHE_ROOT/conf/httpd.conf)

LoadModule python_module libexec/mod_python.so

2. Add the following Apache directives,

AddHandler mod_python .py

PythonHandler mptest

PythonDebug On

3. This redirects all requests for URLs ending in .py to the mod_python handler

4. Restart Apache in order for the changes to take effect

Troubleshooting: Failed to restart apache server

Use Event Viewr to find out the problem:

In System Event say:

The Apache2.2 service terminated with service-specific error 1 (0x1).

In Application Event say:

Apache/conf/httpd.conf: Cannot load

Apache/modules/mod_python.so into server: The specified module could not be found.

From the python interpreter, we can import mod_python successfully, why? mod_python.so use python25.dll, and our user path variable already contain the Python directory; We start the Apache server as a system service, it will use the system path variable to find the import dll files; So we should add the Python directory to the system PATH variable

Edit mptest.py file in the APACHE_ROOT/htdocs directory so that is has the following lines

from mod_python import apache

def handler(req):

req.content_type = 'text/plain'

req.write("Hello World!")

return apache.OK

5. Point your browser to the URL referring to the mptest.py(for example, http://localhost/mptest.py); you should see "Hello World!".

Troubleshooting: No “Hello World!”

From APACHE_ROOT/log/error.log say:

[error] make_obcallback: could not import mod_python.apache.\n

[error] make_obcallback: Python path being used "['C:\\\\Python\\\\python25.zip', '.\\\\DLLs', '.\\\\lib', '.\\\\lib\\\\plat-win', '.\\\\lib\\\\lib-tk', 'C:\\\\Apache\\\\bin']".

[error] get_interpreter: no interpreter callback found.

[error] [client 127.0.0.1] python_handler: Can't get/create interpreter.

Apache server cann’t import mod_python.apache, we should add a PYTHONPATH system variable to contain the python modules path, for example, we can set it as following

C:\Python;C:\Python\DLLs;C:\Python\Lib;

C:\Python\Lib\lib-tk;C:\Python\Lib\site-packages

Thursday, August 9, 2007

"Ctrl+." Problem of SlickEdit 12.0.2

SlickEdit is one of the best programmer's editors, current version is v12.0.2.

There is an annoying bug of v12.0.2 while programming in C++ language
  • Some times, press "Ctrl+." (Go to Definition Of ...) on a symbol, only the corresponding header files are shown, but not the C++ source file with the function definition; this was casused by "using namespace ...;" in *.cpp/*.c source files
Solution:
  • define a using macro, set that macro in Tools > Options > Tagging Options > C Preprocessing

Thursday, April 12, 2007

pleas wait...