Translate group names during serialization

closes #564
This commit is contained in:
Toby Zerner 2015-10-19 15:44:28 +10:30
parent a74c4ebd71
commit 1dd847bd36
2 changed files with 31 additions and 4 deletions

View File

@ -10,9 +10,9 @@
namespace Flarum\Api\Serializer;
use Flarum\Api\Serializer\AbstractSerializer;
use Flarum\Core\Group;
use InvalidArgumentException;
use Symfony\Component\Translation\TranslatorInterface;
class GroupSerializer extends AbstractSerializer
{
@ -21,6 +21,19 @@ class GroupSerializer extends AbstractSerializer
*/
protected $type = 'groups';
/**
* @var TranslatorInterface
*/
private $translator;
/**
* @param TranslatorInterface $translator
*/
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
/**
* {@inheritdoc}
*
@ -35,8 +48,8 @@ class GroupSerializer extends AbstractSerializer
}
return [
'nameSingular' => $group->name_singular,
'namePlural' => $group->name_plural,
'nameSingular' => $this->translateGroupName($group->name_singular),
'namePlural' => $this->translateGroupName($group->name_plural),
'color' => $group->color,
'icon' => $group->icon,
];
@ -49,4 +62,19 @@ class GroupSerializer extends AbstractSerializer
{
return $this->hasMany('Flarum\Api\Serializers\PermissionSerializer');
}
/**
* @param string $name
* @return string
*/
private function translateGroupName($name)
{
$translation = $this->translator->trans('core.group.'.strtolower($name));
if ($translation !== $name) {
return $translation;
}
return $name;
}
}

View File

@ -11,7 +11,6 @@
namespace Flarum\Core;
use Flarum\Core\Support\EventGeneratorTrait;
use Flarum\Core\Support\Locked;
use Flarum\Core\Support\ScopeVisibilityTrait;
use Flarum\Database\AbstractModel;
use Flarum\Event\GroupWasCreated;