Now we learn how to use nice urls and add localization to our project |
|
## Code |
|
First we create our hello class little bit differently: |
|
class hello: public application { |
public: |
hello(worker_thread &worker) : |
application(worker) |
{ |
url.add("^/(en|he)/?$", |
boost::bind(&hello::say_hello,this,_1)); |
use_template("view"); |
} |
void say_hello(string lang) |
{ |
set_lang(lang); |
data::message c; |
c.message=gettext("Hello World"); |
render("message",c); |
} |
}; |
|
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 functioon `say_hello` which receives first matched expression `(en|he)` as first parameter (using placeholder `_1`). |
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. |
|
## Template |
|
Now we change our template, and use following code: |
|
<html> |
<head> |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" > |
</head> |
<% if rtl %> |
<body dir="rtl"> |
<% else %> |
<body> |
<% end %> |
<h1><% gt "We want to say" %></h1> |
<p><% message %></p> |
</body> |
<html> |
|
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" %>` that becomes translated |
to the target language when template is rendered. |
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. |
|
## Translation |
|
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. |
And [translate](http://en.wikipedia.org/wiki/GNU_gettext#Translating) 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. |
|
When we complete translation we can create standard |
After complete translation, we can create standard |
gettext directory `locale/he/LC_MESSAGES` and put there |
our hello.mo. |
|
|
## 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](http://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](http://localhost:8080/hello/en) or [localhost:8080/hello/he](http://localhost:8080/hello/he) |
|
|