Multilingual set up of codeigniter

2019-07-21 00:46发布

问题:

I'm using this tutorial http://sumonbd.wordpress.com/2009/09/16/develop-multilingual-site-using-codeigniter-i18n-library/ for multilingual in codeigniter.

I've follow the instruction listed correctly. It does not gave me any error, but I couldn't say that it is working properly. when I run through mysite.com/en/about and mysite.com/fr/about it always give me the default language which is english. I'm wondering if there is any configuration that I need to set to be able to work properly.

I'm thinking about this in config.php

$config['language']

and

this in autoload.php

$autoload['language']

Do I have to configure those? or any other configuration to work the multilingual properly.

回答1:

After analyzing the code of CI and the codes in the blog I've come up to this solution.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class About extends CI_Controller {

    function index()
    {
        // you might want to just autoload these two helpers
        $this->load->helper('language');
        $this->load->helper('url');

    $this->getLang();
        $this->load->view('about');
    }

    function getLang(){
        $url =  $_SERVER['REQUEST_URI'];
        $lang = explode("/", $url);

        if($lang[2] == 'en'){
        // load language file
            return $loadLang = $this->lang->load('english','english');
        }
        else if($lang[2] == 'fr'){
        // load language file
            return $loadLang = $this->lang->load('french','french');
        }
        else{
            // load language file
            return $loadLang = $this->lang->load('english','english');
        }

        return false;
    }


}

I've created the function getLang() in which I load the language file.

$this->lang->load('language_file','language_folder');


回答2:

Sorry, you don't provide enough information about what you have done. But I'll try to answer.

I've try the updated tutorial, and it's works like a charm.

First, you point to an out to date tutorial. This is the source of your link with an update: source

Second, a file prefixed with MY_ (default configuration) is a core classes ( see Core Classes ) not a library and should be placed on application/core directory.

Third, in your view file, try to avoid using php short tag (<?=$variabel;?>) because not all server configuration enable php short tag. Use normal echo tag instead <?php echo $var; ?>. Longer, but worth it as you write the standard pattern.

As I said before, if you follow the updated tutorial your script should work as mine. I hope this help. Sorry for my English, correct me if I'm wrong.



回答3:

i think you should have a look at URI-Language-Identifier i use it for my multilingual projects, and its working as it should.