简体   繁体   English

如何在Codeigniter中更改语言

[英]how to change language in codeigniter

I've saved a product name in my DB with two language (En & Fr) 我已经用两种语言(En和Fr)将产品名称保存在数据库中

how can i load them differently by just changing the url 我如何通过仅更改URL来不同地加载它们

for example 例如

localhost/mysite/en/products/

loads the "en_name" out of my table 从表中加载“ en_name”

and

localhost/mysite/fr/products/

loads the "fr_name" for me? 为我加载“ fr_name”?

I think the simplest method may be to pass the uri segment to the where clause of the query. 我认为最简单的方法可能是将uri段传递给查询的where子句。

https://www.codeigniter.com/user_guide/libraries/uri.html https://www.codeigniter.com/user_guide/libraries/uri.html

https://www.codeigniter.com/user_guide/database/query_builder.html?highlight=select#selecting-data https://www.codeigniter.com/user_guide/database/query_builder.html?highlight=select#selecting-data

Something like: 就像是:

$lang = $this-uri->segment(3);
$this->db->where('language', $lang);
$this->db->get('table');

The language library is used if you want to store the text in different languages in a file (it will be included in the page) and not in a database. 如果您想将不同语言的文本存储在文件中(它将包含在页面中)而不是数据库中,则使用语言库。 So if the language text is stored in the database, you want to check the method responsible for checking the url segment to check if the url contains the en or the fr and load the corresponding language. 因此,如果语言文本存储在数据库中,则要检查负责检查url段的方法,以检查url是否包含enfr并加载相应的语言。

So the method to use is: 因此使用的方法是:

$this->uri->segment(n)

More info on how to use it: 有关如何使用它的更多信息:
https://www.codeigniter.com/user_guide/libraries/uri.html https://www.codeigniter.com/user_guide/libraries/uri.html

and your condition should be something like: 并且您的情况应该类似于:
Edit 编辑

if($this->uri->segment(1) == "fr"){
   $this->session->set_userdata('lang', 'fr');
}else{
   $this->session->set_userdata('lang', 'en');
}

Now you can easily check for the language selected from the model (db files) by checking the value of the lang Session 现在,您可以通过检查lang Session的值来轻松检查从model (db文件)中选择的lang

i finally did it with this CI extension i18n and here is my code: 我终于使用此CI扩展名i18n做到了,这是我的代码:

$lang = $this->uri->segment(1);
echo $prodct[$lang."_name"];

so now if i enter my url like this 所以现在如果我这样输入网址

localhost/mysite/en/products/

it will get those fields from DB which is like "en_name" , same goes for "fr" too. 它将从数据库中获得诸如“ en_name”之类的字段,“ fr”也是如此。

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

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