set_language()

public function set_language(string $lang): void

Description

Sets the language for validation error messages. Changes both the session/cookie language (via the Language module) and loads the corresponding validation language file.

This method affects all subsequent validation error messages displayed by validation_errors() or display_errors().

Parameters

Parameter Type Description
$lang string The language code (e.g., 'en', 'fr', 'es', 'de').

Return Value

Type Description
void This method does not return a value.

Example Usage

PHP
// Set validation language to French
$this->validation->set_language('fr');

// Set language based on user preference
public function submit_form() {
    $user_lang = post('language', true) ?? 'en';
    $this->validation->set_language($user_lang);
    
    $this->validation->set_rules('email', 'Email', 'required|valid_email');
    // ... rest of validation
}

// Multi‑language application setup
public function init() {
    // Detect language from session, cookie, or default
    $lang = $this->language->get_language();
    $this->validation->set_language($lang);
}

Notes

  • Calls $this->language->set_language($lang) to update session and cookie.
  • Calls $this->model->load_validation_language($lang) to load the appropriate error‑message file.
  • Language files are located at modules/validation/language/{lang}/validation_errors.php.
  • Use reset_language() to revert to the system default.