support installation specific translations (#1099)

This commit is contained in:
Kevin Papst
2019-09-11 23:18:35 +02:00
committed by GitHub
parent 8c503a226e
commit 7f1a206a52
12 changed files with 349 additions and 16 deletions

View File

@@ -62,6 +62,16 @@ class AppExtension extends Extension
$container->setParameter('kimai.timesheet.rounding', $config['timesheet']['rounding']);
$this->setLdapParameter($config['ldap'], $container);
// translation files, which can overwrite the default kimai translations
$localTranslations = [];
if (null !== $config['theme']['branding']['translation']) {
$localTranslations[] = $config['theme']['branding']['translation'];
}
if (null !== $config['industry']['translation']) {
$localTranslations[] = $config['industry']['translation'];
}
$container->setParameter('kimai.i18n_domains', $localTranslations);
}
protected function setLdapParameter(array $config, ContainerBuilder $container)

View File

@@ -58,6 +58,7 @@ class Configuration implements ConfigurationInterface
->append($this->getLanguagesNode())
->append($this->getCalendarNode())
->append($this->getThemeNode())
->append($this->getIndustryNode())
->append($this->getDashboardNode())
->append($this->getWidgetsNode())
->append($this->getDefaultsNode())
@@ -344,6 +345,9 @@ class Configuration implements ConfigurationInterface
->scalarNode('title')
->defaultNull()
->end()
->scalarNode('translation')
->defaultNull()
->end()
->end()
->end()
->integerNode('autocomplete_chars')
@@ -355,6 +359,22 @@ class Configuration implements ConfigurationInterface
return $node;
}
protected function getIndustryNode()
{
$builder = new TreeBuilder('industry');
/** @var ArrayNodeDefinition $rootNode */
$node = $builder->getRootNode();
$node
->addDefaultsIfNotSet()
->children()
->scalarNode('translation')->defaultNull()->end()
->end()
;
return $node;
}
protected function getUserNode()
{
$builder = new TreeBuilder('user');

View File

@@ -340,11 +340,20 @@ class User extends BaseUser implements UserInterface
return $this->hasRole(static::ROLE_ADMIN);
}
public function getDisplayName(): ?string
{
if (!empty($this->getAlias())) {
return $this->getAlias();
}
return $this->getUsername();
}
/**
* @return string
*/
public function __toString()
{
return $this->getAlias() ?: $this->getUsername();
return $this->getDisplayName();
}
}

View File

@@ -31,11 +31,7 @@ class UserType extends AbstractType
'class' => User::class,
'label' => 'label.user',
'choice_label' => function (User $user) {
if (!empty($user->getAlias())) {
return $user->getAlias() . ' (' . $user->getUsername() . ')';
}
return $user->getUsername();
return $user->getDisplayName();
},
]);

128
src/Utils/Translator.php Normal file
View File

@@ -0,0 +1,128 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Utils;
use Symfony\Bundle\FrameworkBundle\Translation\Translator as BaseTranslator;
use Symfony\Component\Translation\Exception\InvalidArgumentException;
use Symfony\Component\Translation\MessageCatalogueInterface;
use Symfony\Component\Translation\TranslatorBagInterface;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Contracts\Translation\LocaleAwareInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Should be:
* class Translator implements TranslatorInterface, TranslatorBagInterface, LocaleAwareInterface
*
* But this is not compatible with PagerFanta yet.
*/
class Translator implements LegacyTranslatorInterface, TranslatorInterface, TranslatorBagInterface
{
/**
* @var BaseTranslator
*/
private $translator;
/**
* @var array
*/
private $localDomains = [];
public function __construct(BaseTranslator $translator, array $localDomains = [])
{
$this->translator = $translator;
$this->localDomains = $localDomains;
}
public function trans($id, array $parameters = [], $domain = 'messages', $locale = null)
{
if (null === $domain) {
$domain = 'messages';
}
foreach ($this->localDomains as $localDomain) {
if (false !== $this->hasLocalOverwrite($id, $localDomain, $locale)) {
$domain = $localDomain;
break;
}
}
return $this->translator->trans($id, $parameters, $domain, $locale);
}
protected function hasLocalOverwrite($id, $domain, $locale = null): bool
{
$found = false;
$catalogue = $this->getCatalogue($locale);
while (false === ($found = $catalogue->defines($id, $domain))) {
if ($cat = $catalogue->getFallbackCatalogue()) {
$catalogue = $cat;
} else {
break;
}
}
return $found;
}
/**
* Gets the catalogue by locale.
*
* @param string|null $locale The locale or null to use the default
*
* @return MessageCatalogueInterface
*
* @throws InvalidArgumentException If the locale contains invalid characters
*/
public function getCatalogue($locale = null)
{
return $this->translator->getCatalogue($locale);
}
/**
* Sets the current locale.
*
* @param string $locale The locale
*
* @throws \InvalidArgumentException If the locale contains invalid characters
*/
public function setLocale($locale)
{
$this->translator->setLocale($locale);
}
/**
* Returns the current locale.
*
* @return string The locale
*/
public function getLocale()
{
return $this->translator->getLocale();
}
/**
* Translates the given choice message by choosing a translation according to a number.
*
* @param string $id The message id (may also be an object that can be cast to string)
* @param int $number The number to use to find the index of the message
* @param array $parameters An array of parameters for the message
* @param string|null $domain The domain for the message or null to use the default
* @param string|null $locale The locale or null to use the default
*
* @return string The translated string
*
* @throws InvalidArgumentException If the locale contains invalid characters
*/
public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null)
{
return $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
}
}