Skip to content

Move the TranslationLoader to the Translation component. #23666

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

Closed
wants to merge 8 commits into from
Closed
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
3 changes: 3 additions & 0 deletions UPGRADE-3.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ FrameworkBundle
* The `--no-prefix` option of the `translation:update` command is deprecated and
will be removed in 4.0. Use the `--prefix` option with an empty string as value
instead (e.g. `--prefix=""`)

* The class `Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader` was
moved to the Translation component (`Symfony\Component\Translation\Loader\TranslationLoader`)
Copy link
Member

Choose a reason for hiding this comment

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

You need to also add a note in the CHANGELOG for 4.0.


* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddCacheClearerPass`
class has been deprecated and will be removed in 4.0. Use the
Expand Down
3 changes: 3 additions & 0 deletions UPGRADE-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,9 @@ FrameworkBundle

* The `--no-prefix` option of the `translation:update` command has
been removed.

* The class `Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader` was
moved to the Translation component (`Symfony\Component\Translation\Loader\TranslationLoader`)

* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddCacheClearerPass` class has been removed.
Use the `Symfony\Component\HttpKernel\DependencyInjection\AddCacheClearerPass` class instead.
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ CHANGELOG
name as value, using it makes the command lazy
* Added `cache:pool:prune` command to allow manual stale cache item pruning of supported PSR-6 and PSR-16 cache pool
implementations
* Deprecated `Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader`, use
`Symfony\Component\Translation\Loader\TranslationLoader` instead

3.3.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Symfony\Bundle\FrameworkBundle\Command;

use Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader;
use Symfony\Component\Translation\Loader\TranslationLoader;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
<tag name="translation.extractor" alias="php" />
</service>

<service id="translation.loader" class="Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader" public="true" />
<service id="translation.loader" class="Symfony\Component\Translation\Loader\TranslationLoader" public="true" />

<service id="translation.extractor" class="Symfony\Component\Translation\Extractor\ChainExtractor" public="true" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private function getContainer($extractedMessages = array(), $loadedMessages = ar
})
);

$loader = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader')->getMock();
$loader = $this->getMockBuilder('Symfony\Component\Translation\Loader\TranslationLoader')->getMock();
$loader
->expects($this->any())
->method('loadMessages')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private function getContainer($extractedMessages = array(), $loadedMessages = ar
})
);

$loader = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader')->getMock();
$loader = $this->getMockBuilder('Symfony\Component\Translation\Loader\TranslationLoader')->getMock();
$loader
->expects($this->any())
->method('loadMessages')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,56 +11,11 @@

namespace Symfony\Bundle\FrameworkBundle\Translation;

use Symfony\Component\Finder\Finder;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Loader\LoaderInterface;
@trigger_error(sprintf('The class "%s" has been deprecated. Use "%s" instead. ', self::class, \Symfony\Component\Translation\Loader\TranslationLoader::class), E_USER_DEPRECATED);

/**
* TranslationLoader loads translation messages from translation files.
*
* @author Michel Salib <michelsalib@hotmail.com>
* @deprecated Class moved to Symfony\Component\Translation\Loader\TranslationLoader
*/
class TranslationLoader
class TranslationLoader extends \Symfony\Component\Translation\Loader\TranslationLoader
Copy link
Member

Choose a reason for hiding this comment

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

You should add a deprecation notice when this class is used.

Copy link
Member

Choose a reason for hiding this comment

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

And internal usages of this class should be changed to use the new class instead (which could lead to some composer dep changes in frameworkbundle probably)

{
/**
* Loaders used for import.
*
* @var array
*/
private $loaders = array();

/**
* Adds a loader to the translation extractor.
*
* @param string $format The format of the loader
* @param LoaderInterface $loader
*/
public function addLoader($format, LoaderInterface $loader)
{
$this->loaders[$format] = $loader;
}

/**
* Loads translation messages from a directory to the catalogue.
*
* @param string $directory the directory to look into
* @param MessageCatalogue $catalogue the catalogue
*/
public function loadMessages($directory, MessageCatalogue $catalogue)
{
if (!is_dir($directory)) {
return;
}

foreach ($this->loaders as $format => $loader) {
// load any existing translation files
$finder = new Finder();
$extension = $catalogue->getLocale().'.'.$format;
$files = $finder->files()->name('*.'.$extension)->in($directory);
foreach ($files as $file) {
$domain = substr($file->getFilename(), 0, -1 * strlen($extension) - 1);
$catalogue->addCatalogue($loader->load($file->getPathname(), $catalogue->getLocale(), $domain));
}
}
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/Translation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* Added `TranslationDumperPass`
* Added `TranslationExtractorPass`
* Added `TranslatorPass`
* Added class `Symfony\Component\Translation\Loader\TranslationLoader` from FrameworkBundle

3.2.0
-----
Expand Down
65 changes: 65 additions & 0 deletions src/Symfony/Component/Translation/Loader/TranslationLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?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\Loader;

use Symfony\Component\Finder\Finder;
use Symfony\Component\Translation\MessageCatalogue;

/**
* TranslationLoader loads translation messages from translation files.
*
* @author Michel Salib <michelsalib@hotmail.com>
*/
class TranslationLoader
{
/**
* Loaders used for import.
*
* @var array
*/
private $loaders = array();

/**
* Adds a loader to the translation extractor.
*
* @param string $format The format of the loader
* @param LoaderInterface $loader
*/
public function addLoader($format, LoaderInterface $loader)
{
$this->loaders[$format] = $loader;
}

/**
* Loads translation messages from a directory to the catalogue.
*
* @param string $directory the directory to look into
* @param MessageCatalogue $catalogue the catalogue
*/
public function loadMessages($directory, MessageCatalogue $catalogue)
{
if (!is_dir($directory)) {
return;
}

foreach ($this->loaders as $format => $loader) {
// load any existing translation files
$finder = new Finder();
$extension = $catalogue->getLocale().'.'.$format;
$files = $finder->files()->name('*.'.$extension)->in($directory);
Copy link
Member

Choose a reason for hiding this comment

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

I would drop the finder requirement for an iterator.

Copy link
Member

Choose a reason for hiding this comment

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

Already pointed by @javiereguiluz but @Nyholm said it would need the Finder dep anyway for some other changes he wants to make. So, probably good as is.

foreach ($files as $file) {
$domain = substr($file->getFilename(), 0, -1 * strlen($extension) - 1);
$catalogue->addCatalogue($loader->load($file->getPathname(), $catalogue->getLocale(), $domain));
}
}
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/Translation/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"symfony/dependency-injection": "~3.4|~4.0",
"symfony/intl": "^2.8.18|^3.2.5|~4.0",
"symfony/yaml": "~3.3|~4.0",
"symfony/finder": "~2.8|~3.0|~4.0",
"psr/log": "~1.0"
},
"conflict": {
Expand Down