Multilanguage node
From Achievo/ATK Wiki
|
ATK Howto: Multilanguage node
|
Contents |
Intro
This howto shows the implementation of a multilanguage ATK (meta)node. It provides the possibility to specify fields which should have language specific content. The language is based on the 2-character languagecode.
Configuration
In the siteroot/config.inc.php you can specify the languages you want to support:
$config_supported_languages = array("NL","EN","DE");
And the default language:
$config_defaultlanguage="NL";
Database
Create a database table as usual, but add a 'lang' column and add it to the PRIMARY KEY constraint, e.g.:
CREATE TABLE `news` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(100), `body` text, `is_published` tinyint(1) DEFAULT '0', `lang` char(2) NOT NULL, PRIMARY KEY (`id`,`lang`) );
Node
We use the following flags to create a multilanguage node:
- NF_ML (alias: NF_MULTILANGUAGE) - to enable the auto-multilanguage feature for this node
- AF_ML (alias: AF_MULTILANGUAGE) - to indicate which attribute is used to store the languagecode
class News extends atkMetaNode
{
protected $flags = NF_ML;
static public function meta(atkMetaPolicy $policy)
{
$policy->setType("title", "atkMlAttribute");
$policy->setType("body", "atkMlTextAttribute");
$policy->setType("is_published", "atkBoolAttribute");
$policy->add('lang', 'atkAttribute', array(), AF_MULTILANGUAGE|AF_HIDE);
}
}
Your node now shows a 'title' field in your default language. With the language-dropdown, you can switch the language of the second 'title' field.
When you add a new record, one record per language will be added to your table (so 3 in this case). The id's will be exactly the same, so that's why you need the combined PRIMARY KEY.