Skip to content

[Translation][Command][FrameworkBundle] Enable translation debugging in directories #13443

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

Merged
merged 1 commit into from
Apr 6, 2015
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ protected function configure()
))
->setDefinition(array(
new InputArgument('locale', InputArgument::REQUIRED, 'The locale'),
new InputArgument('bundle', InputArgument::REQUIRED, 'The bundle name'),
new InputArgument('bundle', InputArgument::OPTIONAL, 'The bundle name or directory where to load the messages, defaults to app/Resources folder'),
new InputOption('domain', null, InputOption::VALUE_OPTIONAL, 'The messages domain'),
new InputOption('only-missing', null, InputOption::VALUE_NONE, 'Displays only missing messages'),
new InputOption('only-unused', null, InputOption::VALUE_NONE, 'Displays only unused messages'),
))
->setDescription('Displays translation messages informations')
->setDescription('Displays translation messages information')
->setHelp(<<<EOF
The <info>%command.name%</info> command helps finding unused or missing translation
messages and comparing them with the fallback ones by inspecting the
templates and translation files of a given bundle.
templates and translation files of a given bundle or the app folder.

You can display information about bundle translations in a specific locale:

Expand All @@ -71,6 +71,10 @@ protected function configure()

<info>php %command.full_name% --only-unused en AcmeDemoBundle</info>

You can display information about app translations in a specific locale:

<info>php %command.full_name% en</info>

EOF
)
;
Expand All @@ -87,17 +91,39 @@ protected function execute(InputInterface $input, OutputInterface $output)

$locale = $input->getArgument('locale');
$domain = $input->getOption('domain');
$bundle = $this->getContainer()->get('kernel')->getBundle($input->getArgument('bundle'));
$loader = $this->getContainer()->get('translation.loader');
$kernel = $this->getContainer()->get('kernel');

// Define Root Path to App folder
$rootPath = $kernel->getRootDir();

// Override with provided Bundle info
if (null !== $input->getArgument('bundle')) {
try {
$rootPath = $kernel->getBundle($input->getArgument('bundle'))->getPath();
} catch (\InvalidArgumentException $e) {
// such a bundle does not exist, so treat the argument as path
$rootPath = $input->getArgument('bundle');

if (!is_dir($rootPath)) {
throw new \InvalidArgumentException(sprintf('<error>"%s" is neither an enabled bundle nor a directory.</error>', $rootPath));
}
}
}

// get bundle directory
$translationsPath = $rootPath.'/Resources/translations';

// Extract used messages
$extractedCatalogue = new MessageCatalogue($locale);
$this->getContainer()->get('translation.extractor')->extract($bundle->getPath().'/Resources/views', $extractedCatalogue);
if (is_dir($rootPath.'/Resources/views')) {
$this->getContainer()->get('translation.extractor')->extract($rootPath.'/Resources/views', $extractedCatalogue);
}

// Load defined messages
$currentCatalogue = new MessageCatalogue($locale);
if (is_dir($bundle->getPath().'/Resources/translations')) {
$loader->loadMessages($bundle->getPath().'/Resources/translations', $currentCatalogue);
if (is_dir($translationsPath)) {
$loader->loadMessages($translationsPath, $currentCatalogue);
}

// Merge defined and extracted messages to get all message ids
Expand Down Expand Up @@ -130,7 +156,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$fallbackCatalogue = new MessageCatalogue($fallbackLocale);
$loader->loadMessages($bundle->getPath().'/Resources/translations', $fallbackCatalogue);
$loader->loadMessages($translationsPath, $fallbackCatalogue);
$fallbackCatalogues[] = $fallbackCatalogue;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected function configure()
->setName('translation:update')
->setDefinition(array(
new InputArgument('locale', InputArgument::REQUIRED, 'The locale'),
new InputArgument('bundle', InputArgument::OPTIONAL, 'The bundle where to load the messages, defaults to app/Resources folder', null),
new InputArgument('bundle', InputArgument::OPTIONAL, 'The bundle name or directory where to load the messages, defaults to app/Resources folder'),
new InputOption('prefix', null, InputOption::VALUE_OPTIONAL, 'Override the default prefix', '__'),
new InputOption('output-format', null, InputOption::VALUE_OPTIONAL, 'Override the default output format', 'yml'),
new InputOption('dump-messages', null, InputOption::VALUE_NONE, 'Should the messages be dumped in the console'),
Expand Down Expand Up @@ -83,16 +83,27 @@ protected function execute(InputInterface $input, OutputInterface $output)

return 1;
}
$kernel = $this->getContainer()->get('kernel');

// Define Root Path to App folder
$rootPath = $this->getApplication()->getKernel()->getRootDir();
$rootPath = $kernel->getRootDir();
$currentName = "app folder";

// Override with provided Bundle info
if (null !== $input->getArgument('bundle')) {
$foundBundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('bundle'));
$rootPath = $foundBundle->getPath();
$currentName = $foundBundle->getName();
try {
$foundBundle = $kernel->getBundle($input->getArgument('bundle'));
$rootPath = $foundBundle->getPath();
$currentName = $foundBundle->getName();
} catch (\InvalidArgumentException $e) {
// such a bundle does not exist, so treat the argument as path
$rootPath = $input->getArgument('bundle');
$currentName = $rootPath;

if (!is_dir($rootPath)) {
throw new \InvalidArgumentException(sprintf('<error>"%s" is neither an enabled bundle nor a directory.</error>', $rootPath));
}
}
}

// get bundle directory
Expand Down