-
-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support for symfony translations providers like loco #130
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,9 +34,22 @@ | |
use Symfony\Component\Config\ConfigCacheFactory; | ||
use Symfony\Component\Config\ConfigCacheFactoryInterface; | ||
use Symfony\Component\Translation\Loader\LoaderInterface; | ||
use Symfony\Component\Translation\Loader\XliffFileLoader; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
use Symfony\Component\Translation\Provider\ProviderFactoryInterface; | ||
use Symfony\Component\Translation\Provider\ProviderInterface; | ||
use Symfony\Component\Translation\Provider\TranslationProviderCollection; | ||
use Symfony\Component\Translation\Provider\TranslationProviderCollectionFactory; | ||
use Contributte\Translation\Dumpers\NeonFileDumper; | ||
use Symfony\Component\Translation\Writer\TranslationWriter; | ||
use Symfony\Component\Translation\Reader\TranslationReader; | ||
use Symfony\Component\Translation\Command\TranslationPushCommand; | ||
use Symfony\Component\Translation\Command\TranslationPullCommand; | ||
use Symfony\Component\Translation\Command\XliffLintCommand; | ||
use Symfony\Component\HttpClient\CurlHttpClient; | ||
use Tracy\IBarPanel; | ||
|
||
|
||
/** | ||
* @property stdClass $config | ||
*/ | ||
|
@@ -83,6 +96,7 @@ | |
'returnOriginalMessage' => Expect::bool()->default(true), | ||
'autowired' => Expect::type('bool|array')->default(true), | ||
'latteFactory' => Expect::string(ILatteFactory::class)->nullable(), | ||
'providers' => Expect::array()->default([]), | ||
]); | ||
} | ||
|
||
|
@@ -187,6 +201,9 @@ | |
$translator->setAutowired($autowired); | ||
} | ||
|
||
$reader = $builder->addDefinition($this->prefix('providerTranslationReader')) | ||
->setFactory(TranslationReader::class); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. coding style, missing one more tab |
||
|
||
// Loaders | ||
foreach ($this->config->loaders as $k1 => $v1) { | ||
$reflection = new ReflectionClass(DIHelpers::unwrapEntity($v1)); | ||
|
@@ -199,8 +216,76 @@ | |
->setFactory($v1); | ||
|
||
$translator->addSetup('addLoader', [$k1, $loader]); | ||
$reader->addSetup('addLoader', [$k1, $loader]); | ||
|
||
} | ||
|
||
|
||
$neonFileDumper = $builder->addDefinition($this->prefix('neonFileDumper')) | ||
->setFactory(NeonFileDumper::class); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cs |
||
|
||
$write = $builder->addDefinition($this->prefix('providerTranslationWriter')) | ||
->setFactory(TranslationWriter::class) | ||
->addSetup('addDumper', ['neon', $neonFileDumper]); | ||
|
||
// Symfony providers | ||
|
||
$builder->addDefinition($this->prefix('providerCurlHttpClient')) | ||
->setFactory(CurlHttpClient::class); | ||
Check failure on line 234 in src/DI/TranslationExtension.php GitHub Actions / Static analysis (phpstan)
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cs and more on other places |
||
|
||
$xlfLoader = $builder->addDefinition($this->prefix('loaderXLF')) | ||
->setFactory(XliffFileLoader::class); | ||
|
||
$providers = []; | ||
foreach ($this->config->providers as $k1 => $v1) { | ||
|
||
if (!isset($v1['provider'])) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing brackets |
||
throw new InvalidArgument('Missing provider parameter in definitions of providers.'); | ||
if (!isset($v1['dsn'])) | ||
throw new InvalidArgument('Missing DSN parameter in definitions of providers.'); | ||
|
||
$reflection = new ReflectionClass(DIHelpers::unwrapEntity($v1['provider'])); | ||
|
||
if (!$reflection->implementsInterface(ProviderFactoryInterface::class)) { | ||
throw new InvalidArgument('Provider must implement interface "' . ProviderFactoryInterface::class . '".'); | ||
|
||
} | ||
|
||
$provider = $builder->addDefinition($this->prefix('provider' . Strings::firstUpper($k1))) | ||
->setFactory($v1['provider'],['defaultLocale' => $this->config->locales->default, 'loader' => $xlfLoader]); | ||
|
||
$providers[$k1] = $provider; | ||
|
||
/*if (!isset($this->config->providers[$k1]['domains']) || empty($this->config->providers[$k1]['domains'])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why this? |
||
$this->config->providers[$k1]['domains'] = ['common']; | ||
}*/ | ||
|
||
} | ||
|
||
$builder->addDefinition($this->prefix('providerCollectionFatory')) | ||
->setFactory(TranslationProviderCollectionFactory::class, ["factories" => $providers, 'enabledLocales' => $this->config->locales->whitelist]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mixing single quotes and double quotes |
||
|
||
|
||
$providerCollections = $builder->addDefinition($this->prefix('providerCollection')) | ||
->setFactory('@'.$this->prefix('providerCollectionFatory').'::fromConfig', ['config' => $this->config->providers]); | ||
|
||
|
||
// Symfony commands | ||
$builder->addDefinition($this->prefix('translationPullCommand')) | ||
->setFactory(TranslationPullCommand::class, ['providerCollection' => $providerCollections, 'defaultLocale' => $this->config->locales->default, 'enabledLocales' => $this->config->locales->whitelist, 'transPaths' => $this->config->dirs]) | ||
->setAutowired(false); | ||
|
||
$builder->addDefinition($this->prefix('translationPushCommand')) | ||
->setFactory(TranslationPushCommand::class, ['providers' => $providerCollections, 'enabledLocales' => $this->config->locales->whitelist, 'transPaths' => $this->config->dirs]) | ||
->setAutowired(false); | ||
|
||
$builder->addDefinition($this->prefix('translationXliffLintCommand')) | ||
->setFactory(XliffLintCommand::class) | ||
->setAutowired(false); | ||
|
||
|
||
|
||
|
||
// Tracy\Panel | ||
if (!$this->config->debug || !$this->config->debugger) { | ||
return; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
/* | ||
* Nette neon dumper for contributte translation and symfony translation | ||
* | ||
* (c) Lukas Divacky <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
*/ | ||
|
||
namespace Contributte\Translation\Dumpers; | ||
|
||
use Symfony\Component\Translation\Exception\LogicException; | ||
use Symfony\Component\Translation\MessageCatalogue; | ||
use Symfony\Component\Translation\Util\ArrayConverter; | ||
use Symfony\Component\Translation\Dumper\FileDumper; | ||
use Symfony\Component\Yaml\Yaml; | ||
use Nette\Neon\Neon as NetteNeon; | ||
|
||
/** | ||
* NeonFileDumper generates yaml files from a message catalogue. | ||
* | ||
* @author Lukas Divacky <[email protected]> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need, your name will be in release notes |
||
*/ | ||
class NeonFileDumper extends FileDumper | ||
{ | ||
private string $extension; | ||
|
||
public function __construct(string $extension = 'neon') | ||
{ | ||
$this->extension = $extension; | ||
} | ||
|
||
public function formatCatalogue(MessageCatalogue $messages, string $domain, array $options = []): string | ||
Check failure on line 36 in src/Dumpers/NeonFileDumper.php GitHub Actions / Static analysis (phpstan)
Check failure on line 36 in src/Dumpers/NeonFileDumper.php GitHub Actions / Static analysis (--prefer-lowest, phpstan-lowest)
|
||
{ | ||
if (!class_exists(NetteNeon::class)) { | ||
throw new LogicException('Dumping translations in the neon format requires the Nette neon component.'); | ||
} | ||
|
||
$data = $messages->all($domain); | ||
|
||
$data = ArrayConverter::expandToTree($data); | ||
|
||
$neon = NetteNeon::encode($data, true); | ||
|
||
return $neon; | ||
|
||
} | ||
|
||
protected function getExtension(): string | ||
{ | ||
return $this->extension; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this dependency?