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

Feat/generate #17

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
79 changes: 79 additions & 0 deletions Command/GenerateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace ServerGrove\Bundle\TranslationEditorBundle\Command;

use Symfony\Component\Console\Input\InputArgument,
Symfony\Component\Console\Input\InputInterface,
Symfony\Component\Console\Input\InputOption,
Symfony\Component\Console\Output\OutputInterface,
Symfony\Component\Translation\MessageCatalogue;


/**
* Command for generating new translation files
*
* @author Joris de Wit <[email protected]>
*/
class GenerateCommand extends AbstractCommand
{
/**
* {@inheritdoc}
*/
protected function configure()
{
parent::configure();

$this
->setName('locale:editor:generate')
->setDescription('Creates a translation file for a locale automatically')
;
}

/**
* {@inheritdoc}
*/
public function execute(InputInterface $input, OutputInterface $output)
{
$this->input = $input;
$this->output = $output;

$dialog = $this->getHelper('dialog');

$storage = $this->getContainer()->get('server_grove_translation_editor.storage');

$localesArray = array();
$locales = $storage->findLocaleList();

foreach($locales as $locale) {
$localesArray[] = $locale->getLanguage();
}

$fromIndex = $dialog->select(
$output,
'Please select the locale to translate from',
$localesArray,
0
);

$from = $localesArray[$fromIndex];

$to = $dialog->ask(
$output,
'Please enter the locale of the language to translate to: ',
''
);

if (strlen($to) != 2) {
throw new \Exception('Locale must be 2 digits');
}

$generator = $this->getContainer()->get('server_grove_translation_editor.translation.generator');

$this->output->writeln('Please wait while your translations are translated.');

$generator->run($from, $to, true);

$this->output->writeln('Done.');
}

}
5 changes: 5 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public function getConfigTreeBuilder()
->end()
->end()
->end()
->booleanNode('use_translator')
->defaultValue(false)
->isRequired()
->cannotBeEmpty()
->end()
->end();

return $treeBuilder;
Expand Down
4 changes: 4 additions & 0 deletions DependencyInjection/ServerGroveTranslationEditorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@ public function load(array $configs, ContainerBuilder $container)

$container->setParameter($this->getAlias() . '.storage.type', $config['storage']['type']);
$container->setParameter($this->getAlias() . '.storage.manager', $config['storage']['manager']);

if ($config['use_translator'] == true) {
$loader->load('translator.xml');
}
}
}
66 changes: 66 additions & 0 deletions Generator/TranslationGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace ServerGrove\Bundle\TranslationEditorBundle\Generator;

use ServerGrove\Bundle\TranslationEditorBundle\Document\Locale;
use ServerGrove\Bundle\TranslationEditorBundle\Storage\StorageInterface;

use Avro\TranslatorBundle\Translator\TranslatorInterface;

class TranslationGenerator
{

protected $storageService;
protected $translator;

/**
* @param StorageInterface $storageService
* @param TranslaterIterface $translator
*/
public function __construct(StorageInterface $storageService, TranslatorInterface $translator)
{
$this->storageService = $storageService;
$this->translator = $translator;
}

/**
* Converts translations from one language to another
*
* @param string $from The language to translate from
* @param string $to The language to translate to
*/
public function run($from, $to, $progress = false)
{
$localeList = $this->storageService->findLocaleList(array('language' => $from));

if (count($localeList) == 0) {
throw new \Exception(sprintf('Locale "%s" does not exist', $from));
}

$locale = reset($localeList);

$toLocaleList = $this->storageService->findLocaleList(array('language' => $to));

$toLocale = reset($toLocaleList);

if (!$toLocale instanceOf Locale) {
$toLocale = $this->storageService->createLocale($to);

$this->storageService->flush();
}

$translations = $this->storageService->findTranslationList(array('locale' => $locale));

foreach ($translations as $translation) {
$newValue = $this->translator->translate($translation->getValue(), $from, $to);

$this->storageService->createTranslation($toLocale, $translation->getEntry(), $newValue);

$this->storageService->flush();

if ($progress) {
echo '.';
}
}
}
}
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ Import translation files into MongoDB. If no files is specified, the command wil

Export translations to translation files from MongoDB. If no files is specified, the command will search for files in translations directories in src/

* Generate new translation files

./app/console locale:editor:generate

Automatically generate translation files for a new locale using Microsoft Translator. Requires AvroTranslatorBundle.


## Screenshot

Expand Down Expand Up @@ -75,6 +81,12 @@ Doctrine MongoDB configuration (in your config_dev.yml):
type: server_grove_translation_editor.storage.mongodb
manager: doctrine_mongodb.odm.document_manager

Enable the generator with the following configuration.

server_grove_translation_editor:
use_translator: true


Add the routing configuration to app/config/routing_dev.yml

SGTranslationEditorBundle:
Expand All @@ -95,6 +107,10 @@ Add the routing configuration to app/config/routing_dev.yml

./app/console locale:editor:export

3. Generate translations from an existing locale to a new locale

./app/console locale:editor:generate

## WARNING

**PLEASE** Backup your translation files before using the editor. **Use a source control system like git, even svn is ok**. We are not responsible for lost information.
Expand Down
15 changes: 15 additions & 0 deletions Resources/config/translator.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>

<service id="server_grove_translation_editor.translation.generator" class="ServerGrove\Bundle\TranslationEditorBundle\Generator\TranslationGenerator">
<argument type="service" id="server_grove_translation_editor.storage"/>
<argument type="service" id="avro_translator.translator"/>
</service>

</services>
</container>
17 changes: 11 additions & 6 deletions Storage/MongoDBStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace ServerGrove\Bundle\TranslationEditorBundle\Storage;

use ServerGrove\Bundle\TranslationEditorBundle\Document\Entry;
use ServerGrove\Bundle\TranslationEditorBundle\Document\Locale;

/**
* Doctrine MongoDB Storage
*
Expand Down Expand Up @@ -68,13 +71,15 @@ public function findTranslationList(array $criteria = array())
{
$builder = $this->manager->createQueryBuilder($this->getTranslationClassName());

if(isset($criteria['locale']) && $criteria['locale'] instanceof \ServerGrove\Bundle\TranslationEditorBundle\Document\Locale) {
$criteria['locale'] = $criteria['locale']->getId();
if (isset($criteria['locale']) && $criteria['locale'] instanceof Locale) {
$criteria['locale.id'] = $criteria['locale']->getId();
unset($criteria['locale']);
}
if(isset($criteria['entry']) && $criteria['entry'] instanceof \ServerGrove\Bundle\TranslationEditorBundle\Document\Entry) {
$criteria['entry'] = $criteria['entry']->getId();
if (isset($criteria['entry']) && $criteria['entry'] instanceof Entry) {
$criteria['entry.id'] = $criteria['entry']->getId();
unset($criteria['entry']);
}

$this->hydrateCriteria($builder, $criteria);

return iterator_to_array($builder->getQuery()->execute());
Expand All @@ -89,7 +94,7 @@ public function findTranslationList(array $criteria = array())
protected function hydrateCriteria($builder, array $criteria = array())
{
foreach ($criteria as $fieldName => $fieldValue) {
$builder->addOr($builder->expr()->field($fieldName)->equals($fieldValue));
$builder->field($fieldName)->equals($fieldValue);
}
}
}
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
"php": ">=5.3.2",
"symfony/framework-bundle": "2.*"
},
"suggest": {
"avro/translator-bundle": "Needed when using the translation generator"
},
"autoload": {
"psr-0": {
"ServerGrove\\Bundle\\TranslationEditorBundle": ""
Expand Down