Skip to content
This repository was archived by the owner on Jul 16, 2020. It is now read-only.

Commit dec5e10

Browse files
gerpomartinlindhe
authored andcommitted
Adding escape character (#94)
Adds a escape character to allow escaping translation strings that contain the Laravel :link keyword.
1 parent 0da6b5b commit dec5e10

File tree

3 files changed

+120
-4
lines changed

3 files changed

+120
-4
lines changed

src/Generator.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Generator
1414

1515
const VUEX_I18N = 'vuex-i18n';
1616
const VUE_I18N = 'vue-i18n';
17+
const ESCAPE_CHAR = '!';
1718

1819
/**
1920
* The constructor
@@ -28,6 +29,9 @@ public function __construct($config = [])
2829
if (!isset($config['excludes'])) {
2930
$config['excludes'] = [];
3031
}
32+
if (!isset($config['escape_char'])) {
33+
$config['escape_char'] = self::ESCAPE_CHAR;
34+
}
3135
$this->config = $config;
3236
}
3337

@@ -275,10 +279,10 @@ private function adjustArray(array $arr)
275279
{
276280
$res = [];
277281
foreach ($arr as $key => $val) {
278-
$key = $this->adjustString($key);
282+
$key = $this->removeEscapeCharacter($this->adjustString($key));
279283

280284
if (is_string($val)) {
281-
$res[$key] = $this->adjustString($val);
285+
$res[$key] = $this->removeEscapeCharacter($this->adjustString($val));
282286
} else {
283287
$res[$key] = $this->adjustArray($val);
284288
}
@@ -287,7 +291,7 @@ private function adjustArray(array $arr)
287291
}
288292

289293
/**
290-
* Adjus vendor index placement.
294+
* Adjust vendor index placement.
291295
*
292296
* @param array $locales
293297
*
@@ -328,15 +332,35 @@ private function adjustString($s)
328332
$s = preg_replace($searchPipePattern, $threeColons, $s);
329333
}
330334

335+
$escaped_escape_char = preg_quote($this->config['escape_char'], '/');
331336
return preg_replace_callback(
332-
'/(?<!mailto|tel):\w+/',
337+
"/(?<!mailto|tel|{$escaped_escape_char}):\w+/",
333338
function ($matches) {
334339
return '{' . mb_substr($matches[0], 1) . '}';
335340
},
336341
$s
337342
);
338343
}
339344

345+
/**
346+
* Removes escape character if translation string contains sequence that looks like
347+
* Laravel style ":link", but should not be interpreted as such and was therefore escaped.
348+
*
349+
* @param string $s
350+
* @return string
351+
*/
352+
private function removeEscapeCharacter($s)
353+
{
354+
$escaped_escape_char = preg_quote($this->config['escape_char'], '/');
355+
return preg_replace_callback(
356+
"/{$escaped_escape_char}(:\w+)/",
357+
function ($matches) {
358+
return mb_substr($matches[0], 1);
359+
},
360+
$s
361+
);
362+
}
363+
340364
/**
341365
* Returns filename, with extension stripped
342366
* @param string $filename

src/config/vue-i18n-generator.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,15 @@
7979
|
8080
*/
8181
'showOutputMessages' => false,
82+
83+
/*
84+
|--------------------------------------------------------------------------
85+
| Escape character
86+
|--------------------------------------------------------------------------
87+
|
88+
| Allows to escape translations strings that should not be treated as a
89+
| variable
90+
|
91+
*/
92+
'escape_char' => '!',
8293
];

tests/GenerateTest.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,31 @@ function testBasicWithTranslationString()
246246
$this->destroyLocaleFilesFrom($arr, $root);
247247
}
248248

249+
function testBasicWithEscapedTranslationString()
250+
{
251+
$arr = [
252+
'en' => [
253+
'main' => [
254+
'hello :name' => 'Hello :name',
255+
'time test 10!:00' => 'Time test 10!:00',
256+
]
257+
],
258+
];
259+
260+
$root = $this->generateLocaleFilesFrom($arr);
261+
$this->assertEquals(
262+
'export default {' . PHP_EOL
263+
. ' "en": {' . PHP_EOL
264+
. ' "main": {' . PHP_EOL
265+
. ' "hello {name}": "Hello {name}",' . PHP_EOL
266+
. ' "time test 10:00": "Time test 10:00"' . PHP_EOL
267+
. ' }' . PHP_EOL
268+
. ' }' . PHP_EOL
269+
. '}' . PHP_EOL,
270+
(new Generator([]))->generateFromPath($root));
271+
$this->destroyLocaleFilesFrom($arr, $root);
272+
}
273+
249274
function testBasicWithVendor()
250275
{
251276
$arr = [
@@ -383,6 +408,62 @@ function testNamed()
383408
$this->destroyLocaleFilesFrom($arr, $root);
384409
}
385410

411+
function testNamedWithEscaped()
412+
{
413+
$arr = [
414+
'en' => [
415+
'help' => [
416+
'yes' => 'see :link y :lonk at 08!:00',
417+
'no' => [
418+
'one' => 'see :link',
419+
]
420+
]
421+
]
422+
];
423+
424+
$root = $this->generateLocaleFilesFrom($arr);
425+
426+
$this->assertEquals(
427+
'export default {' . PHP_EOL
428+
. ' "en": {' . PHP_EOL
429+
. ' "help": {' . PHP_EOL
430+
. ' "yes": "see {link} y {lonk} at 08:00",' . PHP_EOL
431+
. ' "no": {' . PHP_EOL
432+
. ' "one": "see {link}"' . PHP_EOL
433+
. ' }' . PHP_EOL
434+
. ' }' . PHP_EOL
435+
. ' }' . PHP_EOL
436+
. '}' . PHP_EOL,
437+
(new Generator([]))->generateFromPath($root));
438+
439+
$this->destroyLocaleFilesFrom($arr, $root);
440+
}
441+
442+
function testEscapedEscapeCharacter()
443+
{
444+
$arr = [
445+
'en' => [
446+
'help' => [
447+
'test escaped' => 'escaped escape char not !!:touched',
448+
]
449+
]
450+
];
451+
452+
$root = $this->generateLocaleFilesFrom($arr);
453+
454+
$this->assertEquals(
455+
'export default {' . PHP_EOL
456+
. ' "en": {' . PHP_EOL
457+
. ' "help": {' . PHP_EOL
458+
. ' "test escaped": "escaped escape char not !:touched"' . PHP_EOL
459+
. ' }' . PHP_EOL
460+
. ' }' . PHP_EOL
461+
. '}' . PHP_EOL,
462+
(new Generator([]))->generateFromPath($root));
463+
464+
$this->destroyLocaleFilesFrom($arr, $root);
465+
}
466+
386467
function testShouldNotTouchHtmlTags()
387468
{
388469
$arr = [

0 commit comments

Comments
 (0)