简体   繁体   中英

CodeIgniter and Multilanguage Database

I'm creating a multilanguage application on CodeIgniter and I'm kinda confused on the question how to make the database in the right way.
I've done this before, but using the "easiest" approach:

Table news

id
title_en
title_de
title_rus
title_fr

and so on...
It is easy, cause inserting in the database is done by one query, selecting is from one query too, easy job.
And when I want to display the language that I need for the user I only select the field that accords to.
Using this approach everything that depends on the database is easy (insert, select, update).
But when I need to add another language there are a lot of things to change.
So i thought lets try putting all the text fields which are different for every language in another table.

Table news

id
date
author

Table news_lang

id
news_id
title
language

So I divided it to two tables which should eliminate the problem with new languages for me, and it should make the database look more clean.
Selecting the current language is no problem:

$this->db->select("news.id, news.date, news.author, news_lang.title");
$this->db->join("news_lang", "news_lang.news_id = news.id");
$this->db->where("news.id", $news_id);
$this->db->where("news_lang.language", $language);
$this->db->get("news")->row();


But what to do when I need all the languages fields for editing?
I may have to set them on another object or array which is not so nice.
And the main question which causes me to ask here - How to insert all the data?
In the first approach I've done the things in one method in model. Now I don't think there is any other way without using some loop.
Im I using wrong logic? Is there another "clean" way to do this?
I was thinking about putting all the data in one field without making two tables, but making the language data serialzed or encoded with json and then put it one field, but I'm not pretty sure if that is a great idea.
Could you give me any advices?
Thank you, Mike

What i would do is have separate tables for each language and then i would not directy access the codeigniter database object but rather create a factory which would give me the model i need depending on which language i configured it for. then from that point you would use whatever db object you got from the factory as though there were only one language.

something like this:

$myDBmodel = $this->dbFactory('news','fr');

$myDBmodel->select("id, date, author, etc...");

then there's the factory:

function dbFactory($table, $language) {

    switch($language) {
        case 'fr':

            (psudocode: return french version of db model)

        default:

            (psudocode:  return whatever your default language version of the model is)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM