CodeIgniter Forums
set language on the fly in Validation class [SOLVED] - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: set language on the fly in Validation class [SOLVED] (/showthread.php?tid=2627)



set language on the fly in Validation class [SOLVED] - El Forum - 08-15-2007

[eluser]Unknown[/eluser]
I'm looking for a way to change the language on the fly like
$this->validation->setLang("de") so that all language-dependent
strings automatically get read from the correct language-file.
I use the following snippet to add error-messages to my form-fields:
Code:
foreach($this->validation as $key => $value) {
  if(!is_object($key) && !is_array($key)) {    
    $this->template->setVar($key, $value);
  }
}
This makes handling err-msgs very convenient.
unfortunately only the default-language's error-strings get parsed into the templates even if i load the appropriate language file via:
$this->lang->load("validation", $this->getSessionVal(LANG));

it seems that the correct errormessages are only available then through
this->lang->line and not
$this->validation which makes the concept of validating a little more complicated.
any hints?


set language on the fly in Validation class [SOLVED] - El Forum - 08-15-2007

[eluser]Unknown[/eluser]
I guess the cleanest solution here is to extend the validation-class,
overwrite the run method with an optional language-parameter from my user-session and voila, the correct language file gets loaded.
Thanks to CI for the overloading possibility!


set language on the fly in Validation class [SOLVED] - El Forum - 08-21-2007

[eluser]mipa[/eluser]
Did it work out that way? Was that the best solution?

We're currently looking at the best way to switch the display language for the show_error(), show_404() methods in the Exception class. I'm leaning towards overloading the Exception class unless there's a better way.

I'd be interested to know what you came up with.


set language on the fly in Validation class [SOLVED] - El Forum - 08-21-2007

[eluser]Michael Wales[/eluser]
mipa: Couldn't you just do the following, where db_error is a line within your language file?
Code:
<? show_error($this->lang->line('db_error'); ?>



set language on the fly in Validation class [SOLVED] - El Forum - 08-22-2007

[eluser]mipa[/eluser]
D'oh!

Thanks for suggestion, Michael. It seems sometimes we overlook the most obvious solutions, expecting something more complicated.


set language on the fly in Validation class [SOLVED] - El Forum - 08-22-2007

[eluser]mipa[/eluser]
After some thought, I realize that your solution works in cases where we trigger an error in our own controllers. But how about in the case of errors that are triggered by CodeIgniter? How can we define the error message language then?