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

Load translations for area, fixes #8412 #8413

Merged
merged 3 commits into from
Feb 11, 2017

Conversation

fooman
Copy link
Contributor

@fooman fooman commented Feb 3, 2017

See #8412 for steps to reproduce.

Result
screen shot 2017-02-04 at 12 00 13

@fooman fooman force-pushed the fix-8412-cron-not-translated branch from e8344db to 9601e69 Compare February 4, 2017 05:45
@fooman
Copy link
Contributor Author

fooman commented Feb 4, 2017

Please note I added @SuppressWarnings(PHPMD.CouplingBetweenObjects) as the AreaList pushed it across the boundary of 12. Fixing it like removing the ObjectManager would be a backwards incompatible change - just let me know if you would prefer that instead.

@fooman
Copy link
Contributor Author

fooman commented Feb 6, 2017

Travis failure is a timeout on Integration Tests1 on 5.6 - the same suite passes on 7.0

@reindert-vetter
Copy link
Contributor

@fooman This is the solution! I tested it and it works for me. I hope this solution is implemented quickly!

@ishakhsuvarov ishakhsuvarov self-assigned this Feb 7, 2017
private function getAreaList()
{
if ($this->areaList === null) {
$this->areaList = $this->objectManager->get(\Magento\Framework\App\AreaList::class);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use injection via optional parameter in constructor instead of deprecated method, for example:

public function __construct(
    \Magento\Framework\App\AreaList $arealist = null
) {
    $this->arealist = $arealist ?: $this->objectManager->get(\Magento\Framework\App\AreaList::class);
}

@fooman
Copy link
Contributor Author

fooman commented Feb 7, 2017

@ishakhsuvarov requested changes are implemented

@heldchen
Copy link
Contributor

heldchen commented Feb 8, 2017

#6511 could possibly also be fixed by this PR, have not had chance to test tho, sorry.

@fooman
Copy link
Contributor Author

fooman commented Feb 8, 2017

@heldchen indeed looks like my issue report is a double up to the one you linked to, the suggested solutions are also very similar to the one I proposed

@okorshenko okorshenko added this to the February 2017 milestone Feb 11, 2017
@mmansoor-magento mmansoor-magento merged commit 3548a1a into magento:develop Feb 11, 2017
@okorshenko
Copy link
Contributor

@fooman Thank you for your contribution. Your pull request merged to develop branch

@LucScu
Copy link

LucScu commented May 17, 2017

For my custom cronjob it not works, i have to set correct locale store before load Area
$storeManager = $this->objectManager->get('Magento\Store\Model\StoreManagerInterface');
$storeManager->setCurrentStore(1);
$this->areaList->getArea(Area::AREA_CRONTAB)->load(Area::PART_TRANSLATE);
Otherwise i continue to see en_US string.
Please help me to dynamize it with a parameter in cronjob.

@fooman
Copy link
Contributor Author

fooman commented May 17, 2017

@LucScu thanks for your feedback. Just to confirm what do you mean by custom cron and have you applied the changes from this pull request?

@LucScu
Copy link

LucScu commented May 18, 2017

@fooman
Yes, i applied full patch (except to CronTest.php).
I developed a custom module that executes this crontab

<job name="feed_google_create" instance="Lucasart\Feed\Model\Google" method="execute">
    <schedule>0 1 * * *</schedule>
</job>

Even if i set manually italian store in execute() method, translations are yet in en_US

public function execute() {

    $text = 'Inizio creazione feed';
    $this->logger->debug($text);

    // set italian store
    $this->storeManager->setCurrentStore(1);        

    // $title = __("Login Options");
    // $this->logger->debug($title);
    // $title = __("men's shirt");
    // $this->logger->debug($title);

    // retrieve products for feed
    $items = $this->getProducts();   

    // write feed
    $this->writeToFile($items);

    $text = 'Fine creazione feed';
    $this->logger->debug($text);

}    

To have translation in it_IT i have to set italian store in Magento\Framework\App\Cron before load Area (like explained in previous post).

@fooman
Copy link
Contributor Author

fooman commented May 18, 2017

Have you tried wrapping your code into an emulated store similar to how Magento does when sending email?

@LucScu
Copy link

LucScu commented May 18, 2017

i try to investigate how email translations was managed and i found emulation logic, but i wasn't able to understand how apply it to my code.
Where do you think have i to add it?

@LucScu
Copy link

LucScu commented May 22, 2017

please follow up

@fooman
Copy link
Contributor Author

fooman commented May 23, 2017

@LucScu please understand that this is not a forum to obtain support for custom code. You might have better luck on StackExchange. Generally you need to add
\Magento\Store\Model\App\Emulation to the constructor of your class and then run $this->appEmulation->startEnvironmentEmulation($storeId, 'frontend', true); with your store id.

Best of luck.

@LucScu
Copy link

LucScu commented May 23, 2017

@fooman sure i understand, but my custom code is related to your PR, i think it is helpful for all magento community.
So, i can confirm that it works also with custom crontab code.

@Cornel99
Copy link

Cornel99 commented Nov 2, 2017

@fooman
I have followed your solution and works great under developer (with cache disabled). But when I enable the cache in Magento 2.1.9 it turns emails back to English language. All cache cleared again from Magento backend even from Command line but the emails remain the same in English.

Did I missed something?

@Cornel99
Copy link

Cornel99 commented Nov 3, 2017

After some research I found that only "Translation Cache" if is disabled emails works correctly.
How can I clear this cache in order to work correctly?

@fooman fooman deleted the fix-8412-cron-not-translated branch June 29, 2020 08:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants