The fastest way to assign language variables in the OpenCart controller.

Sorry for my English! English is not my native language. One of the reasons to create this blog is to improve my English writing. So I will be highly obliged if you will help me with this. If you find a grammar error on this page, please select it with your mouse and press Ctrl+Enter.

The fastest way to assign language variables in the OpenCart controller. Probably the most boring thing in the OpenCart module development is assign language variables in the controllers.


First, you should add all the language variables into all the language files. Then you should every  language variable add to the $data in the controller. Then you should every language variable add into the template file. If your module has 2 or 3 language variables it is not a problem. But in the most cases modules have dozens of variables and for every of them you should do this:  


$this->data['foo1'] = $this->language->get('bar1');
$this->data['foo2'] = $this->language->get('bar2');
// dozens of the $this->language->get()


You can replace all these lines with just one:


// OpenCart 1.5.x
$this->data = $this->language->load('path/file');
// OpenCart 2.0.x
$data = $this->language->load('path/file');


It will assign all the language variables to the $data because $this->language->load('path/file') returns an array with all language variables.
You should remember that $this->language->load('path/file') returns not just a 'path/file' variables, but all the variables in the global language array. But you have nothing to worry about conflicts. 
I tested this approach on the admin/controller/catalog/product.php and catalog/controller/product/product.php. They are the biggest controllers with dozens of the language and controller variables. And everything worked fine. 

Why this approach isn't used in the OpenCart core? Does anyone like to add dozens of the unnecessary lines of code into the controllers?..

Add new comment

Spam protection
Target Image