The common solution for your problem is to write validation functions for the variables. Such a function receives a variable as argument, checks its value and return either success or an error code. Then the calling code uses the error code to lookup the error message in the list of localized strings for the current language.
It could be like below:
The validation functions
function var1_is_valid($var1)
{
if ($var1 == 1 || $var1 == 2) {
return 'SUCCESS';
} else {
return 'ERR_VAR1_INVALID';
}
}
function var1_is_present($var1)
{
if ($var1 != '') {
return 'SUCCESS';
} else {
return 'ERR_VAR1_MISSING';
}
}
function var3_is_valid($var3)
{
if (0 <= $var3 && $var3 <= 10) {
return 'SUCCESS';
} else {
return 'ERR_VAR3_INVALID';
}
}
The language file(s)
// Use the strings returned by the validation functions as keys in the array
$lang = array(
'ERR_VAR1_INVALID' => 'var1 should be 1 or 2',
'ERR_VAR1_MISSING' => 'var1 is missing',
'ERR_VAR3_INVALID' => 'var3 should be between 0 and 10',
);
Even better, you can combine the functions var1_is_valid()
and var1_is_present()
into a single validation function for $var1
that returns either 'SUCCESS'
or the appropriate error string.
All the error messages for a language stay in a single language file that is loaded on each request. It works faster than querying the database for the error messages.
Another language means another file with strings identified by the same keys. You won't use two language on the same time. At most, you load the language that is completely implemented before loading the language requested by the user, in order to have a value for each string (a message in the wrong language is still better than nothing).