Skip to content
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

API Update translation to use symfony 6 #10491

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
4 changes: 2 additions & 2 deletions _config/i18n.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ SilverStripe\Core\Injector\Injector:
constructor:
0: [ '%$Symfony\Component\Config\Resource\SelfCheckingResourceChecker' ]
# Create default translator with standard cache path and our custom loader
Symfony\Component\Translation\TranslatorInterface:
Symfony\Contracts\Translation\TranslatorInterface:
class: Symfony\Component\Translation\Translator
constructor:
0: 'en'
Expand All @@ -48,7 +48,7 @@ SilverStripe\Core\Injector\Injector:
SilverStripe\i18n\Messages\MessageProvider:
class: SilverStripe\i18n\Messages\Symfony\SymfonyMessageProvider
properties:
Translator: '%$Symfony\Component\Translation\TranslatorInterface'
Translator: '%$Symfony\Contracts\Translation\TranslatorInterface'
---
Name: textcollector
---
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"symfony/cache": "^6.1",
"symfony/config": "^6.1",
"symfony/filesystem": "^6.1",
"symfony/translation": "^4.4.44",
"symfony/translation": "^6.1",
"symfony/yaml": "^6.1",
"ext-ctype": "*",
"ext-dom": "*",
Expand Down
6 changes: 2 additions & 4 deletions src/i18n/Messages/Symfony/ModuleYamlLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

namespace SilverStripe\i18n\Messages\Symfony;

use SilverStripe\Core\Config\Configurable;
use SilverStripe\Dev\Debug;
use SilverStripe\i18n\i18n;
use SilverStripe\i18n\Messages\Reader;
use Symfony\Component\Translation\Loader\ArrayLoader;
use Symfony\Component\Translation\PluralizationRules;
use Symfony\Component\Translation\MessageCatalogue;

/**
* Loads yaml localisations across all modules simultaneously.
Expand All @@ -23,7 +21,7 @@ class ModuleYamlLoader extends ArrayLoader
*/
protected $reader = null;

public function load($resource, $locale, $domain = 'messages')
public function load(mixed $resource, string $locale, string $domain = 'messages'): MessageCatalogue
{
$messages = [];
foreach ($resource as $path) {
Expand Down
10 changes: 4 additions & 6 deletions src/i18n/Messages/Symfony/SymfonyMessageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,15 @@ public function pluralise($entity, $default, $injection, $count)
$this->load($locale);

// Prepare arguments
$arguments = $this->templateInjection(array_merge(
$injection,
[ 'count' => $count ]
));
$arguments = $this->templateInjection($injection);
$arguments['%count%'] = $count;

// Pass to symfony translator
$result = $this->getTranslator()->transChoice($entity, $count, $arguments, 'messages', $locale);
$result = $this->getTranslator()->trans($entity, $arguments, 'messages', $locale);

// Manually inject default if no translation found
if ($entity === $result) {
$result = $this->getTranslator()->transChoice($default, $count, $arguments, 'messages', $locale);
$result = $this->getTranslator()->trans($default, $arguments, 'messages', $locale);
}

return $result;
Expand Down