• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multilanguage Support Needed

#7
[eluser]n1c0[/eluser]
So, yesterday I tried to make our solution working, but the CI parser class (and to tell the truth our language class too) didn't seem to be of any help in the end. Things just kept getting more and more complicated, with absolutely no satisfying results in the end.
So I just took what the code teached me in the last few days and put it together into a solution that would fit me the best.

First of all as you suggested I made a MY_Loader class to extend the native one, here's the code:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Loader extends CI_Loader {
    
    var $loaded_views = array();

    function view($view, $vars = array(), $return = FALSE)
    {
        // We add the functionality to store the loaded views into an array
        $this->loaded_views[] = $view;
        return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
    }
    
    function get_loaded_views()
    {
        return $this->loaded_views;
    }
}

And then I just made a 'display_override' hook to take care of the rest. Here's it's code code:
Code:
<?php

class Multilanguage extends Controller {
    
    function multilanguage_support()
    {
        // Get the lists of called views: each view should have an equivalent _lang file,
        // if it doesn't anyway no errors will be generated
        $loaded_views = $this->load->get_loaded_views();
        
        if (count($loaded_views) > 0)
        {
            // Get the default and the user language
            $CI =& get_instance();
            $deft_lang = $CI->config->item('language');
            $user_lang = 'italian';

            // It's important to have the default language loaded before the user language,
            // so that the second will overwrite the first (if the elements are present)
            $supported_languages = array($deft_lang);
            if ($user_lang != $deft_lang)
            {
                $supported_languages[] = $user_lang;
            }
            
            foreach ($supported_languages as $supported_language)
            {
                foreach ($loaded_views as $loaded_view)
                {
                    if (file_exists(APPPATH.'language/'.$supported_language.'/'.str_replace('_view', '_lang', $loaded_view).EXT))
                    {
                        include(APPPATH.'language/'.$supported_language.'/'.str_replace('_view', '_lang', $loaded_view).EXT);
                    }
                }
            }
            
            // Now that we have the $lang array fully populated we can change each words presented in the view
            // as '{array_index_identifier}' with it's equivalent human readable value
            $view_words = array();
            $human_words = array();
            
            foreach ($lang as $view_word => $human_word)
            {
                $view_words[] = '{'.$view_word.'}';
                $human_words[] = $human_word;
            }
                
            echo str_replace($view_words, $human_words, $CI->output->get_output());
            
            return;
        }
        
        // We'll output the simple code if no views were loaded
        echo $CI->output->get_output();
        return;
    }
}

/* End of file multilanguage.php */
/* Location: ./system/application/hooks/multilanguage.php */

It works quite well, and I feel quite satisfied with it.
What do you think about it? Any suggestion it's gonna be as always very appreciated. Big Grin

Nico


Messages In This Thread
Multilanguage Support Needed - by El Forum - 06-22-2010, 05:06 PM
Multilanguage Support Needed - by El Forum - 06-22-2010, 05:31 PM
Multilanguage Support Needed - by El Forum - 06-23-2010, 02:58 AM
Multilanguage Support Needed - by El Forum - 06-23-2010, 08:17 AM
Multilanguage Support Needed - by El Forum - 06-23-2010, 12:28 PM
Multilanguage Support Needed - by El Forum - 06-23-2010, 12:32 PM
Multilanguage Support Needed - by El Forum - 06-24-2010, 08:51 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.