All my strings are kept in what i call a locale file. here is an example of a locale file:
Code: Select all
<?php
//English locale manager, load this file to have access to all english based formats and strings
setlocale(LC_ALL, 'en-CA', 'en', 'eng', 'english', 'ang', 'en-US');
function locale_date($timestamp){ if(!is_numeric($timestamp)){ $timestamp = strtotime($timestamp); } return strftime('%b %d %Y at %Hh%M', $timestamp); }
function locale_number($number, $decimals){ return number_format($number, $decimals, '.', ','); }
$strings = array(
'generic' => array(
'yes' => 'yes',
'no' => 'no',
'nodata' => 'No data available',
'delete' => 'Delete',
'view' => 'Show',
'edit' => 'Edit',
'new' => 'New',
'otherlanguage' => 'Français',
'back' => 'Back',
'submit' => 'Submit',
'cancel' => 'Cancel',
'notfound' => 'The requested information doesn\'t exist or was deleted in between two operations, please press BACK to go back to the previous page and start over.',
'operationsuccess' => 'Operation completed successfully',
'operationfailed' => 'An error occured while processing the requested operation',
'search' => 'Search',
'searchtitle' => 'Search :',
'validatedeleteoperation' => 'Please validate the attempted delete operation',
'datepicker' => '',
),
'langs' => array(
'1033' => 'English',
'3084' => 'French',
),
'adminrh' => array(
'title' => 'Job opening administration',
'menubutton' => 'Job openings',
'applicants' => 'Applicants: ',
'viewapplicants' => 'View applicants',
'title1033' => 'Title (English)',
'title3084' => 'Title (French)',
'desc1033' => 'Description (English)',
'desc3084' => 'Description (French)',
'invalidid' => 'Invalid identifier, you cannot fix this error unless you reload and page and lose changes',
'invalidtitle1033' => 'Invalid english title, must contain between 1 and 100 characters',
'invalidtitle3084' => 'Invalid french title, must contain between 1 and 100 characters',
'invaliddesc1033' => 'Invalid english description',
'invaliddesc3084' => 'Invalid french description',
'invalidfirstname' => 'You must enter your first name, must contain between 1 and 50 characters',
'invalidlastname' => 'You must enter your last name, must contain between 1 and 50 characters',
'invalidfile' => 'The file sent is invalid, if only PDF or DOC are accepted',
'invalidtelephones' => 'At least one of the phone numbers must be provided, must contain between 1 and 20 characters',
'appliedon' => ' applied on ',
'cellphone' => 'Telephone (Cell.): ',
'telephone' => 'Telephone (Home): ',
'email' => 'E-Mail: ',
'comment' => 'Comment: ',
'viewattachment' => 'Show attached CV',
'attachment' => 'Curriculum Vitae: ',
'firstname' => 'First name: ',
'lastname' => 'Last name: ',
'apply' => 'Apply for this position',
'applicationsaved' => 'You application has been saved. Please take note that only selected candidates will receive a callback.',
'notessaved' => 'Notes for the following application where saved: ',
'errorsavingnotes' => 'An unexpected error occured while saving the notes, this note was not saved: ',
'cvtype1' => 'Fill out an online job application form',
'cvtype2' => 'Send my own CV',
'cvtype3' => 'Do not attach any CV',
'cvnote' => 'You will be able to fill the online job application form after clicking on the "Submit" button.',
'infolabel' => 'Personal informations',
'firstname' => 'First name',
'lastname' => 'Last name',
'address' => 'Address',
'city' => 'City',
'region' => 'Province',
'postalcode' => 'Postal code',
'email' => 'E-Mail',
'tel1' => 'Telephone 1',
'tel2' => 'Telephone 2',
'drivingclass' => 'Driving permit class',
'displabel' => 'Availabilities',
'hasnaslabel' => 'I\'m allowed to work in canada?',
'hasnas' => 'Yes, i have a social security number (SSN)',
'languageslabel' => 'Languages',
'haswritten' => 'I write',
'hasspoken' => 'I speak',
'availabilitieslabel' => 'Availabilities',
'canpermanent' => 'I\'m expecting a full-time job',
'canparttime' => 'I\'m expecting a part-time job',
'canstudent' => 'I\'m expecting a summer job',
'disp' => 'I will be available from ',
'dispovertime' => 'I\'m available to do overtime',
'dispsaturday' => 'I\'m available to work week-ends',
'schoolinfo' => 'School information (From latest to oldest)',
'schoolinst' => 'Institution',
'schooltype' => 'Orientation',
'schoolfrom' => 'From',
'schoolto' => 'To',
'schooldiploma' => 'Diploma',
'jobinfo' => 'Work experience (From latest to oldest)',
'jobname' => 'Company',
'jobtel' => 'Telephone',
'jobfunc' => 'Function',
'jobfrom' => 'From',
'jobto' => 'To',
'abilitieslabel' => 'Certifications and abilities',
'hasoffice' => 'Office software',
'hasword' => 'I can use MS Office Word (or an equivalent)',
'hasexcel' => 'I can use MS Office Excel (or an equivalent)',
'hasaccess' => 'I can use MS Office Access (or an equivalent)',
'hasother' => 'Other software: ',
'hascert' => 'Certifications',
'certlift' => 'Lift',
'certbridgecrane' => 'Bridge crane',
'certhealth' => 'Health and safety',
'certother' => 'Other certification : ',
'certabilities' => 'Particular abilities (Precise)',
'invalidaddress' => 'Invalid address, must contain between 1 and 250 characters',
'invalidcity' => 'Invalid city, must contain between 1 and 100 characters',
'invalidregion' => 'Invalid province, must contain between 1 and 100 characters',
'invalidpostalcode' => 'Invalid postal code, must contain between 1 and 10 characters',
'alreadydone' => 'You have already submitted your CV for this job opening, it is not possible to change or start over, please apply again',
'invaliddisp' => 'Invalid availability date, must respect the YYYY-MM-DD format',
'invalidform1from' => 'Invalid "From" date at school experience #1, must respect the YYYY-MM format',
'invalidform2from' => 'Invalid "From" date at school experience #2, must respect the YYYY-MM format',
'invalidform3from' => 'Invalid "From" date at school experience #3, must respect the YYYY-MM format',
'invalidform1to' => 'Invalid "To" date at school experience #1, must respect the YYYY-MM format',
'invalidform2to' => 'Invalid "To" date at school experience #2, must respect the YYYY-MM format',
'invalidform3to' => 'Invalid "To" date at school experience #3, must respect the YYYY-MM format',
'invalidjob1from' => 'Invalid "From" date at work experience #1, must respect the YYYY-MM format',
'invalidjob2from' => 'Invalid "From" date at work experience #2, must respect the YYYY-MM format',
'invalidjob3from' => 'Invalid "From" date at work experience #3, must respect the YYYY-MM format',
'invalidjob1to' => 'Invalid "To" date at work experience #1, must respect the YYYY-MM format',
'invalidjob2to' => 'Invalid "To" date at work experience #2, must respect the YYYY-MM format',
'invalidjob3to' => 'Invalid "To" date at work experience #3, must respect the YYYY-MM format',
'viewcvattachment' => 'Show attached CV',
),
);
?>
And the code to load a locale file is the following:
Code: Select all
<?php
//Start the session
session_start();
//Validate the locale and load it
$validlocales = array(3084, 1033);
if(isset($_SESSION['locale']) && !in_array($_SESSION['locale'], $validlocales)){
$_SESSION['locale'] = $validlocales[0];
}elseif(!isset($_SESSION['locale'])){
$_SESSION['locale'] = $validlocales[0];
}
//Check if locale change requested
if(isset($_GET['locale']) && in_array($_GET['locale'], $validlocales)){
$_SESSION['locale'] = $_GET['locale'];
}
//Locale can also be detected via path, check if english or francais can be found in request
if(strpos($_SERVER['REQUEST_URI'], '/english/') !== false){
$_SESSION['locale'] = 1033;
}elseif(strpos($_SERVER['REQUEST_URI'], '/francais/')){
$_SESSION['locale'] = 3084;
}
//Load the locale
include('../assets/locale/'.$_SESSION['locale'].'.php');
//Setup alternate locale
$alternatelocale = ($_SESSION['locale'] == 1033 ? 3084 : 1033);
?>
This previous code i usually put it in my system.php file that i include on each page i work with...
The errors that gets pushed in the $errors array come from the $strings array defined in the locale file. And the errors are detected in the post using various validation techniques such as:
Code: Select all
//Analyse the posted information for errors
if(!isset($_POST['applicantid']) || !is_numeric($_POST['applicantid'])){ $errors[] = $strings['adminrh']['invalidid'].' (1)'; }
if(!isset($_POST['happlicantid']) || !is_string($_POST['happlicantid'])){ $errors[] = $strings['adminrh']['invalidid'].' (2)'; }
if(!isset($_POST['firstname']) || !is_string($_POST['firstname']) || strlen($_POST['firstname']) < 1 || strlen($_POST['firstname']) > 50){ $errors[] = $strings['adminrh']['invalidfirstname']; }
if(!isset($_POST['lastname']) || !is_string($_POST['lastname']) || strlen($_POST['lastname']) < 1 || strlen($_POST['lastname']) > 50){ $errors[] = $strings['adminrh']['invalidlastname']; }
if(!isset($_POST['address']) || !is_string($_POST['address']) || strlen($_POST['address']) < 1 || strlen($_POST['address']) > 250){ $errors[] = $strings['adminrh']['invalidaddress']; }
if(!isset($_POST['city']) || !is_string($_POST['city']) || strlen($_POST['city']) < 1 || strlen($_POST['city']) > 100){ $errors[] = $strings['adminrh']['invalidcity']; }
if(!isset($_POST['region']) || !is_string($_POST['region']) || strlen($_POST['region']) < 1 || strlen($_POST['region']) > 100){ $errors[] = $strings['adminrh']['invalidregion']; }
if(!isset($_POST['postalcode']) || !is_string($_POST['postalcode']) || strlen($_POST['postalcode']) < 1 || strlen($_POST['postalcode']) > 10){ $errors[] = $strings['adminrh']['invalidpostalcode']; }
if(!isset($_POST['tel1']) || !is_string($_POST['tel1']) || strlen($_POST['tel1']) < 1 || strlen($_POST['tel1']) > 20){ $tel1invalid = true; }
if(!isset($_POST['tel2']) || !is_string($_POST['tel2']) || strlen($_POST['tel2']) < 1 || strlen($_POST['tel2']) > 20){ $tel2invalid = true; }
//Analyse the dates if posted, no date is required, but if posted, it must have a correct format
if(isset($_POST['disp']) && !empty($_POST['disp']) && datefrom($_POST['disp'], 1970, 1, 1, true, true, true) == false){ $errors[] = $strings['adminrh']['invaliddisp']; }
if(isset($_POST['form1from']) && !empty($_POST['form1from']) && datefrom($_POST['form1from'], 1970, 1, 1, true, true, false) == false){ $errors[] = $strings['adminrh']['invalidform1from']; }
if(isset($_POST['form2from']) && !empty($_POST['form2from']) && datefrom($_POST['form2from'], 1970, 1, 1, true, true, false) == false){ $errors[] = $strings['adminrh']['invalidform2from']; }
if(isset($_POST['form3from']) && !empty($_POST['form3from']) && datefrom($_POST['form3from'], 1970, 1, 1, true, true, false) == false){ $errors[] = $strings['adminrh']['invalidform3from']; }
if(isset($_POST['form1to']) && !empty($_POST['form1to']) && datefrom($_POST['form1to'], 1970, 1, 1, true, true, false) == false){ $errors[] = $strings['adminrh']['invalidform1to']; }
if(isset($_POST['form2to']) && !empty($_POST['form2to']) && datefrom($_POST['form2to'], 1970, 1, 1, true, true, false) == false){ $errors[] = $strings['adminrh']['invalidform2to']; }
if(isset($_POST['form3to']) && !empty($_POST['form3to']) && datefrom($_POST['form3to'], 1970, 1, 1, true, true, false) == false){ $errors[] = $strings['adminrh']['invalidform3to']; }
if(isset($_POST['job1from']) && !empty($_POST['job1from']) && datefrom($_POST['job1from'], 1970, 1, 1, true, true, false) == false){ $errors[] = $strings['adminrh']['invalidjob1from']; }
if(isset($_POST['job2from']) && !empty($_POST['job2from']) && datefrom($_POST['job2from'], 1970, 1, 1, true, true, false) == false){ $errors[] = $strings['adminrh']['invalidjob2from']; }
if(isset($_POST['job3from']) && !empty($_POST['job3from']) && datefrom($_POST['job3from'], 1970, 1, 1, true, true, false) == false){ $errors[] = $strings['adminrh']['invalidjob3from']; }
if(isset($_POST['job1to']) && !empty($_POST['job1to']) && datefrom($_POST['job1to'], 1970, 1, 1, true, true, false) == false){ $errors[] = $strings['adminrh']['invalidjob1to']; }
if(isset($_POST['job2to']) && !empty($_POST['job2to']) && datefrom($_POST['job2to'], 1970, 1, 1, true, true, false) == false){ $errors[] = $strings['adminrh']['invalidjob2to']; }
if(isset($_POST['job3to']) && !empty($_POST['job3to']) && datefrom($_POST['job3to'], 1970, 1, 1, true, true, false) == false){ $errors[] = $strings['adminrh']['invalidjob3to']; }
Then, if there are errors in the $errors array, i simply do not process the mysql part and continue down the HTML portion and then i hit the section where it checks count($errors) and then displays the errors in the $errors array using implode().
Is that more clear?