Change Language in Runtime - php

I'm trying to change the language of one webapp in realtime using codeigniter.
I follow the online documentation, create the folder for language2 with all the traductions, but when I do:
$this->config->set_item('language', 'portuguese');
It don't change the lang, the only way it works is changing the config file ex:
$config['language'] = "english";
But what I need is change in realtime not changing the config of the framework.
Regards,
Pedro

you can use this code. (this example for ion_auth)
$this->config->set_item('language', 'portuguese');
$this->lang->is_loaded = array();
$this->lang->load('ion_auth', 'portuguese');
this code used in constractor .
$this->lang->is_loaded = array();
top line empty laded languages.

Use hooks instead of class constructor, or even extend the Controller to MY_Controller and call in it's constructor.

Related

CodeIgniter getting CI load the correct language file when validating forms

This is rather a call for testimonies than a real questions. I would greatly appreciate feedbacks from CI experts and fans.
I have been searching the Internet for hours about this issue of mine: getting CI Form Validation class load the proper language file dynamically.
Let me explain. In my config.php file, I have:
$config['language'] = 'english';
Which is indeed the default language. But I have implemented a Settings controller which is letting my users set some values and of course change their default language. I could have stored that setting into a session variable but for the moment I don't, I just load that language user setting into each controller within the constructor :
$this->idiom = get_user_setting('language');
$this->lang->load('main', $this->idiom);
$this->lang->load('settings', $this->idiom);
$this->lang->load('cst', $this->idiom);
and as you can see I then load all the language files I need for each controller with the appropriate language. The 'get_user_setting' function is just a helper of mine querying the database to get a particular setting id.
I have copied the form_validation_lang.php from the /system/language/english/ directory and put it into my /application/language/french/ directory, and then I thought doing the following would do the magic:
$this->lang->load('form_validation', $this->idiom);
But nope... does not change anything. I took a look at the Form Validation class in the core folder and saw the following:
// Load the language file containing error messages
$this->CI->lang->load('form_validation');
THis clearly makes me think it will always load the file of the default language set in the config.php file. Am I right or wrong?
Hence, the only way I got to have this work with my user defined settings I fetch from the database (and which I could also store in a session variable), is to do the following:
$this->idiom = get_user_setting('language');
$this->config->set_item('language', $this->idiom);
...
I would greatly appreciate some feedbacks if some of you already had to cope with this kind of requirements and if you indeed managed that the same way I did or not. If I'm totally wrong, I'd appreciate solutions of course.
THanks a lot for your help.
Yes form_validation load default language file.If you want to load different language you can do a trick or change the source code of the Form validation class.
The trick I use, change the default language before using form validation(you can change back after form validation complete if you need.)
You can change default language with this code.
$this->config->set_item('language', 'YOUR_LANGUAGE');
After this code you can use form validation which will load the language you set.
Here is the way I solved it:
Create your own application\libraries\My_Form_validation.php.
Copy over the run() method from system\libraries\Form_validation.php into your file.
Change the following line in the run() method in your file from:
$this->CI->lang->load('form_validation');
To:
$language = !empty($this->CI->session->language) ? $this->CI->session->language : 'english';
$this->CI->lang->load('form_validation', $language);
This is assuming you are storing the language in the sessions. In your case you would do $language = get_user_setting('language'); I assume.

Changing the behaviour of view in Codeigniter

I am using codeigniter for a project that is used by a variety of companies.
The default version of our software is up and running and works fine - however some of our customers want slightly different view files for their instance of the system.
Ideally what I would like to do is set a variable (for example VIEW_SUFFIX) and whenever a view file is loaded it would first check if there was a suffix version available if there was use that instead.
For example if the system had a standard view file called 'my_view.php' but one client had a VIEW_SUFFIX of 'client_1' - whenever I called $this->load->view('my_view') if the VIEW_SUFFIX was set it would first check if my_view_client_1 existed (and if it did use that) or if not use the default my_view.php.
I hope that my question is clear enough... If anyone has done this before or can think of a way to do it I would really appreciate it.
EDIT:
Ideally I would like a solution that works without me changing every place that I am calling the view files. Firstly because there are a few files that may want different client versions and also because the view files are called from a lot of controllers
I had a similar requirement for which I created a helper function. Among other things, this function can check for a suffix before loading the specified view file. This function can check for the suffix and check if the file exists before loading it.
Unfortunately, the file checking logic would be a bit brittle. As an alternative, you can implement a MY_Loader class that will override the basic CI_Loader class.
Something like this in your application/core/MY_Loader.php:
class MY_Loader extends CI_Loader {
protected function _ci_load($_ci_data)
{
// Copy paste code from CI with your modifications to check prefix.
}
}
Could you not do this
// some method of creating $client
// probably created at login
$_SESSION['client'] = 'client_1';
$client = (isset($_SESSION['client'])) ? $_SESSION['client'] : '';
$this->load->view("your_view{$client}", $data);

Codeigniter Change loaded language

