I have been developing multilanguage website based on Symfony4. Structure of one of the tables:
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
id | int(11) | NO | PRI | NULL | auto_increment |
title_en | varchar(255) | NO | NULL | ||
title_fr | varchar(255) | NO | NULL | ||
title_de | varchar(255) | NO | NULL | ||
parent_id | int(11) | NO | NULL |
What is the optimal way to select appropriate column in template depending on user locale?
How bad will look the approach below to get needed entity field?
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Category
*
* @ORM\Table(name="category")
* @ORM\Entity
*/
class Category
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="title_en", type="string", length=255, nullable=false)
*/
private $titleEn;
/**
* @var string
*
* @ORM\Column(name="title_fr", type="string", length=255, nullable=false)
*/
private $titleFr;
/**
* @var string
*
* @ORM\Column(name="title_de", type="string", length=255, nullable=false)
*/
private $titleDe;
/**
* @var int
*
* @ORM\Column(name="parent_id", type="integer", nullable=false)
*/
private $parentId = '0';
public function getId(): ?int
{
return $this->id;
}
public function getLocalizedTitle(): ?string
{
$locale='title'.ucfirst($GLOBALS['request']->getLocale());
return $this->{$locale};
}
public function getTitleEn(): ?string
{
return $this->titleEn;
}
public function setTitleEn(string $titleEn): self
{
$this->titleEn = $titleEn;
return $this;
}
public function getTitleDe(): ?string
{
return $this->titleDe;
}
public function setTitleDe(string $titleDe): self
{
$this->titleDe = $titleDe;
return $this;
}
public function getParentId(): ?int
{
return $this->parentId;
}
public function setParentId(int $parentId): self
{
$this->parentId = $parentId;
return $this;
}
}
Then in view to get field we just use
{{ job.category.getLocalizedTitle }}
-
\$\begingroup\$ This code already works and suggests improvements based on better performance and readability \$\endgroup\$Yrtymd– Yrtymd2019年11月10日 19:09:27 +00:00Commented Nov 10, 2019 at 19:09
-
3\$\begingroup\$ I think this isn't a sustainable approach that will scale well. Consider keying your resources with an ID and a language code instead; that way adding support for a new language doesn't require schema + code changes, just the new data. \$\endgroup\$Mathieu Guindon– Mathieu Guindon2019年11月10日 19:17:31 +00:00Commented Nov 10, 2019 at 19:17
-
1\$\begingroup\$ @MathieuGuindon Something like a single JSON per language? \$\endgroup\$Mast– Mast ♦2019年11月12日 08:58:30 +00:00Commented Nov 12, 2019 at 8:58
-
\$\begingroup\$ @MathieuGuindon are you willing to turn your comment into an answer? As you likely are aware: short answers are okay \$\endgroup\$Sᴀᴍ Onᴇᴌᴀ– Sᴀᴍ Onᴇᴌᴀ ♦2022年07月06日 06:07:52 +00:00Commented Jul 6, 2022 at 6:07
-
1\$\begingroup\$ @SᴀᴍOnᴇᴌᴀ done! \$\endgroup\$Mathieu Guindon– Mathieu Guindon2022年07月06日 19:06:56 +00:00Commented Jul 6, 2022 at 19:06
1 Answer 1
I think this isn't a sustainable approach that will scale well. Consider keying your resources with an ID and a language code instead; that way adding support for a new language doesn't require schema + code changes, just the new data.
In other words you would have an instance of this entity per supported language, a property that can identify the language it's for, and only one title
property, without any language code suffix.