Welcome Guest, Not a member yet? Register   Sign In
Multiple language Support
#1

Hi All,

I would like to know what is the recommended best practice for dealing with multiple languages and setting the default language as I seem to be doubling up on code.

Presently in each of my controllers functions I am loading the required language files and each individual line required.

I am thinking that I should move the loading of the language files to the autoloader but was wondering what others do to load each of the language lines required as it is making a mess of my controllers and would like to know if there may be a better way of doing this.

Any feedback in relation to this is greatly appreciated.
Reply
#2

(This post was last modified: 02-23-2015, 08:48 AM by RWCH.)

Do you need to load multiple languages at once?
Normally you should load only one language, and that is the user's language. You load it like this:

PHP Code:
$userLanguage $this->current_user->language
$this->lang->load('filename'$userLanguage); 

After that you get a translated string like this: $this->lang->line('HelloWorld');

See old documentation link: https://ellislab.com/codeIgniter/user-gu...guage.html

How does it make a mess of your controllers?

The language strings are only output and should not be in your controllers at all. Translating can be done in the view. If you want the strings to be translated before the view (which is better, because you do not want any logic in your views), you just pass them as data to your view. Something like this:

PHP Code:
$data = array(); //fill with whatever data you want
$data['language'] = $this->current_user->language;
$this->lang->load('filename'$data['language']); 
$data['resources']['HelloWorld'] = $this->lang->line('HelloWorld'); //add a translated string
$data['resources']['GoodbyeWorld'] = $this->lang->line('GoodbyeWorld'); //add a translated string

$this->load->view("some_page"$data); 

Cleaner would be to use a custom wrapper function like this:

PHP Code:
$data = array(); //fill with whatever data you want
$resources = array('HelloWorld''GoodbyeWorld');
$data['resources'] = GetTranslations($resources);
$this->load->view("some_page"$data); 

function 
GetTranslations($resources)
{
 $result = array();

 $userLanguage $this->current_user->language
 $this->lang->load('filename'$userLanguage); 

 foreach($resources as $resource)
 {
    $result[$resource] = $this->lang->line($resource);
 }
 return $result;


In your view you simple use:

Code:
<?php $resources['HelloWorld']; />

This is just one approach. Of course there are other ways of doing it.
Reply
#3

(This post was last modified: 02-24-2015, 04:14 PM by itconstruct.)

Hi RWCH,

Yes sorry I should have been clearer on that point I am only loading one language at a time.

Thanks for your response. I think that I would prefer to go down the custom wrapper path but I noticed that in the following code:

Quote:
PHP Code:
$this->lang->load('filename'$userLanguage); 

The 'filename' is static in the custom wrapper so I would pass this as a parameter to the function as I have more than one lang file per language.

In relation to following array to retrieve particular language strings for a particular page :
Quote:
PHP Code:
$resources = array('HelloWorld''GoodbyeWorld'); 


I was wondering if it would be more beneficial to autoload the language needed with all variables specified in my application in some way although I presume this would use up additional i/o, memory/swap and bloat the application. Please let me know your thoughts about this sort of method.

Additionally the custom wrapper function where should I put this. I don't have access to look at my code atm but at this stage I was thinking perhaps making it into a helper.

I will let you know how I go when I code this up tomorrow night but in the meantime I would be interested in your comments on the above. Thanks for your advice so far.
Reply
#4

Generally, you would want to get/set the current language in your base controller (usually MY_Controller), so you don't have to do anything special to deal with supporting the user's language. For example, assuming $this->current_user->language is the language 'idiom' for the current user:

Code:
if (isset($this->current_user->language)) {
                    $this->config->set_item('language', $this->current_user->language);
                    $this->session->set_userdata('language', $this->current_user->language);
                }

Then, when you call $this->lang->load('language_file_name'); it retrieves the language file for the current language (if available).
Reply
#5

(This post was last modified: 02-26-2015, 09:06 AM by RWCH.)

Hi itconstruct.

Quote:The 'filename' is static in the custom wrapper so I would pass this as a parameter to the function as I have more than one lang file per language.

Maybe you should just pass the language (and not the file name). Can't you construct the file names from the passed language? You should try to do this in a generic way.

Quote:I was wondering if it would be more beneficial to autoload the language needed with all variables specified in my application in some way although I presume this would use up additional i/o, memory/swap and bloat the application. Please let me know your thoughts about this sort of method.

You could. At some point you have to load the resource file and it does not really matter much if you do that with autoload or by just always loading it in your own base controller (usually MY_Controller). A matter of taste I guess. Autoload seems a little more cleaner.

Quote:Additionally the custom wrapper function where should I put this. I don't have access to look at my code atm but at this stage I was thinking perhaps making it into a helper.

I would go for a private method in your own base controller (usually MY_Controller). Loading the language resources has to be done once and with every page request and is a specific task which has to be executed by a controller (or indirect by a specified class, which seems overkill only for this task)

A helper file is simply a collection of functions in a particular category. They are not meant to do one specific task.  

Quote:Thanks for your advice so far.

Your welcome.

I would also like to see other people to comment on this. What I have written would be my approach, just based on my experience, but it is no way the preferred or the only way.
Reply
#6

(This post was last modified: 06-27-2015, 04:50 AM by itconstruct.)

Hi RWCH,

I know it has been a few months but I have updated my entire application to utilise the custom wrapper and it has made my code much easier to read and reduces repetitive code within my application so very happy with it so far. It has also made it quicker to write this part of my application.

Thank you to you both for your help so far.

I just have found one aspect of my application I hadn't changed over to utilise the custom wrapper which was the language variable function for populating my menu bar items and this function is currently residing in a model however my custom wrapper is in my base controller as a function.

Could you please let me know your thoughts on this.

My menu bar's utilise CSS and are text based links.
Reply
#7

Hi All,

An update I have moved that function from being in a model that needed to be loaded for each controller function needing it to be placed into the MY_Controller which removes the need to load that model for now as it was the only function and I can now just call it with the $this-> command.

I will look to see where else I can implement this code reuse.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB