Welcome Guest, Not a member yet? Register   Sign In
Multiple Language Website
#1

[eluser]Unknown[/eluser]
Hi All,

I have just began developing websites on codeigniter . Now i want to develop website in two languages(english and french) the record will stored in two fields assume content_en(english) and content_fr(french). Now how can i display the data from content_fr when i select french language and data from content_en when i select english language(note: english will be default language).

Thanks in advance
#2

[eluser]Jelmer[/eluser]
What do you mean? The problem you're trying to solve is probably not how to display one value or another, but how to select which language is being sought.

Of the top of my head I can think of 2 solutions:
- Use routes as an identifier by starting each URL with either "en" or "fr", try searching the wiki and the forums for that one - I'm quite sure someone already wrote a solution for that one.
- Use (sub)domains and give both their own index.php in which you define a constant that has the language in it.

There's probably many more. Best practice is to always use unique URLs for each and not use sessions or cookies to do the selection (which can't be seen in the URL). Also prevent the website from handling 2 as the same. For instance if you got yourdomain.com/en/page and want yourdomain.com/page to use the default language, make it redirect to the default language and not just serve up the default.

EDIT:
Also I'd use another way to save pages. Giving one entry a field for each language is limiting. You need one language field for each for content (content_en, content_fr) but titles are probably also language specific (title_en, title_fr) and that goes on. If you want to add a language your tables are going to be more and more massive.
The better way is to give the entries a language identifier (a field "language") and save each page for both English and French. If there's values to be shared, make your admin do the heavy lifting to replicate those. Or create a master table with non-language-specific fields on which you JOIN a table that has the language specific fields.
#3

[eluser]Unknown[/eluser]
if you're using
Code:
$config['enable_query_strings'] = TRUE;
it's alot easier, all you need to do is add ?lang=foo to your query string and you can do
Code:
switch($_GET['lang']){
case "fr":
  //... query to get french data from DB
   $this->load->view('fr/news');
break;
default:
  //... query to get english data from DB
  $this->load->view('en/news');
break;
}

etc ...

if you're not using query strings, it's tougher.
I used a modified URI Language Identifier (which i still am modifying) but I need to remove
Code:
$RTR->uri->_reindex_segments(array_shift($RTR->uri->segments));
so you can do this for example:
http://www.site.com/en/news/

Code:
switch($this->uri->segment(1))
{
  case "fr":
    //... query to get french data from DB
    $this->load->view('fr/news');
  break;
  default:
    //... query to get english data from DB
    $this->load->view('en/news');
  break;
}
I'm working on this when I have time, but seeing as 75% of my sites need to be bilingual, and I need to pull different fields in the database depending on language, I feel your pain.
I still haven't used CI in production.




Theme © iAndrew 2016 - Forum software by © MyBB