Implementing dual language (multilingual / multi language) feature in CodeIgniter based PyroCMS?
Semicolon Developers are in trouble. All clients we have now are requesting websites in Nepali and English language. CodeIgniter and most of all CodeIgniter based PyroCMS has been our choice of development tool since a long time. Check other posts on echo and you’ll know what I am talking about.
PyroCMS or CI as is are designed to support multilingual feature. But we have yet to figure out what is the best and effective way to implement this.
For now, given the urgency of feature request of our client and our commitment to project deadline we did a little hack for supporting two language in PyroCMS as follows:
General UI and widget blocks or different “titles” are easy to translate by using the default CI Language Class refer here and here for CI.
1) We stored default language in session or initially session could be blank.
2) We used two links to switch language in frontend.
{{ if {session:data name="language"} == 'np' }}
<li><a href="{{ url:site }}language/en">EN</a></li>
{{ else }}
<li><a href="{{ url:site }}language/np">NP</a></li>
{{ endif }}
the /language controller does the magic !
3) Now for the navigation links we created two navigation groups and switched depending upon the language value stored in session.
{{ if {session:data name="language"} == 'np' }}
{{ navigation:links group="header_np" }}
{{ else }}
{{ navigation:links group="header" }}
{{ endif }}
4) /language/en or /language/np can be routed to a function through routes. A function can be used to get specific segment of the url . When it is fetched the necessary session modification can be done on function as:
This modification is done on the session variable (here language)
Finally routing to the base url page as per required
//...controller code
public function language($lang){
if($lang=="en" || $lang=="np") {
$this->session->set_userdata(array('language' => $lang));
}
redirect(base_url()); }
This operation is intended for dual language and can be extended further with required conditional statements.
FYI, this is not a perfect way to build multilingual feature. Problem can arise while dealing with more than two languages. If you have better solution let us know.