Welcome Guest, Not a member yet? Register   Sign In
Use Language library/helper for multilingual content
#1

(This post was last modified: 08-27-2015, 04:17 AM by Lykos22.)

Hi all, I'd like to ask some questions, regarding the Language library or the Language helper , as I didn't have the chance to use it before.

Assuming that we have an About controller, standing for the about page, like this
PHP Code:
class About extends CI_Controller {
       
           var 
$data = array();

           public function 
index(){
                    
$this->data['page_title'] = 'About Us';
                    
$this->data['page_content'] = 'Some static content which doesn\'t come from database';

                    
$this->load->view('about'$this->data);            
           }


Is it possible to use the language class to create multilingual content for `page_title` and `page_content` depending on which language is selected, or is it better to store this content on different config files and select the appropriate one depending on the language selected from the url? If so how I could do that ?

I probably guess the url would be something like this
Code:
www.my_example.com/about/eng
www.my_example.com/about/fre
to detect the language, am I right?
Reply
#2

(This post was last modified: 08-27-2015, 12:47 PM by mwhitney.)

How you handle the language is up to you, but the "usual" method of doing it through the URL is to pass it as the first part of the URL, rather than at the end, but to do this with CI requires some other changes, which you can probably find on this forum already. Other choices would be to let the user choose the language as a preference or by selecting it on the page and saving it in the session.

If you really want to pass it at the end of the URL, you could probably catch it in your /application/config/routes.php and adjust your routes accordingly, or you would have to add a method to your controller for every language you wanted to support (which sounds painful to me, but if you got creative you could probably some creative code in MY_Controller to handle it).

Once you've figured out how to set the language choice, the easiest way to setup your site to use that choice is to create a base controller (usually called MY_Controller, because CodeIgniter automatically supports that name, assuming you haven't changed the subclass_prefix in /application/config/config.php) which gets the current language choice, sets the 'language' item in the config, then loads your application-wide language files:

PHP Code:
class MY_Controller extends CI_Controller {

    public function 
__construct()
    {
        
parent::__construct();

        
$language $this->getCurrentLanguage();
        if (! empty(
$language)) {
            
// Set the default language idiom used by lang()
            // and $this->lang->line() when an idiom is not supplied.
            
$this->config->set_item('language'$language);
        }

        
// Now you can load your language files.
        
$this->lang->load('application');
    }

    protected function 
getCurrentLanguage()
    {
        
// Get the current language idiom based on whatever makes sense 
        // for your site and return it.
        
return 'english';
    }


Then you modify your About controller to extend your base controller, and in the constructor you call the parent constructor, then load any controller-specific language files. After your language files are loaded, you can retrieve an language entries you need from them:

PHP Code:
class About extends MY_Controller {
       
    public 
$data = array();

    public function 
__construct()
    {
        
parent::__construct();

        
// Load a controller-specific language file.
        
$this->lang->load('about');
    }


    public function 
index(){
        
// Assuming you've loaded the language helper, otherwise 
        // use $this->lang->line() instead of lang()
        
$this->data['page_title'] = lang('about_index_page_title');
        
$this->data['page_content'] = lang('about_index_page_content');

        
$this->load->view('about'$this->data);
    }


While language files can be thought of as configuration files, they are stored in /application/language/{idiom}/, so the files loaded in my example code above would be:
/application/language/english/application_lang.php
/application/language/english/about_lang.php

If I changed the language to french, they would be:
/application/language/french/application_lang.php
/application/language/french/about_lang.php

I usually try to name my language keys to match the file they were loaded from, but you could re-use the same keys in different controllers, or even load different files for different methods in the same controller, if you wanted to do something like sharing a view between multiple controllers and reference the language keys directly, but have the actual content change based on the controller/method.

One thing to keep in mind is that the Lang class will only load the file if it exists for the current language. You would have to extend the Lang class if you wanted it to fallback to another language if the file (or key) wasn't found for the current language idiom.
Reply
#3

(This post was last modified: 08-28-2015, 01:37 AM by Lykos22.)

@mwhitney : Thank you for your reply.

Actually you were right, I was going to use the language as a 1st segment on my uri, my bad, thanks for mention it though. So basicly the url will look like this:

Code:
www.me-example.com/eng/about-us
www.my-example.com/eng/blog/post/this-is-a-post etc etc

But in order to do that I'm planning to use routes . Actually I have posted about this here. And also use this helper function for setting up properly my uris

PHP Code:
function set_lang($uri '') {
 
       
        $lang 
'eng';
 
       
        
// prepend the language to the uri string
 
       switch ($lang) {
 
           case 'fra':
 
               $url 'fra/' $uri;
 
           break;
 
           
            case 
'eng':
 
           default:
 
               $url 'eng/' $uri;
 
           break;
 
       }

 
       return $url ;
 
   
Reply




Theme © iAndrew 2016 - Forum software by © MyBB