Simplify Language Load

Written by in Tips, Tutorials on October 19, 2012  |  3 Comments

simplify-language-load

In OpenCart, to pass language variable into template we need to defines it all in the controller. But actually there is a trick to allow us automatically define language variable in controller instead of write it one by one.

When we load language file, OpenCart Language library merge existing data with the new language variable and return it as array. Since Controller engine extract the array and push it to template, so we can simplify the language load with:

$this->data = $this->language->load('product/product');

In default method (product controller) :

$this->language->load('product/product');

$this->data['text_select'] = $this->language->get('text_select');
$this->data['text_manufacturer'] = $this->language->get('text_manufacturer');
$this->data['text_model'] = $this->language->get('text_model');
$this->data['text_reward'] = $this->language->get('text_reward');
... ...
$this->data['text_minimum'] = sprintf($this->language->get('text_minimum'), $product_info['minimum']);
... ...

... ...
$this->data['button_compare'] = $this->language->get('button_compare');			
$this->data['button_upload'] = $this->language->get('button_upload');
$this->data['button_continue'] = $this->language->get('button_continue');

$this->load->model('catalog/review');

$this->data['tab_description'] = $this->language->get('tab_description');
$this->data['tab_attribute'] = $this->language->get('tab_attribute');
$this->data['tab_review'] = sprintf($this->language->get('tab_review'), $this->model_catalog_review->getTotalReviewsByProductId($this->request->get['product_id']));
$this->data['tab_related'] = $this->language->get('tab_related');

Simple method:

$this->data = $this->language->load('product/product');

// All language automatically defined, but some data need to be re-define with proper value.
$this->data['text_minimum'] = sprintf($this->language->get('text_minimum'), $product_info['minimum']);

$this->load->model('catalog/review');
$this->data['tab_review'] = sprintf($this->language->get('tab_review'), $this->model_catalog_review->getTotalReviewsByProductId($this->request->get['product_id']));

Simple method, load two language that have same variable:

$this->data = $this->language->load('product/product'); // $_['text_error'] = 'Product not found!';
$this->data['text_error_product'] = $this->language->get('text_error');

$this->data = $this->language->load('product/category'); // $_['text_error'] = 'Category not found!';

// template ===============================
echo $text_error_product; // result: Product not found!
echo $text_error; // result: Category not found!

With this, all language variables is now automatically pushed to template. You need to remember to re-defines language variable if need an extra value or variable conflict.

Tags: , ,

About the Author

The man behind OpencartNews and EchoThemes.

View all posts by

3 Comments on "Simplify Language Load"

  1. Jerome Bohg May 17, 2014 at 2:28 pm · Reply

    This is really handy stuff. Can drive me crazy sometimes, defining all the language items. Thanks for the great tip!!

  2. Cevarief October 21, 2012 at 3:06 am · Reply

    As addition,

    $this->data = $this->language->load(‘product/product’); will be best suited if we want to load language before any variable assignment to $this->data variable, which means it is still empty.

    If we later decide to load another language somewhere in the middle of a function then we should merge new loaded language with existing $this->data variable, so it won’t replace all data that has been assigned previously.

    For example :
    $this->data = $this->language->load(‘product/product’);

    $this->data['breadcrumbs'][] = array(‘some breadcrumb’)
    $this->data['action'][] = array(‘some_value’);

    Now if we would like to load manufacturer language, so we can do this way :

    $lang = $this->language->load(‘product/manufacturer’);
    $this->data = array_merge($this->data, $lang);
    ..

    Hope this addition can help.

    • Qahar October 26, 2012 at 1:40 pm ·

      It’s still work even if you not use the array_merge(), but I know some people prefer to use it.

Leave a Comment

comm comm comm