mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-07-01 14:36:13 +00:00
create an helper + twig filter to show translatable string in current locale.
The twig filter is localize_translatable_string Example : {{ person.nationality|localize_translatable_string }} The helper may be called with $container->get('chill.main.helper.translatable_string'). The main function is ->localize(array $strings) Example: $container->get('chill.main.helper.translatable_string')->localize($country->getName()); #return the name in current locale close #299
This commit is contained in:
parent
234a3c9a5f
commit
57f2fa3178
@ -25,11 +25,20 @@ services:
|
||||
- "%locale%"
|
||||
tags:
|
||||
- { name: form.type, alias: translatable_string }
|
||||
|
||||
|
||||
chill.main.helper.translatable_string:
|
||||
class: Chill\MainBundle\Templating\TranslatableStringHelper
|
||||
arguments:
|
||||
- "@request_stack"
|
||||
|
||||
chill.main.twig.translatable_string:
|
||||
class: Chill\MainBundle\Templating\TranslatableStringTwig
|
||||
calls:
|
||||
- [ setContainer, ["@service_container"]]
|
||||
tags:
|
||||
- { name: twig.extension }
|
||||
|
||||
chill.main.form.type.select2choice:
|
||||
class: Chill\MainBundle\Form\Type\Select2ChoiceType
|
||||
tags:
|
||||
|
80
Templating/TranslatableStringHelper.php
Normal file
80
Templating/TranslatableStringHelper.php
Normal file
@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
|
||||
/*
|
||||
* Chill is a suite of a modules, Chill is a software for social workers
|
||||
* Copyright (C) 2014, Champs Libres Cooperative SCRLFS, <http://www.champs-libres.coop>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Templating;
|
||||
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
|
||||
/**
|
||||
*
|
||||
* This helper helps to find the string in current locale from translatable_strings
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*
|
||||
*/
|
||||
class TranslatableStringHelper
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var RequestStack
|
||||
*/
|
||||
private $requestStack;
|
||||
|
||||
public function __construct(RequestStack $requestStack)
|
||||
{
|
||||
$this->requestStack = $requestStack;
|
||||
}
|
||||
|
||||
/**
|
||||
* return the string in current locale if it exists.
|
||||
*
|
||||
* If it does not exists; return the name in the first language available.
|
||||
*
|
||||
* Return a blank string if any strings are available.
|
||||
* Return NULL if $translatableString is NULL
|
||||
*
|
||||
* @param array $translatableStrings
|
||||
* @return string
|
||||
*/
|
||||
public function localize(array $translatableStrings)
|
||||
{
|
||||
if (NULL === $translatableStrings) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
$language = $this->requestStack->getCurrentRequest()->getLocale();
|
||||
|
||||
|
||||
if (isset($translatableStrings[$language])) {
|
||||
return $translatableStrings[$language];
|
||||
} else {
|
||||
foreach ($translatableStrings as $string) {
|
||||
if (!empty($string)) {
|
||||
return $string;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
|
||||
}
|
||||
|
||||
}
|
32
Templating/TranslatableStringTwig.php
Normal file
32
Templating/TranslatableStringTwig.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
namespace Chill\MainBundle\Templating;
|
||||
|
||||
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
|
||||
|
||||
class TranslatableStringTwig extends \Twig_Extension
|
||||
{
|
||||
use ContainerAwareTrait;
|
||||
|
||||
/*
|
||||
* (non-PHPdoc)
|
||||
* @see Twig_Extension::getFilters()
|
||||
*/
|
||||
public function getFilters()
|
||||
{
|
||||
return array(
|
||||
new \Twig_SimpleFilter('localize_translatable_string', array($this, 'localize'))
|
||||
);
|
||||
}
|
||||
|
||||
public function localize(array $translatableStrings)
|
||||
{
|
||||
return $this->container->get('chill.main.helper.translatable_string')
|
||||
->localize($translatableStrings);
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return 'chill_main_localize';
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user