Multilanguage site Codeigniter

1

I need to create a multilingual website in codeigniter. The texts must come from the database. My intention is that each table has both languages. For example. Table sobre would have

id | titulo_br | titulo_en | conteudo_br | conteudo_en

and the session would have language = en. at the time of doing the select it would look something like this:

$this->db->get('sobre')->result_array();

And no html

<?php $lang = $this->session->userdata('site')['idioma ];
 echo $dados['titulo_' .$lang] ?>

But I find it very laborious. Is there any better way ?. Any library or anything like that to facilitate?

    
asked by anonymous 29.09.2016 / 15:16

2 answers

1

When I made a site with two languages in Codeigniter, some time ago .. I used Cookie.

I put the following flags inside the A:

 <a href="<?php print base_url(); ?>idioma/es">ES</a>

No routes I have the following route:

$route['idioma/(:any)'] = 'index_home/idioma/$1';

Index Controller:

  public function index() {

            if (get_cookie('cookie_linguagem')) {

            } else {
                set_cookie('cookie_linguagem', 'pt');
                redirect('/');
            }
    }

public function idioma($id) {

        set_cookie('cookie_linguagem', $id);
        header("Location: " . $_SERVER['HTTP_REFERER'] . "");
    }

In the register, obviously there was a field with the name _pt and the same field with name_es (which were the two languages I used).

In the selection by the bank I checked the cookie and did the following:

 public function mostra_slides() {
        $idioma = get_cookie('cookie_linguagem');
        $sql = "SELECT imagem_$idioma AS imagem, lead_$idioma AS lead, nome_$idioma AS nome, link FROM tb_slides";
        $q = $this->db->query($sql);
        $row = $q->row();
        if ($q->num_rows() > 0) {
            foreach ($q->result() as $row) {
                $data[] = $row;
            }
            return $data;
        } else {
            return 0;
        }
    }

That is, I create a cookie with the name pt and set it to es when it is needed.

I hope I have helped!

    
29.09.2016 / 16:34
0

I would not create 4 columns informing which language it would be like in the example you cited:

titulo_br | titulo_en | conteudo_br | conteudo_en

I would make a normal table and only create a column to know in which language that record is

id | titulo | conteudo | idioma

An example registry in this table:

1 | Um título | Conteudo da tabela em português | pt
2 | One Title | Content of the table in english | en
  

If it's specific content to show, create a column   call for example ID and place a word, ID   or whatever you find easiest and in SELECT make a WHERE by pulling this   content.

Create the links to select the language of the site / content such as

<a href="<?php echo base_url('idioma/pt');?>">Português</a>
<a href="<?php echo base_url('idioma/en');?>">Inglês</a>

In application / config / routes.php add:

$route['idioma/(:any)'] = 'idioma/selecionar_idioma/$1';

Controller language

<?php
class Idioma extends CI_Controller{

    public function selecionar_idioma($idioma){

        $this->session->set_userdata('idioma', $idioma);
        redirect('index'); //Página inicial ou outra página
    }
}
?>

When you display the contents of the table for example:

$idioma = $this->session->userdata('idioma');

$this->db->where('idioma', $idioma);
$query = $this->db->get('tabela_do_conteudo');

foreach($query->result() as $result){

    echo $result->titulo.'<br />';
    echo $result->conteudo.'<br /><br />';
}

I would do more or less like this. You can also use cookies if you think it's better than session .

    
29.09.2016 / 16:50