Welcome Guest, Not a member yet? Register   Sign In
Multiple languages website using CodeIgniter
#1

[eluser]Christophe28[/eluser]
Hello,

I'm trying to create a multiple language website using CodeIgniter.

The idea is to create a library with a map containing the language files.

Now the library looks like this:
Code:
Class Languages {

    function Languages() {
    
    }

    function user_language() {
    
        $CI =& get_instance();
        
        // Get user prefered language (whether logged in or not)
        if(isset($CI->session->userdata['lang'])) {
        
            $lang = $CI->session->userdata['lang'];
            
        } else {
        
            $lang = 'EN';
        
        }
        
            
            switch ($lang) {
                case 'EN':
                    include 'languages/english.php';
                    break;
                    
                case 'NL':
                    include 'languages/dutch.php';
                    break;
                    
                default:
                    include 'languages/english.php';
            }
        
        return $lang;
    
    }

}

A language (like english.php) file looks like this:
Code:
<?php

$lang = array(
    // Welcome (homepage)
    
    'WELCOME_LEARN_MORE'            =>         'Learn more ...',
    
    // Languages
    'LANG_ENGLISH'                    =>        'English',
    'LANG_DUTCH'                    =>        'Dutch',
    
    // Account
    'REGISTER'                        =>        'Register',
    'ACCOUNT_REGISTER_NOW'             =>        'Register now!',
    'LOGIN'                            =>        'Login',
    'FORGOT_PASSWORD'                =>        'Forgot password',
    
);

The controller:
Code:
// Set user language
        $data['lang'] = $this->languages->user_language();
Finally the view:
Code:
<div id="welcome_box_left">
        <h1>Welcome</h1>
        <p>&lt;?php echo $lang['WELCOME_LEFT_BOX']; ?&gt;</p>
        <a href="docs/">&lt;?php echo $lang['WELCOME_LEARN_MORE']; ?&gt;</a>
        
    </div>

But in the view I would rather like instead of $lang['WELCOME_LEFT_BOX'] see $lang->welcome_left_box ... That would be much prettier :-)

Does anybode know how I can do this in CodeIgniter using my library?

Thx!

Christophe
#2

[eluser]treeface[/eluser]
Hi Christophe,

Have you given the Language class a look?

http://ellislab.com/codeigniter/user-gui...guage.html

There's also a helper class available that lets you access your information:

http://ellislab.com/codeigniter/user-gui...elper.html
#3

[eluser]Christophe28[/eluser]
Hi,

Thanks for the reply!

I already got to manage it using the following function:
Code:
function parseArrayToObject($array) {
           $object = new stdClass();
           if (is_array($array) && count($array) > 0) {
              foreach ($array as $name=>$value) {
                 $name = strtolower(trim($name));
                 if (!empty($name)) {
                    $object->$name = $value;
                 }
              }
           }
           return $object;
        }

        $lang = parseArrayToObject($lang);

But I'll take a look at the language class ;-)

Best,
Christophe
#4

[eluser]danmontgomery[/eluser]
...

Code:
$array = array('foo' => 'bar');
echo $array['foo']; // bar
$object = (object)$array;
echo $object->foo; // bar

http://php.net/manual/en/language.types....ggling.php
#5

[eluser]Christophe28[/eluser]
Waw, didn't know that! Thanks for sharing!

1 line instead of 13 lines Smile

Christophe
#6

[eluser]Unknown[/eluser]
thank a lot!




Theme © iAndrew 2016 - Forum software by © MyBB