I am building a php based mobile website that must be in both Spanish and English. This is my first bilingual site so I've been having to re-think a lot of coding practices I currently use to accommodate multiple languages.
I'm using a custom MVC pattern and currently I'm trying to use the same controller and model for both Spanish and English version of the site and just change the view for each language.
Since I'm using same model for both Spanish and English I have created an error system in the model that supports both languages see sample below.
The error list will be different depending on the model but I have 1 model with almost 30 errors and 60 translations. Its a big Form with a lot of input field validations.
Do you think it would be better to keep it as 1 large nested array or split the array into something like $english_errorids and $spanish_errorids
Open to any ideas you guys may have, it currently works as is I'm just trying to make sure I'm doing this the right way.
/**
* Created by PhpStorm.
* User: wmcgee
* Date: 11/19/14
* Time: 12:05 PM
*/
class SampleModel
{
#region Error System
/**
* List of all recorded errors
* @var array
*/
public $errors;
/**
* Can be set to either ENG or SPAN
* @var string
*/
public $lang = "ENG";
/**
* List of errors with translations
* @var array
*/
public $errorids = array(
10 => array('ENG'=>"Vehicle Condition Missing", 'SPAN'=>"Condicion de vehiculo falta"),
11 => array('ENG'=>"Signature Missing", 'SPAN'=>"Falta su firma "),
);
/**
* Records a translated error based on what lang is set and
* what error id was passed into recorded errors list.
*
* @param $id int
* @param bool $override if true id is treated as an
* error message and is recorded
* in error list.
*/
public function recordError($id, $override = false)
{
//- Allow manual error messages not in translation list to be added
if( $override ){
$this->errors[] = $id;
return;
}
$this->errors[] = $this->errorids[$id][$this->lang];
}
/**
* Returns errors formatted in HTML
* @return HTML
*/
public function getErrors()
{
if( sizeof($this->errors) == 0){ return ''; }
$msg = '<p class="errorBox"><b>Report Errors</b><br>';
foreach($this->errors as $error)
{
$msg .= "• " . $error . "<br>";
}
$msg .= "</p>";
return $msg;
}
#endregion
}
en.php
andsp.php
and justrequire_once
as per your need. \$\endgroup\$sprintf
placeholders if needed) and use a translator service. In your, just write$this->translate($data->something);
instead of<?= $data->something; ?>
and you're done \$\endgroup\$