Currently i have a language loaded inside MY_Controller which extends CI_Controller. But inside a special page which controller (let's call it ABC controller) extends MY_Controller, I need to override the loaded language with another language. I tried loading another language inside this ABC controller, but unsuccessful. Is there a way to unload the loaded language and load another language?
an easier way is to reset the language data and is_loaded
$this->lang->is_loaded = array();
$this->lang->language = array();
I know it's a bit late to answer this but I think you can change the config item 'language' dynamically based on page requirement.
$this->config->set_item('language', 'chinese');
$this->config->set_item('language', 'english'); // based on the language folder of course holding language files
I had a requirement to send newsletters in users base lang, and this helped me change the language on the fly, hope this might help..
Have you tried just loading the language file you need?
$this->lang->load('filename', 'language');
It should be then accessible just like your default language. I haven't tested this tho, but from my understanding this should be the way to go about it.
Reference: http://codeigniter.com/user_guide/libraries/language.html
REVISED
I ended up digging a bit more for you, and found that you CANNOT load a default language (define it as default in your controller) and then later try to change it to something else.
Follow these steps:
If you need a language OTHER than english (default), set that in your config.
If you want to load ANOTHER language on a controller basis, you need to define that (most commonly in your constructor using something like session array / user selection.
You cannot load 2 languages (1 in the constructor, then another in a different class.. won't work!)
Reference here per forum posts: http://codeigniter.com/forums/viewthread/176223/
I encounter this problem and find a tricky solution.
$this->lang->load('text', 'english');
echo $this->lang->line('__YOUR_LANG_VARIABLE__');
//CI will record your lang file is loaded, unset it and then you will able to load another
//unset the lang file to allow the loading of another file
if(isset($this->lang->is_loaded)){
for($i=0; $i<=sizeof($this->lang->is_loaded); $i++){
unset($this->lang->is_loaded[$i]);
}
}
$this->lang->load('text', 'chinese');
echo $this->lang->line('__YOUR_LANG_VARIABLE__');
Hope it helps.
If you have any application installed built in codeigniter and you want add a language pack, just follow these steps:
Add language files in folder application/language/arabic
(I added arabic lang in sma2 built in ci)
Go to the file named setting.php
In application/modules/settings/views/setting.php you will find the array:
<div class="controls">
<?php /*
$lang = array (
'english' => 'English',
'arabic' => 'Arabic', // +++ Add this line
'spanish' => 'EspaƱol'
Now save and run the application.

Libraries from system folder does not load correct language

I have set two languages in codeigniter, it works fine in my libraries, models and other files.
I have two folders in application/language/ english and french, and I put in witch one form_validation_lang.php take from system/language
I use form_validation library from system whitch load the language file like this
$this->CI->lang->load('form_validation')
It not have the second parameter and it load just the english language file from application/language/english/ witch is default language.
How can I pass current language to Form_validation.php from system/libraries or how can I make this work?
Load it with second param which is actual language:
$this->lang->load('form_validation', $language);
// or like this if you get instance of CI
$this->CI->lang->load('form_validation', $language);
where $language is a variable holding currently used language.
I fixed it. In my controller I put this line
$this->lang->load('form_validation', $language);
before
$this->form_validation->set_rules (......)
and now it works.
You have right deczo, but you dont tell me where to put this line.

CodeIgniter - accessing $config variable in view

Pretty often I need to access $config variables in views.
I know I can pass them from controller to load->view().
But it seems excessive to do it explicitly.
Is there some way or trick to access $config variable from CI views without
disturbing controllers with spare code?
$this->config->item() works fine.
For example, if the config file contains $config['foo'] = 'bar'; then $this->config->item('foo') == 'bar'
Also, the Common function config_item() works pretty much everywhere throughout the CodeIgniter instance. Controllers, models, views, libraries, helpers, hooks, whatever.
You can do something like that:
$ci = get_instance(); // CI_Loader instance
$ci->load->config('email');
echo $ci->config->item('name');
$this->config->item('config_var') did not work for my case.
I could only use the config_item('config_var'); to echo variables in the view
Your controller should collect all the information from databases, configs, etc. There are many good reasons to stick to this. One good reason is that this will allow you to change the source of that information quite easily and not have to make any changes to your views.
This is how I did it. In config.php
$config['HTML_TITLE'] = "SO TITLE test";
In applications/view/header.php (assuming html code)
<title><?=$this->config->item("HTML_TITLE");?> </title>
Whenever I need to access config variables I tend to use: $this->config->config['variable_name'];
echo $this->config->config['ur config file']
If your config file also come to picture you have to access like this for example I include an app.php in config folder I have a variable
$config['50001'] = "your message"
Now I want access in my controller or model .
Try following two cases one should work
case1:
$msg = $this->config->item('ur config file');
echo $msg['50001']; //out put: "your message";
case2:
$msg = $this->config->item('50001');
echo $msg; //out put: "your message"
$config['cricket'] = 'bat'; in config.php file
$this->config->item('cricket') use this in view
If you are trying to accessing config variable into controller than use
$this->config->item('{variable name which you define into config}');
If you are trying to accessing the config variable into outside the controller(helper/hooks) then use
$mms = get_instance();
$mms->config->item('{variable which you define into config}');
Example, if you have:
$config['base_url'] = 'www.example.com'
set in your config.php then
echo base_url();
This works very well almost at every place.
/* Edit */
This might work for the latest versions of codeigniter (4 and above).

Categories