Skip to content

[Translation] Phrase translation provider #49231

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

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1552,6 +1552,7 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
TranslationBridge\Crowdin\CrowdinProviderFactory::class => 'translation.provider_factory.crowdin',
TranslationBridge\Loco\LocoProviderFactory::class => 'translation.provider_factory.loco',
TranslationBridge\Lokalise\LokaliseProviderFactory::class => 'translation.provider_factory.lokalise',
PhraseProviderFactory::class => 'translation.provider_factory.phrase',
];

$parentPackages = ['symfony/framework-bundle', 'symfony/translation', 'symfony/http-client'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Translation\Bridge\Crowdin\CrowdinProviderFactory;
use Symfony\Component\Translation\Bridge\Loco\LocoProviderFactory;
use Symfony\Component\Translation\Bridge\Lokalise\LokaliseProviderFactory;
use Symfony\Component\Translation\Bridge\Phrase\PhraseProviderFactory;
use Symfony\Component\Translation\Provider\NullProviderFactory;
use Symfony\Component\Translation\Provider\TranslationProviderCollection;
use Symfony\Component\Translation\Provider\TranslationProviderCollectionFactory;
Expand Down Expand Up @@ -63,5 +64,16 @@
service('translation.loader.xliff'),
])
->tag('translation.provider_factory')

->set('translation.provider_factory.phrase', PhraseProviderFactory::class)
->args([
service('http_client'),
service('logger'),
service('translation.loader.xliff'),
service('translation.dumper.xliff'),
service('cache.app'),
param('kernel.default_locale'),
])
->tag('translation.provider_factory')
;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
3 changes: 3 additions & 0 deletions src/Symfony/Component/Translation/Bridge/Phrase/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor
.phpunit.result.cache
composer.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CHANGELOG
=========

6.3
---

* Create the bridge
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Translation\Bridge\Phrase\Config;

use Symfony\Component\Translation\Provider\Dsn;

/**
* @author wicliff <wicliff.wolda@gmail.com>
*/
class ReadConfig
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this class (and the other one for write) is only used in PhraseProvider, I'm not convinced there is any advantage to have it standalone. Could you consider integrate it in PhraseProvider file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my main motivation was not to clutter the provider itself with configuration logic, bit similar to configuring the http client. also probably a bit easier for those who want to alter the config behaviour?

nevertheless, i could move all that logic to the provider class, but that would be quite a bit of a refactor for which i'm not sure i'll have time before the week's end. perhaps on friday which might not be in time...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to make the Provider fully extensible. The main goal is to allow users to push and pull translations from Phrase, with the same common internal API as other Translation Providers (Loco, Lokalise and Crowdin).

If there is something to be configured by the user for the Provider, it should pass via the DSN.

If I understand correctly the code, WriteConfig is just an array in which you adapt values like the domain or the locale. I think we must simplify it with something like a private property with default config (one for read and one for write if needed) to be tweaked right before each Phrase API call with the right values.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true, it's open source so thre's no "need" for anything to be extensible, but i know from experience you'll make a bunch of devs happy when they are 😄

correct, the config classes are not too complex and only the read config contains a bit of logic for the provider (the fallback_locale_enabled one), it's the unsetting of values to basically enforce some values which might not fit into someone's workflow

anyway, i'll have some time today to try and move the logic to the provider so will give it a go. think most time will be spent on re-writing the tests tbh

{
private const DEFAULTS = [
'file_format' => 'symfony_xliff',
'include_empty_translations' => '1',
'tags' => [],
'format_options' => [
'enclose_in_cdata' => '1',
],
];

private function __construct(
private array $options,
private readonly bool $fallbackEnabled
) {
}

/**
* @return $this
*/
public function setTag(string $tag): static
{
$this->options['tags'] = $tag;

return $this;
}

public function isFallbackLocaleEnabled(): bool
{
return $this->fallbackEnabled;
}

/**
* @return $this
*/
public function setFallbackLocale(string $locale): static
{
$this->options['fallback_locale_id'] = $locale;

return $this;
}

public function getOptions(): array
{
return $this->options;
}

/**
* @return $this
*/
public static function fromDsn(Dsn $dsn): static
{
$options = $dsn->getOptions()['read'] ?? [];

// enforce empty translations when fallback locale is enabled
if (true === $fallbackLocale = filter_var($options['fallback_locale_enabled'] ?? false, \FILTER_VALIDATE_BOOL)) {
$options['include_empty_translations'] = '1';
}

unset($options['file_format'], $options['tags'], $options['tag'], $options['fallback_locale_id'], $options['fallback_locale_enabled']);

$options['format_options'] = array_merge(self::DEFAULTS['format_options'], $options['format_options'] ?? []);

$configOptions = array_merge(self::DEFAULTS, $options);

return new self($configOptions, $fallbackLocale);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Translation\Bridge\Phrase\Config;

use Symfony\Component\Translation\Provider\Dsn;

/**
* @author wicliff <wicliff.wolda@gmail.com>
*/
class WriteConfig
{
private const DEFAULTS = [
'file_format' => 'symfony_xliff',
'update_translations' => '1',
];

private function __construct(
private array $options,
) {
}

/**
* @return $this
*/
public function setTag(string $tag): static
{
$this->options['tags'] = $tag;

return $this;
}

/**
* @return $this
*/
public function setLocale(string $locale): static
{
$this->options['locale_id'] = $locale;

return $this;
}

public function getOptions(): array
{
return $this->options;
}

/**
* @return $this
*/
public static function fromDsn(Dsn $dsn): static
{
$options = $dsn->getOptions()['write'] ?? [];

unset($options['file_format'], $options['tags'], $options['locale_id'], $options['file']);

$configOptions = array_merge(self::DEFAULTS, $options);

return new self($configOptions);
}
}
19 changes: 19 additions & 0 deletions src/Symfony/Component/Translation/Bridge/Phrase/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2023-present Fabien Potencier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Loading