Welcome Guest, Not a member yet? Register   Sign In
2 Languages on my site: how to start ?
#1

[eluser]yannyannyann[/eluser]
Hello, I am very new to codeIgniter, I started setting up my MVC but now I am asking myself how to deal with my database entries when I have to deal with languages.

1. Here is what I'd like to achieve :

www.mysite.com -> should be french
www.mysite.com/en -> should be english

What is the best, cookies ? session ? other magical solution ?


2. Then I have in my database some photos:

PHOTO table
-> id
-> title_fr
-> title_en
-> description_fr
-> description_en
-> ...

In my model how do I check what is current language to retrieve the title in the good language ?


thanks in advance
#2

[eluser]pistolPete[/eluser]
Have a look at the wiki: http://codeigniter.com/wiki/Category:Int...alization/

I would rather use 3 tables instead of one:
Code:
photo
  - id
  - filename
  - fr_id
  - en_id
  - (...)

Code:
english
  - id
  - title
  - description
  - (...)

Code:
french
  - id
  - title
  - description
  - (...)

Depending on the current language you would then join the photo table and the corresponding language table.

Quote:In my model how do I check what is current language to retrieve the title in the good language ?
In your controller you pass the current language to your model.

controller:
Code:
(...)
$this->load->model('photo_model');
$this->model->set_lang($current_language);

model:
Code:
class Photo_model extends Model {
    
    var $current_lang;
    
    function set_lang($lang)
    {
        $this->current_lang = $lang
    }
(...)
#3

[eluser]yannyannyann[/eluser]
Good and quick answer I'll try that.
Thanks !
#4

[eluser]TheFuzzy0ne[/eluser]
1. You'll need to have your configuration file check the URI, extract the language from it, and set the default.

Code:
preg_match('/^\/([a-z]+)\//', $_SERVER['REQUEST_URI'], $matches);

$config['language'] = (isset($matches[1]) && $matches[1] == 'en') ? 'english' : 'french';

There are other ways to do it too, but that's probably one of the easiest ways.

2. I'd suggest you change your table slightly.
PHOTO table
-> id
-> title
-> description
-> language

Define some constants in the constants.php file

Code:
DEFINE('ENGLISH', 1);
DEFINE('FRENCH', 2);

Then you can do things like this.

Code:
$result = $this->db->get_where('PHOTOS', array('id' => $id, 'language' => FRENCH));

or

Code:
$data = array(
        'id' => $id,
        'title' => $title,
        'description' => $description,
        'language' => FRENCH
    );
$this->db->insert('PHOTOS', $data);

Hope this helps.
#5

[eluser]xwero[/eluser]
1 why not use the url? check out the uri language identifier. add a default_language setting to the config file and add a function to the MY_language_helper.php or MY_url_helper.php
Code:
function current_language()
{
   $CI =& get_instance();
   if(strlen($CI->uri->segment(1)) != 2)
   {
      return $CI->config->item('default_language');
   }
   else
   {
      return $CI->uri->segment(1);
   }
}

And that brings us to 2. I like to separate the language dependent data from the language independent data so i have two tables; photo with the fields id and file and photo_lang with the fields photo_id,lang,title,description. So the query to get a photo is:
Quote:SELECT t1.id, t1.file, t2.title, t2.description
FROM photo t1
LEFT JOIN photo_lang t2 ON t2.photo_id = t1.id
WHERE t1.id = ? and t2.lang = ?

EDIT : i'm getting to old for this Smile
#6

[eluser]xwero[/eluser]
[quote author="pistolPete" date="1236100834"]

I would rather use 3 tables instead of one:
Code:
photo
  - id
  - filename
  - fr_id
  - en_id
  - (...)

Code:
english
  - id
  - title
  - description
  - (...)

Code:
french
  - id
  - title
  - description
  - (...)
[/quote]
A table for each language, isn't that a bit too much?
#7

[eluser]TheFuzzy0ne[/eluser]
Wow. Spoilt for choice. Tongue

[quote author="xwero" date=""]
EDIT : i’m getting to old for this Smile
[/quote]

You're starting to sound like Danny Glover in Lethal Weapon.
#8

[eluser]yannyannyann[/eluser]
Quote:2. I’d suggest you change your table slightly.
PHOTO table
-> id
-> title
-> description
-> language

In this case I don't see how you can keep one photo id but having 2 languages ?
Because all the pictures will have both en + fr information.


I think I will go for 3 tables with the SELECT ... JOIN solution.
#9

[eluser]yannyannyann[/eluser]
1. I will try this :

- add a default_language setting to the config file

and

Code:
function current_language()
{
   $CI =& get_instance();
   if(strlen($CI->uri->segment(1)) != 2)
   {
      return $CI->config->item('default_language');
   }
   else
   {
      return $CI->uri->segment(1);
   }
}

2. And I will create 2 tables (not 3 as I said before) :

- photo (id, filename, date_added, date_shoot, author, copyright)
- photo_lang (id, lang, photoid, title, description, keywords)
#10

[eluser]TheFuzzy0ne[/eluser]
You're totally right. My bad. Having a table for each is definitely the way forward.

EDIT, the id column in your second table is probably not necessary.




Theme © iAndrew 2016 - Forum software by © MyBB