Last active
June 13, 2020 07:03
-
-
Save jan53n/4be9be5b82513cdfde803569150e3180 to your computer and use it in GitHub Desktop.
Text Translation with custom key format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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