diff options
author | Rick Ellis <rick.ellis@ellislab.com> | 2008-09-20 05:42:20 +0200 |
---|---|---|
committer | Rick Ellis <rick.ellis@ellislab.com> | 2008-09-20 05:42:20 +0200 |
commit | 277451a42c8fac9c4dfc8f4d4c54e0e7a19e0dc1 (patch) | |
tree | e9756955e86bda8d4e92892b917c6740d307daac | |
parent | 28c90a30caddf029aed48b30af097b7f26bb2b8d (diff) |
Added the ability to set translatable field names
-rw-r--r-- | system/libraries/Form_validation.php | 34 | ||||
-rw-r--r-- | user_guide/libraries/form_validation.html | 25 |
2 files changed, 53 insertions, 6 deletions
diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index c2749a0cf..1ba98bef4 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -504,11 +504,11 @@ class CI_Form_validation { }
else
{
- $line = $this->_error_messages['isset'];
+ $line = $this->_translate_fieldname('isset');
}
// Build the error message
- $message = sprintf($line, $row['label']);
+ $message = sprintf($line, $this->_translate_fieldname($row['label']));
// Save the error message
$this->_field_data[$row['field']]['error'] = $message;
@@ -646,11 +646,11 @@ class CI_Form_validation { }
else
{
- $line = $this->_error_messages[$rule];
+ $line = $this->_translate_fieldname($rule);
}
// Build the error message
- $message = sprintf($line, $row['label'], $param);
+ $message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
// Save the error message
$this->_field_data[$row['field']]['error'] = $message;
@@ -663,7 +663,31 @@ class CI_Form_validation { return;
}
}
- }
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Translate a field name
+ *
+ * @access private
+ * @param string the field name
+ * @return string
+ */
+ function _translate_fieldname($fieldname)
+ {
+ // Do we need to translate the field name?
+ // We look for the prefix lang: to determine this
+ if (substr($fieldname, 0, 5) == 'lang:')
+ {
+ $label = $this->CI->lang->line(substr($fieldname, 5));
+
+ // Were we able to translate the field name?
+ $fieldname = ($label === FALSE) ? substr($fieldname, 5) : $label;
+ }
+
+ return $fieldname;
+ }
// --------------------------------------------------------------------
diff --git a/user_guide/libraries/form_validation.html b/user_guide/libraries/form_validation.html index cd5bc2a81..f7fede674 100644 --- a/user_guide/libraries/form_validation.html +++ b/user_guide/libraries/form_validation.html @@ -77,6 +77,7 @@ have left the old class in the library so applications currently using it will n <li><a href="#callbacks">Callbacks</a></li>
<li><a href="#settingerrors">Setting Error Messages</a></li>
<li><a href="#errordelimiters">Changing the Error Delimiters</a></li>
+ <li><a href="#translatingfn">Translating Field Names</a></li>
<li><a href="#individualerrors">Showing Errors Individually</a></li>
<li><a href="#savingtoconfig">Saving Sets of Validation Rules to a Config File</a></li>
<li><a href="#arraysasfields">Using Arrays as Field Names</a></li>
@@ -296,7 +297,7 @@ at the same time. To set validation rules you will use the <dfn>set_rules()</dfn <ol>
<li>The field name - the exact name you've given the form field.</li>
- <li>A "human" name for this field, which will be inserted into the error message. For example, if your field is named "user" you might give it a human name of "Username".</li>
+ <li>A "human" name for this field, which will be inserted into the error message. For example, if your field is named "user" you might give it a human name of "Username". <strong>Note:</strong> If you would like the field name to be stored in a language file, please see <a href="#translatingfn">Translating Field Names</a>.</li>
<li>The validation rules for this form field.</li>
</ol>
@@ -590,6 +591,28 @@ it is assumed that the data is your newly processed form data.</p> +<a name="translatingfn"></a>
+<h2>Translating Field Names</h2>
+
+<p>If you would like to store the "human" name you passed to the <dfn>set_rules()</dfn> function in a language file, and therefore make the name able to be translated, here's how:</p>
+
+<p>First, prefix your "human" name with <dfn>lang:</dfn>, as in this example:</p>
+
+<code>
+$this->form_validation->set_rules('first_name', '<kbd>lang:</kbd>first_name', 'required');<br />
+</code>
+
+<p>Then, store the name in one of your language file arrays (without the prefix):</p>
+
+<code>$lang['first_name'] = 'First Name';</code>
+
+<p><strong>Note:</strong> If you store your array item in a language file that is not loaded automatically by CI, you'll need to remember to load it in your controller using:</p>
+
+<code>$this->lang->load('file_name');</code>
+
+<p>See the <a href="language.html">Language Class</a> page for more info regarding language files.</p>
+
+
<a name="errordelimiters"></a>
<h2>Changing the Error Delimiters</h2>
|