CodeIgniter Forums
Use Language on Error Handling - show_error() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forum-6.html)
+--- Forum: CodeIgniter 3.x (https://forum.codeigniter.com/forum-17.html)
+--- Thread: Use Language on Error Handling - show_error() (/thread-77989.html)



Use Language on Error Handling - show_error() - nicolas33770 - 11-16-2020

Hi,

I need to use language on Error Handling - show_error() and show_404() but I don't find a way to do that. 

For exemple the file "system/core/Security.php" the function csrf_show_error() have the text error directly here. 

Is it a simple way to use my language file for this ?

I tried to load view on "application/views/errors/html/error_general.php" to use a specific header and footer view on this error but I can't. Do you know what I need to do that ?

Thanks for your help  Huh


RE: Use Language on Error Handling - show_error() - nicolas33770 - 11-21-2020

Any help !? ?


RE: Use Language on Error Handling - show_error() - superior - 11-21-2020

You can use the language helper function to define the different languages on errors.

PHP Code:
echo lang('language_key'); 

for more information see: https://codeigniter.com/userguide3/helpers/language_helper.html


RE: Use Language on Error Handling - show_error() - nicolas33770 - 11-23-2020

Thanks for this reply but it's not working. I got this error when I try to use this.

Form Security.php

Quote:public function csrf_show_error()
{
show_error(lang('message_error_403'), 403);
}


Quote:Type: Error

Message: Call to undefined function lang()

Filename: /var/www/gml-app/gml_system/core/Security.php

Line Number: 298

Backtrace:

File: /var/www/gml-app/index.php
Line: 310
Function: require_once

If I try have got a new error

Quote:public function csrf_show_error()
{
$this->lang->load('gml_general', 'french');
show_error(lang('message_error_403'), 403);
}


Quote:A PHP Error was encountered
Severity: Notice

Message: Undefined property: CI_Security::$lang

Filename: core/Security.php

Line Number: 298

Backtrace:

File: /var/www/gml-app/index.php
Line: 310
Function: require_once

An uncaught Exception was encountered
Type: Error

Message: Call to a member function load() on null

Filename: /var/www/gml-app/gml_system/core/Security.php

Line Number: 298

Backtrace:

File: /var/www/gml-app/index.php
Line: 310
Function: require_once



RE: Use Language on Error Handling - show_error() - superior - 11-23-2020

Helper doesn't seem to be loaded, you need to add this in your autoloader


RE: Use Language on Error Handling - show_error() - nicolas33770 - 11-23-2020

We can't load helper on /var/www/gml-app/gml_system/core/Security.php, it's not working. :-(


RE: Use Language on Error Handling - show_error() - superior - 11-23-2020

(11-23-2020, 07:59 AM)nicolas33770 Wrote: We can't load helper on /var/www/gml-app/gml_system/core/Security.php, it's not working. :-(

Yes you can, just override this class with a MY_ class.

Would be something like this, did not test this so feel free to improve
PHP Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

class 
MY_Security extends CI_Security
{
    public function 
__get($instance) {
        return 
get_instance()->$instance;
    }

    public function 
csrf_show_error() {
        
$this->load->helper('language');
        
$this->lang->load('gml_general''french');

        
show_errorlang'message_error_403' ), 403 );
    }




RE: Use Language on Error Handling - show_error() - nicolas33770 - 11-23-2020

Thanks for your help ! I just have 2 more question please :

- Where I put the MY_Security.php file ?

- Is it possible to improve your code to load my views/header_view.php ?


RE: Use Language on Error Handling - show_error() - superior - 11-23-2020

(11-23-2020, 08:21 AM)nicolas33770 Wrote: Thanks for your help ! I just have 2 more question please :

- Where I put the MY_Security.php file ?

- Is it possible to improve your code to load my views/header_view.php ?

Did you read the documentation about this?

- https://codeigniter.com/userguide3/general/core_classes.html


RE: Use Language on Error Handling - show_error() - nicolas33770 - 11-23-2020

I must admit that it seems to me quite complex especially if I have to create a specific and class page for each of the error pages (error_404, error_db, error_exception, error_general, ...)

I never indeed use these functions extension. I would just like to personalize all these pages simply.
I would just like to be able to load a view ( $this->load->view('header'); ) in "gml_application/views/errors/html/error_404.php" but it also doesn't work.

I guess it's because of CI_Controller not being loaded, I may not know how to load it either. It is all very technical.