All the web apps I've built support 3 languages, and are designed for more. I store all the texts in the database, and call them Constants.
create table constants (
id integer not null,
fk_constant_group integer not null
)
create table constants_group (
id integer not null,
name varchar(32)
)
create table languages (
id integer not null,
name varchar(32),
accronym varchar(3),
/*other fields if necessary*/
)
create table constants_value (
id integer not null,
fk_constant integer,
fk_language integer,
value text
)
The constant group is used to group them by module/page. If you are going to display a page you are going to need all the constants, so you use a single query to get all the data in the needed language for one page. An index on the group name would be appropriate.
In PHP I wrote something like this:
public static function SetNames()
{
$info=debug_backtrace(true);
$result=self::GetNames($info);
Factory::getSmarty()->assign("names",$result);
return $result;
}
public static function GetNames($info=false) /*$info is the debug_backtrace result*/
{
if(!$info)
$info=debug_backtrace(true);
$class=$info[1]['class']; /*It's the page name basically*/
if(isset(self::$m_names[$class]))
return self::$m_names[$class]; /*Using a static member for caching*/
global $application;
$langId=$application->langId;
$constants=AppFac::getAdo()->GetAll("SELECT
X.name as constant_name,
XV.value as constant_value
FROM
constants X
LEFT JOIN constants_values XV ON XV.fk_constant=X.id
LEFT JOIN constants_groups XG ON X.fk_constant_group=XG.id
WHERE
XG.name=?
AND XV.fk_language=?",array($class,$langId)); /*Parametrized query*/
$result=array();
foreach($constants as $constant) /*Make constants easily accessible*/
$result[$constant['constant_name']]=$constant['constant_value'];
self::$m_names[$class]=$result;
return $result;
}
From the PHP module, before fetching or displaying the template, call the SetNames()
method, which will automatically determine the name of the class you are calling from, and will use this name to find the constant group and fill in the constants in the language set in the session. After that use the constants from the template like this {$names.label_name}
for example, if you are looking for label_name
constants for your page.
This way is good because:
1. You can add multiple languages right from the Web interface you build for your app.
2. You can easily organize your texts when they are stored in the database.
3. You can easily add and edit texts right from the interface you build for your app.
If you are looking to translate whole pages instead of constants, you might use Smarty Resources, when you just include a resource in the template, and in PHP you define how to handle that resource.
http://www.smarty.net/manual/en/template.resources.php