CodeIgniter’s language management – improvement

Have you ever been annoyed by using the CodeIgniter’s language/localization functionality? I have been pissed by constantly repeating:

echo $this->lang->line('sample_label');


So today I found a forum thread in CodeIgniter forums asking about using the language functionality and a shortcut to that language variables. That made me think of some kind of simplification of the code that we all use. So what I did was just small improvement in the Loader class – specifically the view function. I created  MY_Loader file in libraries folder and put simply these contents:

<?php
class MY_Loader extends CI_Loader
{
 /**
 * Load View
 *
 * This function is used to load a "view" file.  It has three parameters:
 *
 * 1. The name of the "view" file to be included.
 * 2. An associative array of data to be extracted for use in the view.
 * 3. TRUE/FALSE - whether to return the data or load it.  In
 * some cases it's advantageous to be able to return data so that
 * a developer can process it in some way.
 *
 * @access    public
 * @param    string
 * @param    array
 * @param    bool
 * @return    void
 */
 function view($view, $vars = array(), $return = FALSE)
 {
     $vars = $this->_ci_object_to_array($vars);
     $CI =& get_instance();
     $vars['lang'] = &$CI->lang->language;
     return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars),
     '_ci_return' => $return));
  }
}
?>

This way I could just use the $lang array in the view like this:

$lang['sample_label']

UPDATE:
Sorry for messing this … I found out that in the lang helper
of CodeIgniter there is a magic function I have never been aware of -> lang(‘language key’)

This post is also available in: Bulgarian

    • BuKToP
    • August 9th, 2010 5:10pm

    Nice, but will it work if I am using the parser class? Maybe not, as I am not using $this->view in my controllers. Btw, I saw your posts in the CI forums :]

    • Nice catch BuKToP ! I have never used the parser – I think it could have a little bad effect on performance so I try to stay away from such stuff. But actually soon I was thinking on getting more aware of it and start using it. As I see the parser class – it actually makes $CI->load->view call – so I can suppose that it will be fine using it with the parser.

  1. Nowadays, the actual dial contains a telemeter size, something that have not, nor will i ever benefit from. It in no way bothered me, but any time writing this unique review We amusingly were recalled that going barefoot was furthermore there. The sit back and watch only main advice on when to utilize it is usually to perhaps measure the length lighting is beyond you determined by listening so that you can its magic. A telemeter takes advantage of the watch not to mention sound to help you measure exactly how distant a thing is by you. Graduated to an assumed background temperature regarding watch that could be. I appreciate it isn’t lighter green in color. The lume is top notch, but there’s an easy mild trade-off given the colour of typically the lume. It glows legitimate and environmentally friendly, but when ever painted follow makes lighting absorption a little less. However, there ‘s no lume for the chronograph – so effort measuring set in light just. Complete, from a computer program standpoint I allow the new check out high signifies, and it’s always good seeking to boot having its balanced number of dial includes.

  2. I have been blessed this column favorable real for me to purchase a lot of information , as if they were in it .

  3. Because you can arrest it , I put a hot link to my website actually , the man added . My readers , accept for the happiness of the above .

  4. The main benefit has to be that as long as you’re connected to WiFi, you can make and receive calls and texts when you’ve got very little or no signal. This is great news for EE users that get bad reception at home or work, as it’s a cheaper and simpler alternative to switching service providers.

  5. Members of the team would include engineers from Apple’s core operating system group, a quality assurance engineer, a project manager, and either a document writer or a tool writer.

  6. stadium lights OAK LED Co ,Limited specialized in high power led lighting for stadium We offer 100 2000w LED Football Stadium Light With one of the top football stadium light manufacturers and suppliers, we produce high quality products with reliable quality stadium lights

  7. led stadium light OAK LED Co ,Limited specialized in high power led lighting for stadium We offer 100 2000w LED Football Stadium Light With one of the top football stadium light manufacturers and suppliers, we produce high quality products with reliable quality led stadium light

  1. No trackbacks yet.