Skip to content

Commit

Permalink
API Update translation to use symfony 6
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Sep 7, 2022
1 parent de4d0b7 commit 97e5369
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 13 deletions.
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"symfony/cache": "^6.1",
"symfony/config": "^6.1",
"symfony/filesystem": "^6.1",
"symfony/translation": "^4.4.44",
"symfony/translation": "^6.1",
"symfony/translation-contracts": "^3.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

0 comments on commit 97e5369

Please sign in to comment.