OpenCart – Condensing Language Strings (Smart Coding)

This article will be about one of the many tricks involved in writing clean condensed code when doing your own customizations within the Opencart framework. Since Opencart is an MVC-L there will be Language files involved if you are writing your own pages to echo on the front end. You should never hard code text directly into a .tpl (template file) because one “it is messy” and two it makes it difficult to track down and change later on whether it is you or the customer doing it. The first thing you do is create your own language file in the correct language directory. The easiest way to do that is to open up an existing file and just SAVE AS and rename it to that folder. This way you already have strings there you can just replace with your own so if we were looking in for example: catalog/language/english/account/account.php it would look like this:

<?php
// Heading
$_['heading_title']      = ‘My Account';

// Text
$_['text_account']       = ‘Account';
$_['text_my_account']    = ‘My Account';
$_['text_my_orders']     = ‘My Orders';
$_['text_my_newsletter'] = ‘Newsletter';
$_['text_edit']          = ‘Edit your account information';
$_['text_password']      = ‘Change your password';
$_['text_address']       = ‘Modify your address book entries';
$_['text_wishlist']      = ‘Modify your wish list';
$_['text_order']         = ‘View your order history';
$_['text_download']      = ‘Downloads';
$_['text_reward']        = ‘Your Reward Points';
$_['text_return']        = ‘View your return requests';
$_['text_transaction']   = ‘Your Transactions';
$_['text_newsletter']    = ‘Subscribe / unsubscribe to newsletter';
?>

Now you can just replace the strings with your own text. Strings are the words between the ticks ”. So say we added 3 times the amount of what is already there then it would look like this:

<?php
// Heading
$_['heading_title']      = ‘My Account';

// Text
$_['text_account']       = ‘Account';
$_['text_my_account']    = ‘My Account';
$_['text_my_orders']     = ‘My Orders';
$_['text_my_newsletter'] = ‘Newsletter';
$_['text_edit']          = ‘Edit your account information';
$_['text_password']      = ‘Change your password';
$_['text_address']       = ‘Modify your address book entries';
$_['text_wishlist']      = ‘Modify your wish list';
$_['text_order']         = ‘View your order history';
$_['text_download']      = ‘Downloads';
$_['text_reward']        = ‘Your Reward Points';
$_['text_return']        = ‘View your return requests';
$_['text_transaction']   = ‘Your Transactions';
$_['text_newsletter']    = ‘Subscribe / unsubscribe to newsletter';
$_['text_my_account']    = ‘My Account';
$_['text_my_orders']     = ‘My Orders';
$_['text_my_newsletter'] = ‘Newsletter';
$_['text_edit']          = ‘Edit your account information';
$_['text_password']      = ‘Change your password';
$_['text_address']       = ‘Modify your address book entries';
$_['text_wishlist']      = ‘Modify your wish list';
$_['text_order']         = ‘View your order history';
$_['text_download']      = ‘Downloads';
$_['text_reward']        = ‘Your Reward Points';
$_['text_return']        = ‘View your return requests';
$_['text_transaction']   = ‘Your Transactions';
$_['text_newsletter']    = ‘Subscribe / unsubscribe to newsletter';
$_['text_my_account']    = ‘My Account';
$_['text_my_orders']     = ‘My Orders';
$_['text_my_newsletter'] = ‘Newsletter';
$_['text_edit']          = ‘Edit your account information';
$_['text_password']      = ‘Change your password';
$_['text_address']       = ‘Modify your address book entries';
$_['text_wishlist']      = ‘Modify your wish list';
$_['text_order']         = ‘View your order history';
$_['text_download']      = ‘Downloads';
$_['text_reward']        = ‘Your Reward Points';
$_['text_return']        = ‘View your return requests';
$_['text_transaction']   = ‘Your Transactions';
$_['text_newsletter']    = ‘Subscribe / unsubscribe to newsletter';

The language file is now complete. Now we need to open up its sister controller file which is found here: catalog/controller/account/account.php

Once in there you will see this line of code (which calls the language file):

$this->language->load(‘account/account’);

You then will see a control call for each language string which does two things; it sets up the variable to be echoed on the template file as the string, and in by doing so it grabs the language string from the language file. Those commands will look like this:

$this->data['text_my_account'] = $this->language->get(‘text_my_account’);
$this->data['text_my_orders'] = $this->language->get(‘text_my_orders’);
$this->data['text_my_newsletter'] = $this->language->get(‘text_my_newsletter’);
$this->data['text_edit'] = $this->language->get(‘text_edit’);
$this->data['text_password'] = $this->language->get(‘text_password’);
$this->data['text_address'] = $this->language->get(‘text_address’);
$this->data['text_wishlist'] = $this->language->get(‘text_wishlist’);
$this->data['text_order'] = $this->language->get(‘text_order’);
$this->data['text_download'] = $this->language->get(‘text_download’);
$this->data['text_reward'] = $this->language->get(‘text_reward’);
$this->data['text_return'] = $this->language->get(‘text_return’);
$this->data['text_transaction'] = $this->language->get(‘text_transaction’);
$this->data['text_newsletter'] = $this->language->get(‘text_newsletter’);

Instead of having ALL of those commands you can grab all of the language strings in one swoop. You’ll want to go back to this line $this->language->load(‘account/account’); and replace it with this command:

$this->data = array_merge($this->data, $this->load->language(‘account/account.php’));

That’s it. You have just saved a ton of space in your code. Just be sure that the strings you echo on your .tpl match the ones in the language file and you should be okay.

Looking for quality OpenCart Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , | Posted under OpenCart | RSS 2.0

Author Spotlight

Joe Stenhouse

Joe Stenhouse

I am a web application developer that specializes in PHP, JAVASCRIPT, MYSQL, HTML, and CSS. We manifest exciting potentials within the world wide web through means of innovation.

Leave a Reply

Your email address will not be published. Required fields are marked *


4 − 3 =

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>