From 9f09af4a1b2bd75540e0366aaa800ebf828b522f Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 22 Jul 2011 11:37:16 -0700 Subject: [PATCH] Add better language support to Validator. --- system/validator.php | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/system/validator.php b/system/validator.php index f92a1ce8..5d820e9e 100644 --- a/system/validator.php +++ b/system/validator.php @@ -30,6 +30,13 @@ class Validator { */ public $errors; + /** + * The language that should be used when retrieving error messages. + * + * @var string + */ + public $lang; + /** * The "size" related validation rules. * @@ -425,15 +432,15 @@ protected function get_message($attribute, $rule) } else { - $message = Lang::line('validation.'.$rule)->get(); + $message = Lang::line('validation.'.$rule)->get($this->language); // For "size" rules that are validating strings or files, we need to adjust // the default error message appropriately. if (in_array($rule, $this->size_rules) and ! is_numeric($this->attributes[$attribute])) { return (array_key_exists($attribute, $_FILES)) - ? rtrim($message, '.').' '.Lang::line('validation.kilobytes')->get().'.' - : rtrim($message, '.').' '.Lang::line('validation.characters')->get().'.'; + ? rtrim($message, '.').' '.Lang::line('validation.kilobytes')->get($this->language).'.' + : rtrim($message, '.').' '.Lang::line('validation.characters')->get($this->language).'.'; } return $message; @@ -451,7 +458,7 @@ protected function get_message($attribute, $rule) */ protected function format_message($message, $attribute, $rule, $parameters) { - $display = Lang::line('attributes.'.$attribute)->get(null, function() use ($attribute) { return str_replace('_', ' ', $attribute); }); + $display = Lang::line('attributes.'.$attribute)->get($this->language, function() use ($attribute) { return str_replace('_', ' ', $attribute); }); $message = str_replace(':attribute', $display, $message); @@ -504,4 +511,16 @@ protected function parse($rule) return array(is_numeric($colon) ? substr($rule, 0, $colon) : $rule, $parameters); } + /** + * Set the language that should be used when retrieving error messages. + * + * @param string $langauge + * @return Validator + */ + public function lang($language) + { + $this->language = $language; + return $this; + } + } \ No newline at end of file