Welcome Guest, Not a member yet? Register   Sign In
Fast switching between languages (i18n) (CI 2.0.1)
#1

[eluser]YangHax[/eluser]
I've made a replacement for the core/Lang.php that allows you to use the vanilla language file locations and easily switch between languages.

Make sure to autoload session.

application/core/Lang.php
(see /forums/viewthread/179779 for a bug fix to 2.0.l core overrides loading)
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Description of Language_Many
*
* @author BAHO
*/
class CI_Lang
{
        // language strings; only difference from vanilla is that language idiom is prepended to the key name (i.e. "english.key_name")
    var $language    = array();
        var $is_loaded    = array(); // array of loaded language files

        function __construct()
        {
            log_message('debug', "Language Class Initialized (Custom Multi-Lingual)");
        }

        /**
        * Load a language file
        *
        * @access    public
        * @param    mixed    the name of the language file to be loaded. Can be an array
        * @param    string    the language (english, etc.)
        * @return    mixed
        */
        function load($langfile = '', $idiom = '', $return = FALSE, $add_suffix = TRUE, $alt_path = '')
        {
            $langfile = str_replace(EXT, '', $langfile);

            if ($add_suffix == TRUE)
            {
                    $langfile = str_replace('_lang.', '', $langfile).'_lang';
            }

            $langfile .= EXT;

            if (in_array($langfile, $this->is_loaded, TRUE))
            {
                    return;
            }

            log_message('debug','Loading language file: '.$langfile);

            $config =& get_config();

            // Load a language for each supported section
            $supported_languages = $config['languages_supported'];
            $lang_all = array();
            foreach($supported_languages as $idiom)
            {
                    log_message('debug','Loading language: '.$idiom);

                    // Determine where the language file is and load it
                    if ($alt_path != '' && file_exists($alt_path.'language/'.$idiom.'/'.$langfile))
                    {
                            include($alt_path.'language/'.$idiom.'/'.$langfile);
                    }
                    else
                    {
                            $found = FALSE;

                            foreach (get_instance()->load->get_package_paths(TRUE) as $package_path)
                            {
                                    if (file_exists($package_path.'language/'.$idiom.'/'.$langfile))
                                    {
                                            include($package_path.'language/'.$idiom.'/'.$langfile);
                                            $found = TRUE;
                                            break;
                                    }
                            }

                            if ($found !== TRUE)
                            {
                                    show_error('Unable to load the requested language file: language/'.$idiom.'/'.$langfile);
                            }
                    }


                    if ( ! isset($lang))
                    {
                            log_message('error', 'Language file contains no data: language/'.$idiom.'/'.$langfile);
                            return;
                    }

                    // Append idiom to the beginning of every string
                    $lang_keys = array_keys($lang);
                    foreach($lang_keys as $lang_key)
                    {
                            $lang_string = $lang[$lang_key];
                            log_message('debug',$lang_key.": ".$lang_string);
                            $lang_all[$idiom.'.'.$lang_key] = $lang_string;
                    }

                    unset($lang);
            } // eo supported languages foreach

            if ($return == TRUE)
            {
                    return $lang_all;
            }

            $this->is_loaded[] = $langfile;
            $this->language = array_merge($this->language, $lang_all);
            unset($lang_all);
            return TRUE;
        }
        
    function line($line = '')
    {
                $line = $this->current().'.'.$line; // Prepend to the key
                $line = ($line == '' OR ! isset($this->language[$line])) ? '!--'.$line.'--!' : $this->language[$line];
                return $line;
    }

        // Returns current language - either default from config, or from "lang" in session
        // Example: "english"
        function current()
        {
                // Determine language to use for the line
                $config =& get_config();
                $lang_chosen = $config["language"]; // default

                // Pull from session?
                $lang_session = CI_Controller::get_instance()->session->userdata("lang");
                if ( $lang_session != FALSE ) $lang_chosen = $lang_session;

                return $lang_chosen;
        }
}


application/config/config.php
Code:
/*
|--------------------------------------------------------------------------
|  Language
|--------------------------------------------------------------------------
*/
$config['language']    = 'english'; // default language
$config['languages_supported'] = array("english","russian"); // must have corresponding folder under /application/language

Changing User's Language
Code:
// sets language in session and calls index
        public function lang( $code )
        {
                $this->session->set_userdata(array("lang"=>$code));
                $this->index();
        }
#2

[eluser]YangHax[/eluser]
Sample usage in a view

Code:
<div id="language">
            <p>&lt;?=$this->lang->line('esu.language')?&gt;:</p>
            <ul>
                <li><a >lang->current()=='english'?'class="selected"':''?&gt; href="&lt;?=base_url()?&gt;main/lang/english">English</a></li>
                <li><a >lang->current()=='russian'?'class="selected"':''?&gt; href="&lt;?=base_url()?&gt;main/lang/russian">Русский</a></li>
            </ul>
        </div>
#3

[eluser]InsiteFX[/eluser]
You still do not get it!

You do not replace CodeIgniter core Class!

You extend them.

Any one in their right mind knows better then to use your Class because when they upgrade CodeIgniter it is going to over write your Class file!

Learn to extend the Classes...

InsiteFX
#4

[eluser]YangHax[/eluser]
CI 2.0.1 User Guide "Core Classes" section: http://ellislab.com/codeigniter/user-gui...asses.html

Quote:To use one of your own system classes instead of a default one simply place your version inside your local application/core directory

...no mentions that you MUST extend it vs. replacing it, or any negative implications of doing so.

Also, I'm new to these forums and don't know the culture here, but "Learn to extend the Classes…" ? Have you lost your mind talking like that? If you have nothing useful to say, or you cannot say it in a civil manner, you should not say it at all. Re-read your message and think about the tone you're using.
#5

[eluser]InsiteFX[/eluser]
Quote:Also, I’m new to these forums and don’t know the culture here, but “Learn to extend the Classes…” ? Have you lost your mind talking like that? If you have nothing useful to say, or you cannot say it in a civil manner, you should not say it at all. Re-read your message and think about the tone you’re using.

I Know I have not lost my mind!

Go ahead copy a system directory over and then tell me I have lost my mind.

When there is a new veriosn of CodeIgniter all you have to do is copy over the new system directory, you never have to replace the application directory were your code is.

And if you had a mind you would have searched the forums and read this all over the place!

InsiteFX
#6

[eluser]YangHax[/eluser]
Why would I need to replace the application directory where code is, with this approach, if there's an update?

I replaced 100% of the Lang.php functionality.
#7

[eluser]wh1tel1te[/eluser]
This was recently discussed here.
#8

[eluser]lisahill[/eluser]
You can turn on "fast user switching" to allow more than one user to stay logged in to a computer at a time. For example, if you're working on a spreadsheet of financial data and your daughter begs to check for iChat AV messages, you can switch to your daughter's account. When she's done checking for messages, you can switch back to your account and your spreadsheet is right where you left it.

When fast user switching is turned on, the name of the current user is displayed in the upper-right corner of the menu bar. You can click the name to select another user to switch to. If the other user's account doesn't require a password, the computer switches automatically to the new user's home folder. Otherwise, you see a dialog asking for the user's password.

College Girls
#9

[eluser]YangHax[/eluser]
@wh1tel1te if you read my post, I have link to that same thread at almost very beginning.


...where it was determined - hey, it's A-OK to replace the core classes!




Theme © iAndrew 2016 - Forum software by © MyBB