Welcome Guest, Not a member yet? Register   Sign In
Having problem with my language file
#1

I am trying to set my error messages I have a small problem.

On my library function set_error when I var dump my messages it says

Code:
file_extension_not_allowed_jpg


Library Function


PHP Code:
public function set_error($message) {
 
   
    $this
->CI->lang->load('upload''english');

 
   if (count($message) == 1) {
 
       
        $msg 
$this->CI->lang->line($message);

 
       $this->error_message $msg;

 
       var_dump($message);
 
       
    
} else {

 
       foreach ($message as $value) {
 
               
            $msg 
$this->CI->lang->line($value);

 
           $this->error_message $msg;
 
       }

 
   }
 
    
    return $this
;



What I am trying to do when a message has
_jpg

Then some how make the Lang line pick it up uisng something like %s

Code:
$lang['file_extension_not_allowed_' . '%s'] =  'Your %s files is not allowed!';

So will save me having to create a new lang line in my language file.


How my message is set


PHP Code:
public function multi_upload($field 'userfile') {

 
   if (empty($this->upload_path)) {
 
       $this->set_error('upload_path_not_set');
 
       return FALSE;
 
   

 
   if (!realpath(FCPATH $this->upload_path)) {
 
       $this->set_error('upload_path_in_correct');
 
       return FALSE;
 
   

 
   if (!empty($this->files[$field]['name'][0])) {

 
       foreach ($this->files[$field]['name'] as $key => $value) {

 
           $get_file_extension explode('.'$value);
 
           $get_file_extension_end strtolower(end($get_file_extension));
 
           $allowed_extension explode('|'$this->allowed_types);

 
           if (!in_array($get_file_extension_end$allowed_extension)) {
 
               $this->set_error('file_extension_not_allowed_' $get_file_extension_end);
 
               return FALSE;
 
           }

 
       }
 
   }



Any suggestion please if you have examples please show me.
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

The %s replacement you want to use is part of the sprintf PHP function. You could do something like this in your controller:

PHP Code:
public function multi_upload($field 'userfile') {

    
$this->load->helper('language');
 
   $this->lang->load('upload''english');

 
   ...

 
   if (!empty($this->files[$field]['name'][0])) {

 
       foreach ($this->files[$field]['name'] as $key => $value) {

 
           $get_file_extension     explode('.'$value);
 
           $get_file_extension_end strtolower(end($get_file_extension));
 
           $allowed_extension      explode('|'$this->allowed_types);

 
           if (!in_array($get_file_extension_end$allowed_extension)) {
 
               $errormsg sprintf(lang('file_extension_not_allowed'), $get_file_extension_end)
 
               $this->set_error($errormsg);
 
               return FALSE;
 
           }
 
       }
 
   }


And in your language file:

PHP Code:
$lang['file_extension_not_allowed'] =  'Your %s file is not allowed!'
Reply
#3

Thank you for that. I tried it it now picks up %s fine but when I need to use %d first it does not work.

It with %d it should be replaced with the file name the %s replaces correct but not the %d

PHP Code:
<?php

$lang
['upload_path_not_set'] = 'You have not set your upload path!';
$lang['upload_path_in_correct'] = 'Your upload path is incorrect!';
$lang['file_extension_not_allowed'] =  'Your file %d with the extension %s is not allowed!'



PHP Code:
public function set_error($message) {
 
   
    $this
->CI->lang->load('upload''english');

 
   $msg $this->CI->lang->line($message);

 
   if (strpos($msg'%s')) {

 
       echo $this->file_name;

 
       $line sprintf($msg$this->file_name$this->get_file_extension_end);

 
       echo $line;
 
               
    
} else {

 
       //$this->error_message = $msg;

 
   }

 
   return $this;



Out puts code below but when I echo $this->file_name for testing displays name.

PHP Code:
Your file 0 with the extension png is not allowed
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#4

You need to do the same for %d that you did for %s
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

'%s' means a string format, a '%d' can only replace a digit. In your case you want to replace 2 strings so you should use 2 '%s' signs.

PHP Code:
$lang['file_extension_not_allowed'] =  'Your file %s with the extension %s is not allowed!'
...
$line sprintf($msg$this->file_name$this->get_file_extension_end); 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB