Main  /  Edit  /  History  /   /  Users Area

Tutorial: Basic Localization and Nice Urls

Now we learn how to use nice urls and add localization to our project


First we create our hello class little bit differently:

class hello: public application {
    hello(worker_thread &worker) :
    void say_hello(string lang)
        data::message c;
        c.message=gettext("Hello World");

Instead of overloading main() function, we use a url member of application class and add dispatchers according to urls we need.

We bind to url that match regular expression: ^/(en|he)/?$ member function say_hello which receives first matched expression (en|he) as first parameter (using placeholder _1).

Then we implement say_hello(string) member function.

First of all, we define our locale by calling set_lang(lang). and then set value of c.message using gettext().

Note: CppCMS gettext implementation is thread safe, thus you can set different languages in different threads.


Now we change our template, and use following code:

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
    <% if rtl %>
    <body dir="rtl">
    <% else %>
    <% end %>
    <h1><% gt "We want to say" %></h1>
    <p><% message %></p>

First, we added utf-8 charset and then we have two important localization functions:

  1. <% if rtl %> is condition that checks if language is written right-to-left, like Hebrew or Arabic.
  2. Instead of writing directly "We want to say" we use command <% gt "We want to say" %>, defined that quoted text becomes translated to the target language when template is rendered.


Now when we build template code we add gettext domain name for our application using -d hello:

cppcms_tmpl_cc -d hello view.tmpl -o view.cpp

Now when C++ source code is build we can extract messages from them calling

xgettext view.cpp hello.cpp

And translate them.

There is an important part in messages.po:

msgid "LTR"
msgstr ""

It should be translated as RTL for languages like Hebrew or Arabic -- right-to-left languages.

After complete translation, we can create standard gettext directory locale/he/LC_MESSAGES and put there our

Configuration and Running

In order to enable gettext support in our application we should add following lines to our config.txt

locale.dir = "./locale"
locale.lang_list = { "he" "en" }
locale.domain_list = { "hello" }

Specifying location of "locale" directory, the list of supported languages and list of supported domains.

Now we can run our application

cppcms_run hello.fcgi -c config.txt

We can go to localhost:8080/hello.fcgi/en and see our "Hello World".

We can make urls nicer by adding -s parameter --- the script path.

cppcms_run -s /hello hello.fcgi -c config.txt

And now we can visit localhost:8080/hello/en or localhost:8080/hello/he


CppCMS is a web development framework for performance demanding applications.

Support This Project Logo

Поддержать проект

CppCMS needs You


Main Page

Valid CSS | Valid XHTML 1.0