如何在CodeIgniter上翻译上传错误

时间:2012-06-18 15:38:41

标签: codeigniter translation

我正在使用CodeIgniter通过表单上传一些文件。我知道如何翻译“常规”表单错误(必需,valid_email等),但我不知道如何处理文件错误(文件不允许,文件太大等)。

我应该在

中使用哪些键
$this->form_validation->set_message('KEY', 'TRANSLATION');

由于

2 个答案:

答案 0 :(得分:4)

方法1

默认情况下,language文件通常存储在system/language目录中。或者,您可以在upload_lang.php文件夹中创建一个名为application/language的文件,并将其存储在那里。

$lang['upload_userfile_not_set']        = "Unable to find a post variable called userfile.";
$lang['upload_file_exceeds_limit']      = "The uploaded file exceeds the maximum allowed size in your PHP configuration file.";
$lang['upload_file_exceeds_form_limit'] = "The uploaded file exceeds the maximum size allowed by the submission form.";
$lang['upload_file_partial']            = "The file was only partially uploaded.";
$lang['upload_no_temp_directory']       = "The temporary folder is missing.";
$lang['upload_unable_to_write_file']    = "The file could not be written to disk.";
$lang['upload_stopped_by_extension']    = "The file upload was stopped by extension.";
$lang['upload_no_file_selected']        = "You did not select a file to upload.";
$lang['upload_invalid_filetype']        = "The filetype you are attempting to upload is not allowed.";
$lang['upload_invalid_filesize']        = "The file you are attempting to upload is larger than the permitted size.";
$lang['upload_invalid_dimensions']      = "The image you are attempting to upload exceedes the maximum height or width.";
$lang['upload_destination_error']       = "A problem was encountered while attempting to move the uploaded file to the final destination.";
$lang['upload_no_filepath']             = "The upload path does not appear to be valid.";
$lang['upload_no_file_types']           = "You have not specified any allowed file types.";
$lang['upload_bad_filename']            = "The file name you submitted already exists on the server.";
$lang['upload_not_writable']            = "The upload destination folder does not appear to be writable.";

方法2

您可以在上传过程后使用Flashdata

if ($this->upload->do_upload())
{
    $this->session->set_flashdata('success', 'Yep! Upload complete');
    redirect('go_back_home');
}
else
{
    $this->session->set_flashdata('error', 'Uh, upload not complete! The problem is..'); // Manually check
    redirect('go_back_form');
}

您还可以使用$this->upload->data()检查问题。 This is a helper function返回一个数组,其中包含与您上传的文件相关的所有数据。这是数组原型:

Array
(
    [file_name]    => mypic.jpg
    [file_type]    => image/jpeg
    [file_path]    => /path/to/your/upload/
    [full_path]    => /path/to/your/upload/jpg.jpg
    [raw_name]     => mypic
    [orig_name]    => mypic.jpg
    [client_name]  => mypic.jpg
    [file_ext]     => .jpg
    [file_size]    => 22.2
    [is_image]     => 1
    [image_width]  => 800
    [image_height] => 600
    [image_type]   => jpeg
    [image_size_str] => width="800" height="200"
)

答案 1 :(得分:-1)

试试这个:

lang:your_translation_key

一个真实的例子:

$this->form_validation->set_rules('name', 'lang:web_name', 'required|trim|xss_clean|min_length[2]|max_length[100]');