Skip to content

Commit

Permalink
Allow translate callback in dynamic catalog
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Dec 17, 2020
1 parent fe69869 commit e3af0f0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
26 changes: 24 additions & 2 deletions src/Catalog/DynamicArrayCatalog.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

class DynamicArrayCatalog extends ArrayCatalog
{
/**
* @var callable
*/
protected $_translateCallback;

public function getData()
{
return $this->_data;
Expand All @@ -21,16 +26,33 @@ public function asPhpFile($withNewLines = false): string

$content = ['<?php', PHP_EOL, 'return ['];

$callback = $this->_translateCallback;

foreach($this->getData() as $mid => $options)
{
$content[] = $indent . "'" . addslashes($mid) . "' => [";
$content[] = $indent . "'" . addcslashes(stripslashes($mid), "'") . "' => [";
foreach($options as $optK => $text)
{
$content[] = $indent . $indent . "'" . addslashes($optK) . "' => '" . addslashes($text) . "',";
$useText = $callback ? $callback($text) : $text;
$content[] = $indent . $indent
. "'" . addcslashes(stripslashes($optK), "'") . "' => '" . addcslashes(stripslashes($useText), "'") . "',";
}
$content[] = '],';
}
$content[] = '];';
return implode($implode, $content) . PHP_EOL;
}

/**
* Process text through this callback when generating the php file
*
* @param callable $func
*
* @return $this
*/
public function setTranslationCallback(callable $func)
{
$this->_translateCallback = $func;
return $this;
}
}
2 changes: 1 addition & 1 deletion tests/Supporting/TranslatedTextTestClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function setTranslator(Translator $translator): TranslatedTextTestClass
return $this;
}

protected function _getTranslator(): Translatable
protected function _getTranslator(): Translator
{
return $this->_translator;
}
Expand Down
7 changes: 7 additions & 0 deletions tests/Translators/ReplacementsOnlyTranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,11 @@ public function test_t()
$this->assertEquals('Hello John', $class->_t('Hello {name}', ['name' => 'John']));
$this->assertEquals('Hello {name}', $class->_t('Hello {name}', []));
}

public function testQuotes()
{
$class = $this->_getTester();
$string = 'Test for a quoted \'string\' in the middle and the \'end\'';
$this->assertEquals($string, $class->_('test_quoted_string_middle_qlhd63', $string));
}
}

0 comments on commit e3af0f0

Please sign in to comment.