• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] How to pass a whole language file?

#1
[eluser]Vortex23[/eluser]
I have 4 language files for different languages, and I need the whole file to be passed through.. I'm not sure if there is a function for this role (As this is my first day with CodeIgniter). If there is can someone show me a link to the function. I don't believe there is one, so what would be the ideal way?

Here is what I currently have.

Code:
<?php

class Login extends Controller
{

    public function __construct()
    {
        parent::Controller();
        // I know it's bad practice.. I haven't finished yet..
    }
    
    
    private function getLang()
    {
        $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
        if (!in_array($lang, array('en', 'es', 'de', 'fr')))
        {
            $lang = 'en';
        }
        //lang = 'es';
        return $lang;
    }
    
    public function index()
    {
        $data['base_url'] = $this->config->item('base_url');
        
        $this->lang->load('login', $this->getLang());
        $data['username'] = $this->lang->line('username');
        $data['password'] = $this->lang->line('password');
        $data['email'] = $this->lang->line('email');
        $data['register'] = $this->lang->line('register');
        
        $this->load->view('login_view', $data);
    }
    
}

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

Many Regards,
Vortex23

#2
[eluser]tomcode[/eluser]
If You just want to load ONE language file, Your code is - almost - correct.

CodeIgniter does NOT use the 'en', 'es', 'de', 'fr' strings but the names of the folders inside the language folder 'english', 'french', 'german', etc,
so You have to convert the 'en', 'es', 'de', 'fr' to the CI language folder names.

Code:
private function getLang($default = 'en')
{
    // haven't checked that line, let's suppose it does the job
    $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);

    $ci_lang_names = array('en' => 'english', 'fr' => 'french', 'de' => 'german', 'es' => 'spanish');

    if (!in_array($lang, array_keys($ci_lang_names)))
    {
        $lang = $default;
    }

    return $ci_lang_names[$lang];
}
In addition You can now pass the default language to the method Wink


If You intend to set the language site wide to the visitors browser language

You can proceed like this :

Code:
// instead of
// $this->lang->load('login', $this->getLang());

// using my code returns CI lang names
$lang = $this->getLang();

// from now on all language file loads use the visitors language
// so make sure to set the it BEFORE loading any language files
$this->config->set_item('language', $lang);

#3
[eluser]Vortex23[/eluser]
I have actually editted the CI language folder names to the first 2 chars.. Alot simpler personally..

Anyway.. I've tired your way and a few other ways and it failed to work..

Here is the code I currently have:
Code:
<?php

class Login extends Controller
{

    public function __construct()
    {
        parent::Controller();
    }
        
    private function getLang($lang = 'en')
    {    
        $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
        if (!in_array($lang, array('de','en', 'es', 'fr')))
        {
            $lang = 'en';
        }
        return $lang;
    }
    
    public function changeLang($lang = 'en')
    {
        if (!in_array($lang, array('de','en', 'es', 'fr')))
        {
            $lang = 'en';
        }
        
        $this->session->set_userdata('getLang', $lang);
        return redirect('login');
    }
    
    public function index()
    {
        $lang_sess = $this->session->userdata('getLang');
        if (!$lang_sess)
        {
            $this->session->set_userdata('getLang', $this->getLang());
        }
        
        $this->lang->load('login', $this->session->userdata('getLang'));
        

        // These lines are the problem, this is what I meant for passing the whole language file through,
        // Is there anyway to shorten it, I seperate my files like SMF, a language file for each language.
        // I need the whole contents of the file passed through so it can be displayed on the screen..
        $data['username'] = $this->lang->line('username');
        $data['password'] = $this->lang->line('password');
        $data['email'] = $this->lang->line('email');
        $data['register'] = $this->lang->line('register');
        
        $this->load->view('login_view', $data);
    }
    
    public function logout()
    {
        $this->session->sess_destroy();
        redirect('login');
    }
    
}

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

And:
Code:
<?php

echo '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
    &lt;head&gt;
        &lt;title&gt;Login&lt;/title&gt;
        &lt;meta http-equiv="Content-type" content="text/html;charset=UTF-8" /&gt;
        &lt;meta name="description" content="Meta Desc" /&gt;
        &lt;meta name="keywords" content="Key Words" /&gt;
        &lt;meta name="author" content="Vortex23" /&gt;
        &lt;link rel="stylesheet" href="',base_url(),'css/reset.css" type="text/css" /&gt;
        &lt;link rel="stylesheet" href="',base_url(),'css/960.css" type="text/css" /&gt;
        &lt;link rel="stylesheet" href="',base_url(),'css/login.css" type="text/css" /&gt;
    &lt;/head&gt;
    &lt;body&gt;
        <div class="container_12">
            <p>
            ';
            $langs = array('de', 'en', 'es', 'fr');
            foreach ($langs as $lang)
            {
                echo '<a href="',base_url(),'login/changeLang/',$lang,'"><img src="',base_url(),'images/',$lang,'_16x16.png" alt="" title="" /></a>&nbsp;&nbsp;';
            }
            echo '
            </p>
            <p>',$username,'</p>
            <p>',$password,'</p>
            <p>',$email,'</p>
            <p>',$register,'</p>
        </div>
    &lt;/body&gt;
&lt;/html&gt;';

?&gt;

#4
[eluser]tomcode[/eluser]
Ah, now I understand. No, I don't have a solution for fetching a whole language file.

I usually set them up in one array and do all manips by iterating it.


You can still do :

Code:
function fetch_lang_strings($path)
{
    include APPPATH .$path;

    return $lang;
}

#5
[eluser]Vortex23[/eluser]
Thanks for your reply, but just this second I solved it..

I foudn a nice plugin (here) created by Jérôme Jaglale which gives me alot less code on my login controller and saves me a bit of effort when adding new array keys in my language files.

Many Regards,
Vortex23


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.