Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to build a multilingual site with codeigniter?

My current approach has been to use to _remap function provided by codeigniter to get the URI segment in order to check if the language is "en" or "np"

Here is a sample:

function _remap($url_title){

    $this->_identify_language($this->uri->segment(1));

    $data ['sub_categories'] = $this->category_model->get_category_list_by_url($url_title)->result_array();
    $data ['news'] = $this->news_model->get_news_list_by_url($url_title)->result_array();
    $data ['url_title'] = $url_title;

    $this->_render_front_view('main',$data);
}

I am using this technique on every controller. Which is well not very efficient.

I wanted to ask if using sessions to store language codes would be better or is my current technique good enough?

Are there any other ways i can do this multi-lingual thing?

Of course my database is currently shaped for 2 lanaguages and i have seperated the fields. e.g:- title_en, title_np. these are echoed according to the language field used.

like image 460
sodhancha Avatar asked Feb 03 '23 19:02

sodhancha


1 Answers

Lots of parts to this.

  1. Your URL's do not really need to be /en/ and /fr/ unless you want it to be used for Google Analytics. Spidering doesn't make a lot of difference. Accept-Language headers can be just as reliable.

  2. Globally parse this URL segment. You can use this method or the Accept-Language, but either way you need a hook, a MY_Controller or extend the Lang class.

  3. Think about if you want the different languages to be totally seperate. For example, if I have an English page not translated to French, and the French page does not exist, should it show the English page or 404? You can either store the lang = fr in the database and take the value from a constant set in the hook/MY_Controller/etc.

    WHERE lang = CURRENT_LANGUAGE

  4. Structure your DB. title_en title_fr is one method, but it soon because unmanagable with lots of languages. Have a "pages" and "page_content" table, so that all generic information is in one table then all language specific (title, content, meta, etc) is in the page_conten table, which has a lang field.

There are a million ways to do all of this, but there is lots more to think about than just the URL. My favourite

like image 196
Phil Sturgeon Avatar answered Feb 05 '23 09:02

Phil Sturgeon