Page 1 of 1

another way to load language file, and make less code

Posted: Sun Feb 21, 2010 3:07 pm
by yukai.lin
I tried another way to load language file, and it make less code, u dun need add the code like '
$this->data['heading_title'] = $this->language->get('heading_title'); '
. On the other hand, I am not sure if the performance would be worse. :)

1.add the code in \system\library\language.php:

Code: Select all

    public function getFile($filename) {
		return $file = $file = DIR_LANGUAGE . $this->directory . '/' . $filename . '.php';
	}
2.then add the code in \system\engine\controller.php:

Code: Select all

    public function load_language($filename) {
        $this->languages = Registry::get('language');

       $file = $this->languages->getFile($filename);

      if (file_exists($file)) {

         $_ = array();

       require($file);

       $this->data = array_merge($this->data, $_);

     } else {
          exit('Error: Could not load language ' . $filename . '!');
        }
    }
3. load the language by using $this->load_language('common/login');
and u dun need load the language by using $this->data['heading_title'] = $this->language->get('heading_title'); any more.

4.in the tpl, u still display the text with <?php echo $heading_title;?>, no need change anything in tpl.

Re: another way to load language file, and make less code

Posted: Sun Feb 21, 2010 4:19 pm
by OSWorX
Seems like a good approach.
Anyway, let us wait for next OC release after 1.4.0, I still do not know what Daniel will change/add/remove.

Re: another way to load language file, and make less code

Posted: Thu Mar 08, 2012 8:34 pm
by AvanOsch
Ok, now @ version 1.5.2.1, and we're still typing loads of senseless code...

I totally get that OpenCart wants to keep to the MVC structure, but c'mon...
Isn't such a thing made up to make life easier?!?

Code: Select all

We're just repeating lot's of code, because we're "supposed to", like a bunch of drones!
We're just repeating lot's of code, because we're "supposed to", like a bunch of drones!
We're just repeating lot's of code, because we're "supposed to", like a bunch of drones!
I'll be using a modified version of yukai.lin's code from now on!

Re: another way to load language file, and make less code

Posted: Thu Mar 08, 2012 11:29 pm
by Qphoria
AvanOsch wrote:Ok, now @ version 1.5.2.1, and we're still typing loads of senseless code...

I totally get that OpenCart wants to keep to the MVC structure, but c'mon...
Isn't such a thing made up to make life easier?!?

Code: Select all

We're just repeating lot's of code, because we're "supposed to", like a bunch of drones!
We're just repeating lot's of code, because we're "supposed to", like a bunch of drones!
We're just repeating lot's of code, because we're "supposed to", like a bunch of drones!
I'll be using a modified version of yukai.lin's code from now on!
No, you wont. This has actually already been in the code since 1.4.8. Daniel chose NOT to use it in the core files because he likes it the old way, but you can certainly use this method for your own files. The syntax used in opencart is discussed here:
http://forum.opencart.com/viewtopic.php?f=24&t=19000 (item number 1)

Simply replace

Code: Select all

$this->language->load('payment/mypay');
with

Code: Select all

$this->data = array_merge($this->data, $this->load->language('payment/mypay'));
Then you can get rid of all the separate langauge lines.

Re: another way to load language file, and make less code

Posted: Fri Mar 09, 2012 12:05 am
by AvanOsch
Qphoria wrote: No, you wont.
Yeah, found that out for myself too ;D

Thanks for the fix!