Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter dynamic language functionality

I am Codeigniter and i need dynamic language for users.

I have added drop-down at the header and i want to allow users to change language of site at the frontend.

i tried to change language with below code in one controller

$this->config->set_item('language','spanish');

but its not working its not changing language

i also tried with taking session with below code in one of my controller

$mylanguage = $this->session->set_userdata(array('my_language',$dynamiclang));

and tried to access this variable in config file but its also not working.

help me to make this work.

like image 886
Ashok Chandrapal Avatar asked Aug 22 '26 05:08

Ashok Chandrapal


1 Answers

Finally I Got Success to make multi language

follow below steps

MY_Lang.php file in application\core folder

MY_Lang.php
<?php  
(defined('BASEPATH')) OR exit('No direct script access allowed');

class MY_Lang extends CI_Lang
{
    function __construct() {

        global $URI, $CFG, $IN;

        $config =& $CFG->config;

        $index_page    = $config['index_page'];
        $lang_ignore   = $config['lang_ignore'];
        $default_abbr  = $config['language_abbr'];
        $lang_uri_abbr = $config['lang_uri_abbr'];
        #exit('my_lang');   
        #print_r($URI); 
        /*if($index_page=='es')
        {
            #$config['index_page'] = 'es';
            #$config['lang_uri_abbr'] = 'es';
            #$IN->set_cookie('user_lang', 'es', $config['sess_expiration']);
            #$URI->uri_string = str_replace('es','en',$URI->uri_string);
            }
        else{
            #$config['index_page'] = 'en';
            #$config['lang_uri_abbr'] = 'en';
            #$IN->set_cookie('user_lang', 'en', $config['sess_expiration']);
            }
        /* get the language abbreviation from uri */
       $uri_abbr = $URI->segment(1);
        #$uri_abbr='es';    
        /* adjust the uri string leading slash */
        #print $URI->uri_string;
        $URI->uri_string = preg_replace("|^\/?|", '/', $URI->uri_string);



        if ($lang_ignore) {

            if (isset($lang_uri_abbr[$uri_abbr])) {

                /* set the language_abbreviation cookie */
                $IN->set_cookie('user_lang', $uri_abbr, $config['sess_expiration']);

            } else {

                /* get the language_abbreviation from cookie */
                 $lang_abbr = $IN->cookie($config['cookie_prefix'].'user_lang');

            }

            if (strlen($uri_abbr) == 2) {

                /* reset the uri identifier */
                 $index_page .= empty($index_page) ? '' : '/';
              //  exit('654');
                /* remove the invalid abbreviation */
                $URI->uri_string = preg_replace("|^\/?$uri_abbr\/?|", '', $URI->uri_string);

                /* redirect */
                header('Location: '.$config['base_url'].$index_page.$URI->uri_string);
                exit;
            }

        } else {

            /* set the language abbreviation */
            $lang_abbr = $uri_abbr;
        }

        /* check validity against config array */
        if (isset($lang_uri_abbr[$lang_abbr])) {


           /* reset uri segments and uri string */
           //$URI->_reindex_segments(array_shift($URI->segments)); # this is commented becasue this is giving error : @$hok : 09/August/2015
           $URI->uri_string = preg_replace("|^\/?$lang_abbr|", '', $URI->uri_string);

           /* set config language values to match the user language */
           $config['language'] = $lang_uri_abbr[$lang_abbr];
           $config['language_abbr'] = $lang_abbr;


           /* if abbreviation is not ignored */
           if ( ! $lang_ignore) {

                   /* check and set the uri identifier */
                   $index_page .= empty($index_page) ? $lang_abbr : "/$lang_abbr";

                /* reset the index_page value */
                $config['index_page'] = $index_page;
           }

           /* set the language_abbreviation cookie */               
           $IN->set_cookie('user_lang', $lang_abbr, $config['sess_expiration']);

        } else {

            /* if abbreviation is not ignored */   
            if ( ! $lang_ignore) {                   

                   /* check and set the uri identifier to the default value */    
                $index_page .= empty($index_page) ? $default_abbr : "/$default_abbr";

                if (strlen($lang_abbr) == 2) {

                    /* remove invalid abbreviation */
                    $URI->uri_string = preg_replace("|^\/?$lang_abbr|", '', $URI->uri_string);
                }
                /*echo '<pre>';
                print_r($_SERVER);
                print_r($config['base_url'].$index_page.$URI->uri_string);
                exit;*/
                $q = $_SERVER['QUERY_STRING'];
                if($q)
                    $q = "/?".$q;
                /* redirect */
                header('Location: '.$config['base_url'].$index_page.$URI->uri_string.$q);
                exit;
            }

            /* set the language_abbreviation cookie */                
            $IN->set_cookie('user_lang', $default_abbr, $config['sess_expiration']);
        }

        log_message('debug', "Language_Identifier Class Initialized");
    }
}

/* translate helper */
function t($line) {
    global $LANG;
//print_r($LANG);
//  exit;
    return ($t = $LANG->line($line)) ? $t : $line;
} 
function _t($line,$params=array()) {
    global $LANG;
    if($params){
        echo str_replace(array_keys($params),array_values($params),($t = $LANG->line($line)) ? $t : $line);
    }
    else
        echo ($t = $LANG->line($line)) ? $t : $line;
} ?>

and added below things in config.php

$config['language'] = "english";

/* default language abbreviation */
$config['language_abbr'] = "en";

/* set available language abbreviations */
$config['lang_uri_abbr'] = array("en" => "english","es" => "spanish","ca" => "catalan");

/* hide the language segment (use cookie) */
$config['lang_ignore'] = TRUE;

added below code in route.php

$route['^en/(.+)$'] = "$1";
$route['^es/(.+)$'] = "$1";
$route['^ca/(.+)$'] = "$1";

$route['^(\w{2})$'] = $route['default_controller'];
$route['^(\w{2})/(.+)$'] = "$2";

and added language files in language folder like below

language/catalan 
language/spanish 
language/english

i hope this will help.

like image 66
Ashok Chandrapal Avatar answered Aug 23 '26 19:08

Ashok Chandrapal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!