forked from servergrove/TranslationEditorBundle
-
Notifications
You must be signed in to change notification settings - Fork 14
Feat/generate #17
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
Open
jdewit
wants to merge
3
commits into
instaclick:master
Choose a base branch
from
jdewit:feat/generate
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feat/generate #17
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 <joris.w.dewit@gmail.com> | ||
| */ | ||
| 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.'); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 '.'; | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO this bundle should be in the "suggest" part: it is not required if the command 'locale:editor:generate' is not used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good call. the change has been made.