Part of the EllisLab Network
   
 
librarie: multi_language
Posted: 13 July 2007 04:32 AM   [ Ignore ]  
Summer Student
Total Posts:  4
Joined  06-15-2007

hi,
i am have create a Librarie for CI witch allows you to create a multi-language page:

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

class
multi_language
{
    
    private $language
;
    
private $object;
    
    
public function __construct()
    
{

        $this
->object =& get_instance();

        
$lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
        
        
$lange = explode(';', $lang);
        
$lange2 = explode('-', $lange[0]);
        
$lang = $lange2[0];
        
        
$dir_name = './system/application/views/' . $lang . '/';
        if(
is_dir($dir_name))
        
{
            $this
->language = $lang;
        
}else{
            $this
->language = 'en';
        
}
    }
    
    public
function load_view($name,$data = array())
    
{
        $this
->object->load->view($this->language . '/' . $name, $data);
    
}
    
}

?>

Create the Dir’s and the Views like this:

system\application\views\de\welcome.php
system\application\views\fr\welcome
.php
system\application\views\en\welcome
.php

save the librarie in

system\application\libraries\multi_language.php

and implement it in you controller like this:

<?php
class Welcome extends Controller {

    
function Welcome()
    
{
        parent
::Controller();
        
$this->load->library('multi_language');
    
}
    
    
function index()
    
{
        $this
->multi_language->load_view('welcome');
    
}
}
?>

I hope you can use it. :D
And sry about my bad english… xD

good luck

Profile
 
 
Posted: 13 July 2007 04:52 AM   [ Ignore ]   [ # 1 ]  
Summer Student
Total Posts:  4
Joined  06-15-2007

a second method to make an multi-language ci is to modified the loader-class

Replace the Constructor of the loader-class

system/libraries/loader.php

with these code

/**
     * Constructor
     *
     * Sets the path to the view files and gets the initial output buffering level
     *
     * @access    public
     */
    
function CI_Loader()
    
{    
        $this
->_ci_is_php5 = (floor(phpversion()) >= 5) ? TRUE : FALSE;
        
        
$lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
        
$lange = explode(';', $lang);
        
$lange2 = explode('-', $lange[0]);
        
$lang = $lange2[0];
        
        
$view_path = APPPATH.'views/' . $lang . '/';
        
        if(
is_dir($view_path))
        
{
            $this
->_ci_view_path = $view_path;
        
}else{
            $this
->_ci_view_path = APPPATH.'views/en/';
        
}        
        
        
        $this
->_ci_ob_level  = ob_get_level();
                
        
log_message('debug', "Loader Class Initialized");
    
}

and implement the controller like this:

<?php

class Welcome extends Controller {

    
function Welcome()
    
{
        parent
::Controller();
    
}
    
    
function index()
    
{
        $this
->load->view('welcome');
    
}
}

?>

wink

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120267 Total Logged-in Users: 31
Total Topics: 126396 Total Anonymous Users: 2
Total Replies: 664724 Total Guests: 368
Total Posts: 791120    
Members ( View Memberlist )