2

I created a language package by using this command:

php bin/magento i18n:collect-phrases -o "app/default.csv" -m

and using

php bin/magento i18n:pack --mode=merge -d "app/default.csv" "xx_YY"

to distribute to each module. But I want use PHP code to simulate both command line calls. What should I do?

1 Answer 1

1

You can always use the exec or system functions.
But if you don't want to do that...
The commands map to some php class and method so you should be able to simulate the code.
For example the i18n:collect-phrases maps to \Magento\Setup\Console\Command\I18nCollectPhrasesCommand::execute.

Try to replicate in your code the behavior from that method. Do the same for i18n:pack. This one maps to Magento\Setup\Console\Command\I18nPackCommand::execute.

If you want to do it in your own class, try adding this code to your method:

Add this at the top of the file

use Magento\Setup\Module\I18n\ServiceLocator;

then in your method add this:

$directory = 'Add here directory of file to parse. leave empty for all'
$generator = ServiceLocator::getDictionaryGenerator();
$generator->generate(
    $directory,
    "app/default.csv",
    1 //if you specified a certain directory to parse set this to 1.
);

I haven't tested the code, so you might need to debug things.

3
  • 1
    yes, I saw these class. But I don't know how to call it in my controller or block. Sorry because I'm a newbie in Magento.
    – Henry Bui
    Commented Nov 21, 2016 at 10:04
  • 1
    See my edit to the answer.
    – Marius
    Commented Nov 21, 2016 at 10:10
  • 1
    Oh, yay. Thanks so much, it's done. Even though it slow a little bit with browser.
    – Henry Bui
    Commented Nov 21, 2016 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.