Skip to content

Instantly share code, notes, and snippets.

@jan53n
Last active June 13, 2020 07:03
Show Gist options
  • Save jan53n/4be9be5b82513cdfde803569150e3180 to your computer and use it in GitHub Desktop.
Save jan53n/4be9be5b82513cdfde803569150e3180 to your computer and use it in GitHub Desktop.
Text Translation with custom key format
function trans($text, $data, $data_format = "[%s]")
{
$dataWithFormattedKeys = [];
array_map(function($key, $value) use ($data_format, &$dataWithFormattedKeys) {
$formattedKey = sprintf($data_format, $key);
$dataWithFormattedKeys[$formattedKey] = $value;
}, array_keys($data), $data);
return strtr($text, $dataWithFormattedKeys);
}
// print_r(trans('hello [name]', ['name' => "World"]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment