Skip to content

Commit 3e1b965

Browse files
committed
Use string template engine to render language translations
Signed-off-by: Jack Cherng <[email protected]>
1 parent 0668f09 commit 3e1b965

File tree

5 files changed

+77
-19
lines changed

5 files changed

+77
-19
lines changed

.phan/config.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
'src',
1919
'vendor/jfcherng/php-mb-string/src',
2020
'vendor/jfcherng/php-sequence-matcher/src',
21+
'vendor/nicmart/string-template/src',
2122
],
2223

2324
// A directory list that defines files that will be excluded

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"require": {
2929
"php": "^7.1.3",
3030
"jfcherng/php-mb-string": "^1.3",
31-
"jfcherng/php-sequence-matcher": "^3.1"
31+
"jfcherng/php-sequence-matcher": "^3.1",
32+
"nicmart/string-template": "~0.1"
3233
},
3334
"require-dev": {
3435
"friendsofphp/php-cs-fixer": "^2.15",

composer.lock

Lines changed: 55 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Renderer/AbstractRenderer.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,15 @@ protected function updateLanguage($old, $new): self
173173
/**
174174
* A shorthand to do translation.
175175
*
176-
* @param string $text The text
177-
* @param bool $escapeHtml Escape the translated text for HTML?
176+
* @param string $text The text
177+
* @param array $placeholders The placeholders
178+
* @param bool $escapeHtml Escape the translated text for HTML?
178179
*
179180
* @return string the translated text
180181
*/
181-
protected function _(string $text, bool $escapeHtml = true): string
182+
protected function _(string $text, array $placeholders = [], bool $escapeHtml = true): string
182183
{
183-
$text = $this->t->translate($text);
184+
$text = $this->t->translate($text, $placeholders);
184185

185186
return $escapeHtml ? \htmlspecialchars($text) : $text;
186187
}

src/Utility/Language.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@
44

55
namespace Jfcherng\Diff\Utility;
66

7+
use StringTemplate\Engine as StringTemplateEngine;
8+
79
final class Language
810
{
11+
/**
12+
* @var \StringTemplate\Engine
13+
*/
14+
private $stringEngine;
15+
916
/**
1017
* @var string[] the translation dict
1118
*/
@@ -23,6 +30,7 @@ final class Language
2330
*/
2431
public function __construct($target = 'eng')
2532
{
33+
$this->stringEngine = new StringTemplateEngine('{', '}');
2634
$this->setLanguageOrTranslations($target);
2735
}
2836

@@ -106,13 +114,16 @@ public static function getTranslationsByLanguage(string $language): array
106114
/**
107115
* Translation the text.
108116
*
109-
* @param string $text the text
117+
* @param string $text the text
118+
* @param array $placeholders the placeholders
110119
*
111120
* @return string
112121
*/
113-
public function translate(string $text): string
122+
public function translate(string $text, array $placeholders = []): string
114123
{
115-
return $this->translations[$text] ?? "![{$text}]";
124+
return isset($this->translations[$text])
125+
? $this->stringEngine->render($this->translations[$text], $placeholders)
126+
: "![{$text}]";
116127
}
117128

118129
/**

0 commit comments

Comments
 (0)