简体   繁体   English

CodeIgniter和多语言数据库

[英]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. 我正在CodeIgniter上创建一个多语言应用程序,我对如何以正确的方式创建数据库感到困惑。
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 表格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. 我当时正在考虑将所有数据放在一个字段中而不创建两个表,而是将语言数据序列化或用json编码,然后将其放在一个字段中,但是我不确定这是否是一个好主意。
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. 我要做的是每种语言都有单独的表,然后我不直接访问codeigniter数据库对象,而是创建一个工厂,该工厂将根据配置语言为我提供所需的模型。 then from that point you would use whatever db object you got from the factory as though there were only one language. 然后从那时起,您将使用从工厂获得的任何db对象,就好像只有一种语言一样。

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)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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