-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/Symfony/Component/Translation/Bridge/Phrase/.gitattributes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
vendor | ||
.phpunit.result.cache | ||
composer.lock |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CHANGELOG | ||
========= | ||
|
||
6.3 | ||
--- | ||
|
||
* Create the bridge |
86 changes: 86 additions & 0 deletions
86
src/Symfony/Component/Translation/Bridge/Phrase/Config/ReadConfig.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
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); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/Symfony/Component/Translation/Bridge/Phrase/Config/WriteConfig.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 inPhraseProvider
file?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.
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...
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.
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.
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.
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 workflowanyway, 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