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.
Related
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.
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);
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.
Yii's I18n topic isn't enough for me.
My source lang is Turkish , target lang is English (for example)
My test controller's index action :
public function actionIndex()
{
Yii::app()->language='en';
$this->render("index");
}
This is my view file's content :
echo Yii::t('test', 'Deneme');
And lastly, this is my protected/messages/en/test.php file's content:
return array(
'Deneme' => 'Example',
);
Everything OK, it's returning Example . But as you can see, i'm setting language manually on my index action. How can i do it automatically ? Must i add Yii::app()->language='en'; to all actions? How you are using l18n on your projects ?
Note : I'm Yii and l18n noob, so please describe step by step .
Thank you.
You should set the target language in CWebApplication:beginRequest()
in protected/config/main.php, add:
'onBeginRequest' => array('MyApp', 'beginRequest')
In protected/components, create a file MyApp.php, and add this class:
class MyApp {
public static function beginRequest(CEvent $event) {
//set your language, theme, etc here
}
}
Remember to declare beginRequest() as static, or you will encounter errors like this:
https://github.com/yiisoft/yii/issues/794
it's fairly simple. You do all language translations as you said. Then, in the parent controller, in the init method, yo can check the desired language and set the current language. That way, you don't have to do that in every action, just once.
in the Yii's tutorials there is an article that has explained it very good.
in this way you have 3 file: one, your language selector, one, language selector's widget and one is an behavior for handling your language selector file.
read here and use it...
Manage (Target) Language in Multilingual Applications + A Language Selector Widget (i18n)
I've used codeigniter in the past but on my current project I'm making the switch to Kohana. What is the best practice on constants?
In codeigniter there is the actual constants.php, but going through Kohana's source I'm not seeing something similar.
Never used kohana, but after a quick googling I find that you can use the config API to create your own config that will house the constants you need.
This thread suggests that if you are storing database sensitive items, to place them in the database.php config, etc.. making them relative to the type of data they are storing.
I'm familiar with Kohana, but not CI so much, so I'm guessing a bit to what you mean by 'constants.' I believe the closest thing to this is indeed Kohana's config API. So, if you wanted to make templates aware of some site-wide constant like your site name, that's a great thing to use the config API for.
To accomplish this, you'll need to create a config file under your /config folder, probably in the /application directory. What you call it isn't very important, but since it contains site information, let's call it site.php.
To quickly get going, here is what you'll want to have in that file:
<?php defined('SYSPATH') or die('No direct script access.');
return array(
// Your site name!
'name' => 'Oh me, Oh my',
);
Now, you can bring this in to a template by doing something like:
A better way to do this (using dumb templating) would be to assign this as a template variable in your Controller. So, assuming you have some default controller set up, the code would be:
public function action_index() {
$this->template->site_name = Kohana::config('site.name');
}
And then your template would have something like this:
<title><?php echo $site_name; ?></title>
Kohana's config API is interesting because it is hierarchical, meaning you can override and merge new configuration values on top of existing config structures. When you call Kohana::config('site.name'), the engine looks through all the config files named site.php, runs all of those config files and merges the results in to an array. The application-level config files will overwrite modules, which will overwrite system, etc... Then, based on that result array, Kohana will attempt to find the 'name' key and return it.
Assuming you want global constants...the following worked well for me.
application/config/constants.php:
define('MY_COOL_CONSTANT', 'foo');
return array();
index.php:
Kohana::$config->load('constants');
MY_COOL_CONSTANT should then be available globally.