php 翻译 CodeIgniter 的表单验证错误消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9070227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Translating CodeIgniter's form validation error messages
提问by John
Is there anyway to translate CodeIgniter's form validation error messages without touching to system files?
有没有办法在不涉及系统文件的情况下翻译 CodeIgniter 的表单验证错误消息?
回答by Wes Crow
If you are talking about actually translating to another language, this can be done by setting the config value $config['language']
to the language you want. If you don't want to change the actual config.php file you can do it through the use of the Config object's set_item()
function like this:
如果您正在谈论实际翻译成另一种语言,这可以通过将配置值设置为$config['language']
您想要的语言来完成。如果您不想更改实际的 config.php 文件,您可以通过使用 Config 对象的set_item()
函数来完成,如下所示:
$this->config->set_item('language', 'spanish');
See: CodeIgniter Doc for the Config Class
This assumes that you have a spanish directory in your language
directory with at least the form_validation_lang.php
file.
这假设您的目录中有一个language
至少包含该form_validation_lang.php
文件的西班牙语目录。
However, if you are just wanting to create custom messages for the Form_validation object, you can copy the form_validation_lang.php
file from the system\language
directory and move it to the application\language
directory. You can now edit the new language file to make it reflect any different messages you want. You can also revert back to the default messages easily by removing the file from the application/language
directory.
但是,如果您只想为 Form_validation 对象创建自定义消息,则可以form_validation_lang.php
从system\language
目录中复制文件并将其移动到application\language
目录中。您现在可以编辑新语言文件以使其反映您想要的任何不同消息。您还可以通过从application/language
目录中删除文件来轻松恢复到默认消息。
Another way to do it, if you don't want to touch even the language
files is to manually override the messages. You can do that through the Form_validation
library object like so:
另一种方法是,如果您甚至不想触摸language
文件,则手动覆盖消息。您可以通过Form_validation
库对象来做到这一点,如下所示:
$this->form_validation->set_message('required', 'This is a required item!');`
回答by Nikunj Dhimar
If you need to set a custom error message for a particular field on some particular rule, use the set_rules() method:
如果您需要为某个特定规则的特定字段设置自定义错误消息,请使用 set_rules() 方法:
$this->form_validation->set_rules('field_name', 'Field Label', 'rule1|rule2|rule3',
array('rule2' => 'Error Message on rule2 for this field_name')
);
This will solve your all fields problem independently. :)
这将独立解决您的所有领域问题。:)