Ben destek 3 dil inşa ettik, ve daha fazlası için tasarlanmış tüm web uygulamaları. Ben veritabanındaki tüm metinleri depolamak ve Sabitler onları arayın.
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;
}
PHP modülü, şablon getiriliyor ya da göstermeden önce, otomatik olarak aradığınız sınıfın adını belirleyecek SetNames()
yöntemini çağırın ve sürekli grubunu bulmak ve doldurmak için bu adı kullanacak dilinde sabitler oturumda ayarlayın. Bundan sonra böyle şablondan sabitleri kullanın {$names.label_name}
Örneğin, sayfanız için label_name
sabitleri arıyorsanız.
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