4

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.

1
  • Please check your csv file some times that is issue if it having any spaces like that and format is different like utf-8.I have installed 2 new languages in my site both working fine
    – Pawankumar
    May 15, 2017 at 13:31

5 Answers 5

3
+50

Please try using Resolver Interface.

$localeInterface = $objectManager->create('Magento\Framework\Locale\ResolverInterface');

$localeInterface->setLocale('ar_SA');
$localeInterface->setDefaultLocale('ar_SA');

echo __('Check Value');

This is working in my case. if you have any query ask me.

2
  • I'm using the same code but I still get the translation in English, did you make any other changes ? Thanks !
    – magentodev
    Jun 3, 2019 at 14:14
  • @magentodev did you get any solutions for the same ? Thanks Feb 4, 2020 at 7:24
1

If you want to use translation on external php script. You have to initialize translation using following code after $state->setAreaCode('frontend');.

$objectManager->get('\Magento\Framework\View\DesignLoader')->load(\Magento\Framework\App\Area::PART_TRANSLATE); 

Full script will be like

<?php

ini_set('display_errors', 1);
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');
$objectManager->get('\Magento\Framework\View\DesignLoader')->load(\Magento\Framework\App\Area::PART_TRANSLATE);

echo __('Check Value'); // will be print translated value if defined in language csv.

Cheers!

0

I have no idea about the setLocale() but check in your js-translation.json if your translation is really in that file.

If not you need to delete all js-translation.json and redeploy static content for that locale at least.

If it is still not in that file you need to paste your translation module or theme code. Ie. I don't see a /i18n/ar_sa/... subfolder.

0

I do it pretty much the same but with one more call on the $localeInterface

$localeInterface = $this->objectManager->create('Magento\Framework\TranslateInterface');
$localeInterface->setLocale('de_DE');
$localeInterface->loadData();

ant it works in my case.

0

You need to load the locale and set the correct renderer to Phrase class because by default it uses just Placeholder ( Magento\Framework\Phrase\Renderer\Placeholder ).

So something like this should work:

<?php

use Magento\Framework\App\Bootstrap;
use Magento\Framework\App\Http;

require __DIR__ . '/../app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
/** @var Http $app */
$app = $bootstrap->createApplication(Http::class);
$om = $bootstrap->getObjectManager();

///** @var Magento\Framework\App\State $state */
$state = $om->get(Magento\Framework\App\State::class);
$state->setAreaCode(Magento\Framework\App\Area::AREA_GLOBAL);

$translate = $om->get(\Magento\Framework\Translate::class);
$renderer = $om->get(\Magento\Framework\Phrase\Renderer\Translate::class);

$translate->setLocale('de_DE');
$translate->loadData('global', true);
$placeHolder = \Magento\Framework\Phrase::getRenderer();

$composite = new \Magento\Framework\Phrase\Renderer\Composite([
    $renderer, $placeHolder
]);

\Magento\Framework\Phrase::setRenderer($composite);

echo __('"%1" is not a valid email address.', 'Email');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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