コード例 #1
0
 /**
  * Replace translation calls with translation result and return content
  *
  * @param string $content
  * @return string
  */
 public function translate($content)
 {
     foreach ($this->config->getPatterns() as $pattern) {
         $content = preg_replace_callback($pattern, [$this, 'replaceCallback'], $content);
     }
     return $content;
 }
コード例 #2
0
 public function testGetData()
 {
     $chain = $this->getMock('Magento\\Framework\\View\\Asset\\PreProcessor\\Chain', [], [], '', false);
     $originalContent = 'content$.mage.__("hello1")content';
     $translatedContent = 'content"hello1"content';
     $patterns = ['~\\$\\.mage\\.__\\([\'|\\"](.+?)[\'|\\"]\\)~'];
     $this->configMock->expects($this->once())->method('isEmbeddedStrategy')->willReturn(true);
     $chain->expects($this->once())->method('getContent')->willReturn($originalContent);
     $this->configMock->expects($this->once())->method('getPatterns')->willReturn($patterns);
     $chain->expects($this->once())->method('setContent')->with($translatedContent);
     $this->model->process($chain);
 }
コード例 #3
0
 /**
  * @return void
  */
 public function testGetData()
 {
     $themePath = 'blank';
     $areaCode = 'adminhtml';
     $files = [['path1'], ['path2']];
     $relativePathMap = [['path1' => 'relativePath1'], ['path2' => 'relativePath2']];
     $contentsMap = [['relativePath1' => 'content1$.mage.__("hello1")content1'], ['relativePath2' => 'content2$.mage.__("hello2")content2']];
     $patterns = ['~\\$\\.mage\\.__\\([\'|\\"](.+?)[\'|\\"]\\)~'];
     $this->appStateMock->expects($this->once())->method('getAreaCode')->willReturn($areaCode);
     $this->filesUtilityMock->expects($this->once())->method('getJsFiles')->with($areaCode, $themePath)->willReturn($files);
     $this->rootDirectoryMock->expects($this->any())->method('getRelativePath')->willReturnMap($relativePathMap);
     $this->rootDirectoryMock->expects($this->any())->method('readFile')->willReturnMap($contentsMap);
     $this->configMock->expects($this->any())->method('getPatterns')->willReturn($patterns);
     $this->assertEquals([], $this->model->getData($themePath));
 }
コード例 #4
0
 public function testGetData()
 {
     $chain = $this->getMock('Magento\\Framework\\View\\Asset\\PreProcessor\\Chain', [], [], '', false);
     $asset = $this->getMock('Magento\\Framework\\View\\Asset\\File', [], [], '', false);
     $context = $this->getMock('Magento\\Framework\\View\\Asset\\File\\FallbackContext', [], [], '', false);
     $fileName = 'js-translation.json';
     $targetPath = 'path/js-translation.json';
     $themePath = '*/*';
     $dictionary = ['hello' => 'bonjour'];
     $chain->expects($this->once())->method('getTargetAssetPath')->willReturn($targetPath);
     $this->configMock->expects($this->once())->method('getDictionaryFileName')->willReturn($fileName);
     $chain->expects($this->once())->method('getAsset')->willReturn($asset);
     $asset->expects($this->once())->method('getContext')->willReturn($context);
     $context->expects($this->once())->method('getThemePath')->willReturn($themePath);
     $this->dataProviderMock->expects($this->once())->method('getData')->with($themePath)->willReturn($dictionary);
     $chain->expects($this->once())->method('setContent')->with(json_encode($dictionary));
     $chain->expects($this->once())->method('setContentType')->with('json');
     $this->model->process($chain);
 }
コード例 #5
0
 /**
  * @return void
  */
 public function testGetData()
 {
     $themePath = 'blank';
     $areaCode = 'adminhtml';
     $filePaths = [['path1'], ['path2'], ['path3'], ['path4']];
     $jsFilesMap = [['base', $themePath, '*', '*', [$filePaths[0]]], [$areaCode, $themePath, '*', '*', [$filePaths[1]]]];
     $staticFilesMap = [['base', $themePath, '*', '*', [$filePaths[2]]], [$areaCode, $themePath, '*', '*', [$filePaths[3]]]];
     $expectedResult = ['hello1' => 'hello1translated', 'hello2' => 'hello2translated', 'hello3' => 'hello3translated', 'hello4' => 'hello4translated'];
     $contentsMap = ['content1$.mage.__("hello1")content1', 'content2$.mage.__("hello2")content2', 'content2$.mage.__("hello3")content3', 'content2$.mage.__("hello4")content4'];
     $translateMap = [[['hello1'], [], 'hello1translated'], [['hello2'], [], 'hello2translated'], [['hello3'], [], 'hello3translated'], [['hello4'], [], 'hello4translated']];
     $patterns = ['~\\$\\.mage\\.__\\(([\'"])(.+?)\\1\\)~'];
     $this->appStateMock->expects($this->once())->method('getAreaCode')->willReturn($areaCode);
     $this->filesUtilityMock->expects($this->any())->method('getJsFiles')->willReturnMap($jsFilesMap);
     $this->filesUtilityMock->expects($this->any())->method('getStaticHtmlFiles')->willReturnMap($staticFilesMap);
     foreach ($contentsMap as $index => $content) {
         $this->fileReadMock->expects($this->at($index))->method('readAll')->willReturn($content);
     }
     $this->configMock->expects($this->any())->method('getPatterns')->willReturn($patterns);
     $this->translateMock->expects($this->any())->method('render')->willReturnMap($translateMap);
     $this->assertEquals($expectedResult, $this->model->getData($themePath));
 }
コード例 #6
0
 /**
  * Parse content for entries to be translated
  *
  * @param string $content
  * @return string[]
  * @throws \Exception
  */
 protected function getPhrases($content)
 {
     $phrases = [];
     foreach ($this->config->getPatterns() as $pattern) {
         $result = preg_match_all($pattern, $content, $matches);
         if ($result) {
             $phrases = array_merge($phrases, $matches[1]);
         }
         if (false === $result) {
             throw new \Exception(sprintf('Error while generating js translation dictionary: "%s"', error_get_last()));
         }
     }
     return $phrases;
 }
コード例 #7
0
ファイル: ConfigTest.php プロジェクト: tingyeeh/magento2
 public function testgetPatterns()
 {
     $this->assertEquals($this->patterns, $this->model->getPatterns());
 }
コード例 #8
0
ファイル: Js.php プロジェクト: pradeep-wagento/magento2
 /**
  * Is js translation set to dictionary mode
  *
  * @return bool
  */
 public function dictionaryEnabled()
 {
     return $this->config->dictionaryEnabled();
 }
コード例 #9
0
ファイル: PreProcessor.php プロジェクト: tingyeeh/magento2
 /**
  * Is provided path the path to translation dictionary
  *
  * @param string $path
  * @return bool
  */
 protected function isDictionaryPath($path)
 {
     return strpos($path, $this->config->getDictionaryFileName()) !== false;
 }