Welcome Guest, Not a member yet? Register   Sign In
Load language file automatically
#1

[eluser]Assim[/eluser]
Here's my problem, I have a header and footer and my pages are using the template parser along with the other page fragements, and I'm also using the i18n library for internationalization, but the mainthing is there are some standard lines which all pages are using in the header and footer, how do I call these lines automatically and letting them get parsed. For the time being, I'm passing this in the controller:
Quote:$data['TextDirection'] = $this->lang->line('TextDirection');
$data['Left'] = $this->lang->line('Left');
$data['Right'] = $this->lang->line('Right');
$data['WebsiteName'] = $this->lang->line('WebsiteName');
$data['Font'] = $this->lang->line('Font');

I used something like this in the header view:
Quote:<html dir="{TextDirection}">
...
...

I tried using a pre_controller hook by putting them in a function called Language and made the hook call it. But I got one error with $this. Any solution?

I just want the above lines to be passed to my header and footer without rewriting them in every controller.

Does anybody know what to do?
#2

[eluser]cahva[/eluser]
MY_Controller would be the best way. Set default values to $this->data in your MY_Controller and you can use it later when outputting your layout.

Heres a rough example:
Code:
class MY_Controller Extends Controller {
    
    var $data;
    
    function __construct()
    {
        parent::__construct();
        
        $this->data['TextDirection'] = lang('TextDirection');
        $this->data['Left'] = lang('Left');
        $this->data['Right'] = lang('Right');
        $this->data['WebsiteName'] = lang('WebsiteName');
        $this->data['Font'] = lang('Font');
    }
}

// Then later in some controller
$this->load->view('template',$this->data);

EDIT:
BTW, if those are always the same(you dont need to change them in controllers), I would put those straight in to the view like:
Code:
<title><?php echo lang('title') ?></title>

Ofcourse you use parser so if you dont want ANY php tags in your views, then the first option.
#3

[eluser]Assim[/eluser]
It didn't work with me. I got this error:
Quote:Call to undefined function lang()

I don't know but maybe I don't know how to extend the core class. Sad
#4

[eluser]cahva[/eluser]
Load the language helper. You'll be using language class alot so you might just add it to autoload.
#5

[eluser]cahva[/eluser]
Extending is really easy. Just add that MY_Controller controller as MY_Controller.php to libraries folder and then in your controllers you extend MY_Controller instead of Controller.
#6

[eluser]Assim[/eluser]
[quote author="cahva" date="1266795024"]Load the language helper. You'll be using language class alot so you might just add it to autoload.[/quote]
That is already done. :#

[quote author="cahva" date="1266795125"]Extending is really easy. Just add that MY_Controller controller as MY_Controller.php to libraries folder and then in your controllers you extend MY_Controller instead of Controller.[/quote]
Done all that but now I got a few problems.

I only need those for the header and footer, so I will try with the header to test this out.

When using load view:
- <?php echo lang('something'); ?> doesn't work.
- $something also doesn't work. I get an error like "Undefined variable: something" although it's in MY_Controller.php

When using parser:
- {something} doesn't work and I get an error like "Invalid argument supplied for foreach()"

Is there any other way to do this? I don't mind if it was hard but at least the texts in my header and footer shouldn't be repeated in every controller.
#7

[eluser]cahva[/eluser]
Hmm.. Put some code (view and controller) so we can see whats wrong.

If "<?php echo lang('something'); ?>" doesnt work and gives you "Undefined variable: something" that means that the language file doesnt contain $lang['something'] = 'Something';
#8

[eluser]Assim[/eluser]
[quote author="cahva" date="1266806888"]Hmm.. Put some code (view and controller) so we can see whats wrong.

If "<?php echo lang('something'); ?>" doesnt work and gives you "Undefined variable: something" that means that the language file doesnt contain $lang['something'] = 'Something';[/quote]
I removed those and I'm just going to echo them in PHP, there's no need to use the parser which is quite complicated. So for now, everything is working fine.

Thanks for your help nonetheless!




Theme © iAndrew 2016 - Forum software by © MyBB