- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2017
02:07 AM
05-16-2017
02:07 AM
Magento 2 set locale programmatically?
i am create one test PHP file in Magento root directory. i install ar_SA Language pack and try to convert one string English to Saudi Arabia here is my test code.
<?php ini_set('display_errors', 1); ini_set('max_execution_time', 0); ini_set("memory_limit", "-1"); set_time_limit(0); error_reporting(E_ALL); require './app/bootstrap.php'; $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER); $objectManager = $bootstrap->getObjectManager(); $state = $objectManager->get('Magento\Framework\App\State'); $state->setAreaCode('frontend'); $localeInterface = $objectManager->create('Magento\Framework\TranslateInterface'); $localeInterface->setLocale('ar_SA'); echo $localeInterface->getLocale(); // it's print ar_SA echo __('Check Value');
I have the corresponding translation in /i18n/ar_SA.csv file, it doesn't print the translated sentence.
Labels:
2 REPLIES 2
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2019
07:53 AM
06-03-2019
07:53 AM
Re: Magento 2 set locale programmatically?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2022
09:43 AM
12-16-2022
09:43 AM
Re: Magento 2 set locale programmatically?
Late in the part, but found it: https://www.youtube.com/watch?v=D5curtOIPEc&t=77s
TL;DR: import the Magento\Framework\View\DesignLoader class and use a `$this->designLoader->load()` before your translation string