Codeigniter multilingual site

Asked

Viewed 268 times

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 in the session would have language = en. at the time to select would be more or less like this:

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

And in html

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

But I’m finding it very laborious. Is there a better way?. Some library or something to make it easier?

2 answers

0

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

I set the flags at the top the next given inside an A:

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

In the Routes I set 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, I obviously had 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 as follows:

 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, create a cookie with the name pt and Seto it for es when necessary.

I hope I’ve helped!

0

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

titulo_br | titulo_en | conteudo_br | conteudo_en

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

id | titulo | conteudo | idioma

An example record 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 is specific content to be displayed, create a column call for example identification and in it put a word, ID or what you find easier and in SELECT make a WHERE pulling that contents.

Create links to select site language/content, for example

<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 displaying table content 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 think better than session.

  • 1

    A great solution too... I will analyze and see what is the best way. vlw.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.