Skip to content

Commit

Permalink
MAGETWO-72301: [Github] Static content deploy with multiple locales: …
Browse files Browse the repository at this point in the history
…js-translation.json files are the same #10673
  • Loading branch information
Sergey Shvets committed Nov 10, 2017
1 parent 29a1a27 commit fc34d7b
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 109 deletions.
30 changes: 17 additions & 13 deletions app/code/Magento/Deploy/Service/DeployPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Deploy\Service;

use Magento\Deploy\Package\Package;
use Magento\Deploy\Package\PackageFile;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\App\State as AppState;
use Magento\Framework\Locale\ResolverInterface as LocaleResolver;
use Magento\Framework\View\Asset\ContentProcessorException;
use Magento\Deploy\Console\InputValidator;
use Magento\Framework\View\Design\Theme\ListInterface;
use Magento\Framework\View\DesignInterface;
use Psr\Log\LoggerInterface;

/**
Expand Down Expand Up @@ -91,15 +95,15 @@ public function __construct(
* @param array $options
* @param bool $skipLogging
* @return bool true on success
* @throws \Exception
*/
public function deploy(Package $package, array $options, $skipLogging = false)
{
$result = $this->appState->emulateAreaCode(
$package->getArea() == Package::BASE_AREA ? 'global' : $package->getArea(),
$package->getArea() === Package::BASE_AREA ? 'global' : $package->getArea(),
function () use ($package, $options, $skipLogging) {
// emulate application locale needed for correct file path resolving
$this->localeResolver->setLocale($package->getLocale());

$this->deployEmulated($package, $options, $skipLogging);
}
);
Expand All @@ -111,7 +115,7 @@ function () use ($package, $options, $skipLogging) {
* @param Package $package
* @param array $options
* @param bool $skipLogging
* @return int
* @return bool
*/
public function deployEmulated(Package $package, array $options, $skipLogging = false)
{
Expand Down Expand Up @@ -200,14 +204,14 @@ private function processFile(PackageFile $file, Package $package)
private function checkIfCanCopy(PackageFile $file, Package $package, Package $parentPackage = null)
{
return $parentPackage
&& $file->getOrigPackage() !== $package
&& (
$file->getArea() !== $package->getArea()
|| $file->getTheme() !== $package->getTheme()
|| $file->getLocale() !== $package->getLocale()
)
&& $file->getOrigPackage() == $parentPackage
&& $this->deployStaticFile->readFile($file->getDeployedFileId(), $parentPackage->getPath());
&& $file->getOrigPackage() !== $package
&& (
$file->getArea() !== $package->getArea()
|| $file->getTheme() !== $package->getTheme()
|| $file->getLocale() !== $package->getLocale()
)
&& $file->getOrigPackage() === $parentPackage
&& $this->deployStaticFile->readFile($file->getDeployedFileId(), $parentPackage->getPath());
}

/**
Expand All @@ -219,10 +223,10 @@ private function checkIfCanCopy(PackageFile $file, Package $package, Package $pa
*/
private function checkFileSkip($filePath, array $options)
{
if ($filePath != '.') {
if ($filePath !== '.') {
$ext = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
$basename = pathinfo($filePath, PATHINFO_BASENAME);
if ($ext == 'less' && strpos($basename, '_') === 0) {
if ($ext === 'less' && strpos($basename, '_') === 0) {
return true;
}
$option = isset(InputValidator::$fileExtensionOptionMap[$ext])
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/Deploy/Service/DeployStaticContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public function deploy(array $options)
$deployRjsConfig = $this->objectManager->create(DeployRequireJsConfig::class, [
'logger' => $this->logger
]);
/** @var DeployTranslationsDictionary $deployI18n */
$deployI18n = $this->objectManager->create(DeployTranslationsDictionary::class, [
'logger' => $this->logger
]);
Expand Down
28 changes: 22 additions & 6 deletions app/code/Magento/Translation/Model/Json/PreProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Translation\Model\Json;

use Magento\Framework\App\AreaList;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\TranslateInterface;
use Magento\Framework\View\Asset\File\FallbackContext;
use Magento\Framework\View\Asset\PreProcessor\Chain;
use Magento\Framework\View\Asset\PreProcessorInterface;
use Magento\Framework\View\DesignInterface;
use Magento\Translation\Model\Js\Config;
use Magento\Translation\Model\Js\DataProviderInterface;
use Magento\Framework\View\Asset\PreProcessor\Chain;
use Magento\Framework\View\Asset\File\FallbackContext;
use Magento\Framework\App\AreaList;
use Magento\Framework\TranslateInterface;

/**
* PreProcessor responsible for providing js translation dictionary
Expand Down Expand Up @@ -41,30 +44,38 @@ class PreProcessor implements PreProcessorInterface
* @var TranslateInterface
*/
protected $translate;
/**
* @var DesignInterface
*/
private $viewDesign;

/**
* @param Config $config
* @param DataProviderInterface $dataProvider
* @param AreaList $areaList
* @param TranslateInterface $translate
* @param DesignInterface|null $viewDesign
*/
public function __construct(
Config $config,
DataProviderInterface $dataProvider,
AreaList $areaList,
TranslateInterface $translate
TranslateInterface $translate,
DesignInterface $viewDesign = null
) {
$this->config = $config;
$this->dataProvider = $dataProvider;
$this->areaList = $areaList;
$this->translate = $translate;
$this->viewDesign = $viewDesign ?: ObjectManager::getInstance()->get(DesignInterface::class);
}

/**
* Transform content and/or content type for the specified preprocessing chain object
*
* @param Chain $chain
* @return void
* @throws \Exception
*/
public function process(Chain $chain)
{
Expand All @@ -77,7 +88,12 @@ public function process(Chain $chain)
if ($context instanceof FallbackContext) {
$themePath = $context->getThemePath();
$areaCode = $context->getAreaCode();
$this->translate->setLocale($context->getLocale());

$this->viewDesign->setDesignTheme($themePath, $areaCode);

$this->translate
->setLocale($context->getLocale())
->loadData($areaCode);
}

$area = $this->areaList->getArea($areaCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Translation\Test\Unit\Model\Json;

use Magento\Translation\Model\Js\Config;
Expand Down Expand Up @@ -41,7 +42,15 @@ protected function setUp()
$this->configMock = $this->createMock(\Magento\Translation\Model\Js\Config::class);
$this->dataProviderMock = $this->createMock(\Magento\Translation\Model\Js\DataProvider::class);
$this->areaListMock = $this->createMock(\Magento\Framework\App\AreaList::class);
$this->translateMock = $this->getMockForAbstractClass(\Magento\Framework\TranslateInterface::class);
$this->translateMock = $this->getMockBuilder(\Magento\Framework\Translate::class)
->disableOriginalConstructor()
->getMock();

$this->translateMock
->expects($this->once())
->method('setLocale')
->willReturn($this->translateMock);

$this->model = new PreProcessor(
$this->configMock,
$this->dataProviderMock,
Expand Down Expand Up @@ -97,6 +106,11 @@ public function testGetData()
->method('setContentType')
->with('json');

$this->translateMock
->expects($this->once())
->method('loadData')
->willReturn($this->translateMock);

$this->model->process($chain);
}
}
47 changes: 35 additions & 12 deletions dev/tests/integration/testsuite/Magento/Framework/TranslateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,43 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Framework;

use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Helper\CacheCleaner;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject;

/**
* @magentoAppIsolation enabled
* @magentoCache all disabled
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class TranslateTest extends \PHPUnit\Framework\TestCase
class TranslateTest extends TestCase
{
/** @var \Magento\Framework\Translate */
private $translate;

protected function setUp()
{
/** @var \Magento\Framework\View\FileSystem $viewFileSystem */
/** @var \Magento\Framework\View\FileSystem | PHPUnit_Framework_MockObject_MockObject $viewFileSystem */
$viewFileSystem = $this->createPartialMock(
\Magento\Framework\View\FileSystem::class,
['getLocaleFileName', 'getDesignTheme']
['getLocaleFileName']
);

$viewFileSystem->expects($this->any())
->method('getLocaleFileName')
->will(
$this->returnValue(dirname(__DIR__) . '/Theme/Model/_files/design/frontend/Test/default/i18n/en_US.csv')
$this->returnValue(
dirname(__DIR__) . '/Translation/Model/_files/Magento/design/Magento/theme/i18n/en_US.csv'
)
);

/** @var \Magento\Framework\View\Design\ThemeInterface $theme */
/** @var \Magento\Framework\View\Design\ThemeInterface | PHPUnit_Framework_MockObject_MockObject $theme */
$theme = $this->createMock(\Magento\Framework\View\Design\ThemeInterface::class);
$theme->expects($this->any())->method('getId')->will($this->returnValue(10));

$viewFileSystem->expects($this->any())->method('getDesignTheme')->will($this->returnValue($theme));
$theme->expects($this->any())->method('getThemePath')->will($this->returnValue('Magento/luma'));

/** @var \Magento\TestFramework\ObjectManager $objectManager */
$objectManager = Bootstrap::getObjectManager();
Expand All @@ -55,7 +58,7 @@ protected function setUp()
dirname(__DIR__) . '/Translation/Model/_files/Magento/Catalog/i18n'
);

/** @var \Magento\Theme\Model\View\Design $designModel */
/** @var \Magento\Theme\Model\View\Design | \PHPUnit_Framework_MockObject_MockObject $designModel */
$designModel = $this->getMockBuilder(\Magento\Theme\Model\View\Design::class)
->setMethods(['getDesignTheme'])
->setConstructorArgs(
Expand Down Expand Up @@ -96,6 +99,9 @@ public function testLoadData()
/**
* @magentoCache all disabled
* @dataProvider translateDataProvider
* @param string $inputText
* @param string $expectedTranslation
* @throws Exception\LocalizedException
*/
public function testTranslate($inputText, $expectedTranslation)
{
Expand All @@ -111,9 +117,26 @@ public function translateDataProvider()
{
return [
['', ''],
['Text with different translation on different modules', 'Text translation that was last loaded'],
['text_with_no_translation', 'text_with_no_translation'],
['Design value to translate', 'Design translated value']
[
'Theme phrase will be translated',
'Theme phrase is translated',
],
[
'Phrase in Magento_Store module that doesn\'t need translation',
'Phrase in Magento_Store module that doesn\'t need translation',
],
[
'Phrase in Magento_Catalog module that doesn\'t need translation',
'Phrase in Magento_Catalog module that doesn\'t need translation',
],
[
'Magento_Store module phrase will be override by theme translation',
'Magento_Store module phrase is override by theme translation',
],
[
'Magento_Catalog module phrase will be override by theme translation',
'Magento_Catalog module phrase is override by theme translation',
],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"Theme phrase will be translated","Theme phrase is translated"
"Magento_Catalog module phrase will be override by theme translation","Magento_Catalog module phrase is override by theme translation"
"Magento_Store module phrase will be override by theme translation","Magento_Store module phrase is override by theme translation"
Loading

0 comments on commit fc34d7b

Please sign in to comment.