9

It there a way to read everything from within a language with Opencart?

At the moment I have to:

Controller
$this->load->language('help');          
$this->data['heading_title'] = $this->language->get('heading_title');    
$this->data['tab1'] = $this->language->get('tab1');

Language File

<?php
// Heading
$_['heading_title']      = 'Help';
$_['tab1'] = 'Account';    
?>
4
  • why you would like to do so ? this will pollute your $this->data array Commented Dec 22, 2011 at 11:20
  • Yes I thought of this and added everything to $this->data['language'] to prevent anything getting overwritten. Commented Dec 22, 2011 at 11:26
  • i think should be added to the core of opencart, in the controller class i think,or improving the language class it self Commented Dec 25, 2011 at 12:54
  • 3
    If you do this at the top of your controller, then it will not "pollute" your data array whatsoever. This is actually how a lot of developers do this these days, including myself Commented Dec 26, 2011 at 2:23

2 Answers 2

9

The easiest thing to do is to use array merge at the top of your controller

$this->data = array_merge($this->data, $this->language->load('language/file'));

or simply

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

Edit

For 2.x, use

$data = array_merge($this->data, $this->language->load('language/file'));

3.x does this automatically

Sign up to request clarification or add additional context in comments.

4 Comments

For anyone working in newer versions of OpenCart: Same method applies to 2.x by using $data (instead of $this->data). Behavior is automatic as of 3.x.
Thanks @Mavelo - I've updated the answer accordingly
@JayGilford You're welcome. In OC 2.x, I like using $data = $this->load->language('extension/module/customer_sale_popup'); as long as it's done before any other changes to $data :)
@Mavelo yeah I pretty much do that myself
0

In system/library/language.php is a function to get everything called all().

This is how to get a single item:

$var = $this->language->get('heading_title');   

This returns an array with all language entries:

$var = $this->language->all();

1 Comment

Note: Language::all() is not available in 1.5.x. Its use is discouraged in 2.x according to the "dont use" comment in the class' code. The function however persists in 3.x and has been used in the core in ControllerEventLanguage::before() so apparently it's here to stay :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.