/** * Replace placeholder wrapped by {{ }} with $data array * @param string $content * @param array $data * @return string */ public static function replacePlaceholderValues($content, $data) { if (is_array($data)) { $data = ArrayUtils::dot($data); } $content = StringUtils::replacePlaceholder($content, $data); return $content; }
/** * @inheritDoc */ public function getCode($data) { return StringUtils::replacePlaceholder($this->getFormatTemplate(), $data); }
public function testReplacePlaceholder() { $string = 'I went to {{place}}'; $data = ['place' => 'Portland']; $expected = 'I went to Portland'; $this->assertEquals($expected, StringUtils::replacePlaceholder($string, $data)); $this->assertEquals($expected, StringUtils::replacePlaceholder($string, $data, StringUtils::PLACEHOLDER_DOUBLE_MUSTACHE)); $string = 'I went to %{place}'; $this->assertEquals($expected, StringUtils::replacePlaceholder($string, $data, StringUtils::PLACEHOLDER_PERCENTAGE_MUSTACHE)); $string = 'Took a flight from {{from_airport}} to {{to_airport}}'; $data = ['from_airport' => 'SFO', 'to_airport' => 'PDX']; $expected = 'Took a flight from SFO to PDX'; $this->assertEquals($expected, StringUtils::replacePlaceholder($string, $data)); $this->assertEquals($expected, StringUtils::replacePlaceholder($string, $data, StringUtils::PLACEHOLDER_DOUBLE_MUSTACHE)); $string = 'Took a flight from %{from_airport} to %{to_airport}'; $this->assertEquals($expected, StringUtils::replacePlaceholder($string, $data, StringUtils::PLACEHOLDER_PERCENTAGE_MUSTACHE)); }
function __t($key, $data = []) { static $phrases; if (!$phrases) { $phrases = get_phrases(get_user_locale()); } $phrase = isset($phrases[$key]) ? $phrases[$key] : $key; $phrase = \Directus\Util\StringUtils::replacePlaceholder($phrase, $data, \Directus\Util\StringUtils::PLACEHOLDER_PERCENTAGE_MUSTACHE); return $phrase; }