10000 feature #49231 [Translation] Phrase translation provider (wickedOne) · zim32/symfony@489c887 · GitHub
[go: up one dir, main page]

Skip to content

Commit 489c887

Browse files
committed
feature symfony#49231 [Translation] Phrase translation provider (wickedOne)
This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [Translation] Phrase translation provider | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix symfony#49142 | License | MIT | Doc PR | symfony/symfony-docs#17861 | Recipe PR | symfony/recipes#1172 Commits ------- b8f35ba [Translation] Phrase translation provider
2 parents 185941e + b8f35ba commit 489c887

File tree

18 files changed

+2421
-0
lines changed

18 files changed

+2421
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,6 +1552,7 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
15521552
TranslationBridge\Crowdin\CrowdinProviderFactory::class => 'translation.provider_factory.crowdin',
15531553
TranslationBridge\Loco\LocoProviderFactory::class => 'translation.provider_factory.loco',
15541554
TranslationBridge\Lokalise\LokaliseProviderFactory::class => 'translation.provider_factory.lokalise',
1555+
PhraseProviderFactory::class => 'translation.provider_factory.phrase',
15551556
];
15561557

15571558
$parentPackages = ['symfony/framework-bundle', 'symfony/translation', 'symfony/http-client'];

src/Symfony/Bundle/FrameworkBundle/Resources/config/translation_providers.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Translation\Bridge\Crowdin\CrowdinProviderFactory;
1515
use Symfony\Component\Translation\Bridge\Loco\LocoProviderFactory;
1616
use Symfony\Component\Translation\Bridge\Lokalise\LokaliseProviderFactory;
17+
use Symfony\Component\Translation\Bridge\Phrase\PhraseProviderFactory;
1718
use Symfony\Component\Translation\Provider\NullProviderFactory;
1819
use Symfony\Component\Translation\Provider\TranslationProviderCollection;
1920
use Symfony\Component\Translation\Provider\TranslationProviderCollectionFactory;
@@ -63,5 +64,16 @@
6364
service('translation.loader.xliff'),
6465
])
6566
->tag('translation.provider_factory')
67+
68+
->set('translation.provider_factory.phrase', PhraseProviderFactory::class)
69+
->args([
70+
service('http_client'),
71+
service('logger'),
72+
service('translation.loader.xliff'),
73+
service('translation.dumper.xliff'),
74+
service('cache.app'),
75+
param('kernel.default_locale'),
76+
])
77+
->tag('translation.provider_factory')
6678
;
6779
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.gitattributes export-ignore
4+
/.gitignore export-ignore
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor
2+
.phpunit.result.cache
3+
composer.lock
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
6.3
5+
---
6+
7+
* Create the bridge
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Translation\Bridge\Phrase\Config;
13+
14+
use Symfony\Component\Translation\Provider\Dsn;
15+
16+
/**
17+
* @author wicliff <wicliff.wolda@gmail.com>
18+
*/
19+
class ReadConfig
20+
{
21+
private const DEFAULTS = [
22+
'file_format' => 'symfony_xliff',
23+
'include_empty_translations' => '1',
24+
'tags' => [],
25+
'format_options' => [
26+
'enclose_in_cdata' => '1',
27+
],
28+
];
29+
30+
private function __construct(
31+
private array $options,
32+
private readonly bool $fallbackEnabled
33+
) {
34+
}
35+
36+
/**
37+
* @return $this
38+
*/
39+
public function setTag(string $tag): static
40+
{
41+
$this->options['tags'] = $tag;
42+
43+
return $this;
44+
}
45+
46+
public function isFallbackLocaleEnabled(): bool
47+
{
48+
return $this->fallbackEnabled;
49+
}
50+
51+
/**
52+
* @return $this
53+
*/
54+
public function setFallbackLocale(string $locale): static
55+
{
56+
$this->options['fallback_locale_id'] = $locale;
57+
58+
return $this;
59+
}
60+
61+
public function getOptions(): array
62+
{
63+
return $this->options;
64+
}
65+
66+
/**
67+
* @return $this
68+
*/
69+
public static function fromDsn(Dsn $dsn): static
70+
{
71+
$options = $dsn->getOptions()['read'] ?? [];
72+
73+
// enforce empty translations when fallback locale is enabled
74+
if (true === $fallbackLocale = filter_var($options['fallback_locale_enabled'] ?? false, \FILTER_VALIDATE_BOOL)) {
75+
$options['include_empty_translations'] = '1';
76+
}
77+
78+
unset($options['file_format'], $options['tags'], $options['tag'], $options['fallback_locale_id'], $options['fallback_locale_enabled']);
79+
80+
$options['format_options'] = array_merge(self::DEFAULTS['format_options'], $options['format_options'] ?? []);
81+
82+
$configOptions = array_merge(self::DEFAULTS, $options);
83+
84+
return new self($configOptions, $fallbackLocale);
85+
}
86+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Translation\Bridge\Phrase\Config;
13+
14+
use Symfony\Component\Translation\Provider\Dsn;
15+
16+
/**
17+
* @author wicliff <wicliff.wolda@gmail.com>
18+
*/
19+
class WriteConfig
20+
{
21+
private const DEFAULTS = [
22+
'file_format' => 'symfony_xliff',
23+
'update_translations' => '1',
24+
];
25+
26+
private function __construct(
27+
private array $options,
28+
) {
29+
}
30+
31+
/**
32+
* @return $this
33+
*/
34+
public function setTag(string $tag): static
35+
{
36+
$this->options['tags'] = $tag;
37+
38+
return $this;
39+
}
40+
41+
/**
42+
* @return $this
43+
*/
44+
public function setLocale(string $locale): static
45+
{
46+
$this->options['locale_id'] = $locale;
47+
48+
return $this;
49+
}
50+
51+
public function getOptions(): array
52+
{
53+
return $this->options;
54+
}
55+
56+
/**
57+
* @return $this
58+
*/
59+
public static function fromDsn(Dsn $dsn): static
60+
{
61+
$options = $dsn->getOptions()['write'] ?? [];
62+
63+
unset($options['file_format'], $options['tags'], $options['locale_id'], $options['file']);
64+
65+
$configOptions = array_merge(self::DEFAULTS, $options);
66+
67+
return new self($configOptions);
68+
}
69+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2023-present Fabien Potencier
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

0 commit comments

Comments
 (0)
0