Skip to content

Commit

Permalink
link new translations to a File
Browse files Browse the repository at this point in the history
  • Loading branch information
Cédric Girard committed Jul 12, 2016
1 parent 7bf7b8d commit 4130eca
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 7 deletions.
3 changes: 3 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ public function getConfigTreeBuilder()
->booleanNode('create_missing')
->defaultFalse()
->end()
->scalarNode('file_format')
->defaultValue('yml')
->end()
->end()
->end()

Expand Down
1 change: 1 addition & 0 deletions DependencyInjection/LexikTranslationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('lexik_translation.auto_cache_clean', $config['auto_cache_clean']);
$container->setParameter('lexik_translation.dev_tools.enable', $config['dev_tools']['enable']);
$container->setParameter('lexik_translation.dev_tools.create_missing', $config['dev_tools']['create_missing']);
$container->setParameter('lexik_translation.dev_tools.file_format', $config['dev_tools']['file_format']);

$objectManager = isset($config['storage']['object_manager']) ? $config['storage']['object_manager'] : null;

Expand Down
6 changes: 5 additions & 1 deletion Manager/FileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ public function __construct(StorageInterface $storage, $rootDir)
/**
* {@inheritdoc}
*/
public function getFor($name, $path)
public function getFor($name, $path = null)
{
if (null === $path) {
$path = sprintf('%s/Resources/translations', $this->rootDir);
}

$hash = $this->generateHash($name, $this->getFileRelativePath($path));
$file = $this->storage->getFileByHash($hash);

Expand Down
3 changes: 2 additions & 1 deletion Manager/FileManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ public function create($name, $path, $flush = false);

/**
* Returns a translation file according to the given name and path.
* If path is null, app/Resources/translations will be used as default path.
*
* @param string $name
* @param string $path
* @return File
*/
public function getFor($name, $path);
public function getFor($name, $path = null);
}
4 changes: 2 additions & 2 deletions Manager/TransUnitManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ public function updateTranslationsContent(TransUnitInterface $transUnit, array $
protected function getTranslationFile(TransUnitInterface & $transUnit, $locale)
{
$file = null;
foreach ($transUnit->getTranslations() as $translationModel) {
if (null !== $file = $translationModel->getFile()) {
foreach ($transUnit->getTranslations() as $translation) {
if (null !== $file = $translation->getFile()) {
break;
}
}
Expand Down
4 changes: 4 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,15 @@

<service id="lexik_translation.data_grid.request_handler" class="%lexik_translation.data_grid.request_handler.class%">
<argument type="service" id="lexik_translation.trans_unit.manager" />
<argument type="service" id="lexik_translation.file.manager" />
<argument type="service" id="lexik_translation.translation_storage" />
<argument type="service" id="lexik_translation.locale.manager" />
<call method="setCreateMissing">
<argument>%lexik_translation.dev_tools.create_missing%</argument>
</call>
<call method="setDefaultFileFormat">
<argument>%lexik_translation.dev_tools.file_format%</argument>
</call>
</service>

<!-- Overview -->
Expand Down
3 changes: 3 additions & 0 deletions Resources/doc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,15 @@ From the translations grid you can get untranslated keys from a given Symfony pr
Note that the key must exist in the database to appear in the grid.
If you want to force the translations keys to appear in the grid you can enable the `create_missing` option.
If you do so, while getting missing translations from a profile, if a key/domain pair does not exist in the database the bundle will create it.
During this process some new keys and translations can be created in the database. Each new translation will be associated to a file according to the tanslation's domain and locale.
In this case the bundle will look for files in `app/Resources/translations`. You can change the default format of these files by using the `file_format` option.

```yml
lexik_translation:
dev_tools:
enable: false
create_missing: false
file_format: yml
```

If you use Doctrine ORM, you have to update your database:
Expand Down
30 changes: 27 additions & 3 deletions Util/DataGrid/DataGridRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Lexik\Bundle\TranslationBundle\Util\DataGrid;

use Lexik\Bundle\TranslationBundle\Manager\FileManagerInterface;
use Lexik\Bundle\TranslationBundle\Manager\LocaleManagerInterface;
use Lexik\Bundle\TranslationBundle\Document\TransUnit as TransUnitDocument;
use Lexik\Bundle\TranslationBundle\Manager\TransUnitManagerInterface;
Expand All @@ -24,6 +25,11 @@ class DataGridRequestHandler
*/
protected $transUnitManager;

/**
* @var FileManagerInterface
*/
protected $fileManager;

/**
* @var StorageInterface
*/
Expand All @@ -44,17 +50,25 @@ class DataGridRequestHandler
*/
protected $createMissing;

/**
* @var string
*/
protected $defaultFileFormat;

/**
* @param TransUnitManagerInterface $transUnitManager
* @param FileManagerInterface $fileManager
* @param StorageInterface $storage
* @param LocaleManagerInterface $localeManager
*/
public function __construct(TransUnitManagerInterface $transUnitManager, StorageInterface $storage, LocaleManagerInterface $localeManager)
public function __construct(TransUnitManagerInterface $transUnitManager, FileManagerInterface $fileManager, StorageInterface $storage, LocaleManagerInterface $localeManager)
{
$this->transUnitManager = $transUnitManager;
$this->fileManager = $fileManager;
$this->storage = $storage;
$this->localeManager = $localeManager;
$this->createMissing = false;
$this->defaultFileFormat = 'yml';
}

/**
Expand All @@ -73,6 +87,14 @@ public function setCreateMissing($createMissing)
$this->createMissing = (bool) $createMissing;
}

/**
* @param string $format
*/
public function setDefaultFileFormat($format)
{
$this->defaultFileFormat = $format;
}

/**
* Returns an array with the trans unit for the current page and the total of trans units
*
Expand Down Expand Up @@ -147,8 +169,10 @@ public function getByToken($token)
}

// Also store the translation if profiler state was defined
if (!$transUnit->hasTranslation($message['locale']) && $message['state'] == DataCollectorTranslator::MESSAGE_DEFINED) {
$this->transUnitManager->addTranslation($transUnit, $message['locale'], $message['translation'], null, true);
if (!$transUnit->hasTranslation($message['locale']) && $message['state'] === DataCollectorTranslator::MESSAGE_DEFINED) {
$file = $this->fileManager->getFor(sprintf('%s.%s.%s', $message['domain'], $message['locale'], $this->defaultFileFormat));

$this->transUnitManager->addTranslation($transUnit, $message['locale'], $message['translation'], $file, true);
}
}

Expand Down

0 comments on commit 4130eca

Please sign in to comment.