Welcome Guest, Not a member yet? Register   Sign In
Need help: Language files.
#1

[eluser]heavener[/eluser]
Hi everyone.

I am literally brand new with CodeIgniter. I am just trying to build my very first application and am having trouble with the Language files.

I need a file that I can load all of my global language keys in to, like the Site Name, repetitive sentences, etc., and then I'd like to be able to fetch the line of language from any view file.

I looked at this: http://ellislab.com/codeigniter/user-gui...guage.html
I found that it shows me how to create language files, and I understand how to load my language file, but how do I echo it on my view page?

Can someone help me with this? :question:

Thanks.
#2

[eluser]pistolPete[/eluser]
The user guide describes that as well:
Quote:Fetching a Line of Text

Once your desired language file is loaded you can access any line of text using this function:
Code:
$this->lang->line('language_key');

Where language_key is the array key corresponding to the line you wish to show.
Note: This function simply returns the line. It does not echo it for you.
#3

[eluser]heavener[/eluser]
Mhm, I know what you mean, and I've tried that. But none of my echo statements seem to work.
I'll show you what I mean...

Here is my controller:
Code:
<?php

class Home extends Controller {

    function Home()
    {
        parent::Controller();
    }

    function index()
    {
        $this->lang->load('glob', 'english');
        $this->lang->line('site_title');

        $this->load->view('home/home_header');
        $this->load->view('home/welcome_message');
        $this->load->view('home/home_footer');
    }
}

/* End of file home.php */
/* Location: ./system/application/controllers/home.php */

And now I know I'm supposed to echo it in my corresponding View file, so here's what I've tried:
Code:
<?=$lang['site_title']?>
And also I've tried doing this:
Code:
<?=$site_title?>

But those just don't work! I just get that typical PHP "Undefined variable" error.

What should I do?

Thanks.
#4

[eluser]pistolPete[/eluser]
You didn't assign the variable.
Code:
function index()
{
        $this->lang->load('glob', 'english');
        
        // assign the variable
         $data['site_title'] = $this->lang->line('site_title');

        // pass it to the view
        $this->load->view('home/home_header',$data);

        $this->load->view('home/welcome_message');
        $this->load->view('home/home_footer');
}

In your home_header view:
Code:
<?php echo $site_title; ?>

Have a look at the user guide: http://ellislab.com/codeigniter/user-gui...views.html
#5

[eluser]xwero[/eluser]
The best way to use language strings in your views is by loading the language helper and in the views you can do
Code:
<?php echo lang('site_title') ?>
#6

[eluser]heavener[/eluser]
Thanks all for the help! Big Grin
#7

[eluser]JulianM[/eluser]
Moreover, if you want to avoid write $this->lang->line each time, you can create a simple helper.

Good luck.

Julian


[quote author="pistolPete" date="1235837361"]The user guide describes that as well:
Quote:Fetching a Line of Text

Once your desired language file is loaded you can access any line of text using this function:
Code:
$this->lang->line('language_key');

Where language_key is the array key corresponding to the line you wish to show.
Note: This function simply returns the line. It does not echo it for you.
[/quote]
#8

[eluser]TheFuzzy0ne[/eluser]
Such a helper exists already. It's the [url="http://ellislab.com/codeigniter/user-guide/helpers/language_helper.html"]language helper[/url]. You'd use it like this:
Code:
<?php echo lang('site_title'); ?>

EDIT: Err... As xwero suggested...




Theme © iAndrew 2016 - Forum software by © MyBB