Skip to content

Commit

Permalink
Adding escape character (martinlindhe#94)
Browse files Browse the repository at this point in the history
Adds a escape character to allow escaping translation strings that contain the Laravel :link keyword.
  • Loading branch information
gerpo authored and martinlindhe committed Oct 8, 2019
1 parent 0da6b5b commit dec5e10
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 4 deletions.
32 changes: 28 additions & 4 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Generator

const VUEX_I18N = 'vuex-i18n';
const VUE_I18N = 'vue-i18n';
const ESCAPE_CHAR = '!';

/**
* The constructor
Expand All @@ -28,6 +29,9 @@ public function __construct($config = [])
if (!isset($config['excludes'])) {
$config['excludes'] = [];
}
if (!isset($config['escape_char'])) {
$config['escape_char'] = self::ESCAPE_CHAR;
}
$this->config = $config;
}

Expand Down Expand Up @@ -275,10 +279,10 @@ private function adjustArray(array $arr)
{
$res = [];
foreach ($arr as $key => $val) {
$key = $this->adjustString($key);
$key = $this->removeEscapeCharacter($this->adjustString($key));

if (is_string($val)) {
$res[$key] = $this->adjustString($val);
$res[$key] = $this->removeEscapeCharacter($this->adjustString($val));
} else {
$res[$key] = $this->adjustArray($val);
}
Expand All @@ -287,7 +291,7 @@ private function adjustArray(array $arr)
}

/**
* Adjus vendor index placement.
* Adjust vendor index placement.
*
* @param array $locales
*
Expand Down Expand Up @@ -328,15 +332,35 @@ private function adjustString($s)
$s = preg_replace($searchPipePattern, $threeColons, $s);
}

$escaped_escape_char = preg_quote($this->config['escape_char'], '/');
return preg_replace_callback(
'/(?<!mailto|tel):\w+/',
"/(?<!mailto|tel|{$escaped_escape_char}):\w+/",
function ($matches) {
return '{' . mb_substr($matches[0], 1) . '}';
},
$s
);
}

/**
* Removes escape character if translation string contains sequence that looks like
* Laravel style ":link", but should not be interpreted as such and was therefore escaped.
*
* @param string $s
* @return string
*/
private function removeEscapeCharacter($s)
{
$escaped_escape_char = preg_quote($this->config['escape_char'], '/');
return preg_replace_callback(
"/{$escaped_escape_char}(:\w+)/",
function ($matches) {
return mb_substr($matches[0], 1);
},
$s
);
}

/**
* Returns filename, with extension stripped
* @param string $filename
Expand Down
11 changes: 11 additions & 0 deletions src/config/vue-i18n-generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,15 @@
|
*/
'showOutputMessages' => false,

/*
|--------------------------------------------------------------------------
| Escape character
|--------------------------------------------------------------------------
|
| Allows to escape translations strings that should not be treated as a
| variable
|
*/
'escape_char' => '!',
];
81 changes: 81 additions & 0 deletions tests/GenerateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,31 @@ function testBasicWithTranslationString()
$this->destroyLocaleFilesFrom($arr, $root);
}

function testBasicWithEscapedTranslationString()
{
$arr = [
'en' => [
'main' => [
'hello :name' => 'Hello :name',
'time test 10!:00' => 'Time test 10!:00',
]
],
];

$root = $this->generateLocaleFilesFrom($arr);
$this->assertEquals(
'export default {' . PHP_EOL
. ' "en": {' . PHP_EOL
. ' "main": {' . PHP_EOL
. ' "hello {name}": "Hello {name}",' . PHP_EOL
. ' "time test 10:00": "Time test 10:00"' . PHP_EOL
. ' }' . PHP_EOL
. ' }' . PHP_EOL
. '}' . PHP_EOL,
(new Generator([]))->generateFromPath($root));
$this->destroyLocaleFilesFrom($arr, $root);
}

function testBasicWithVendor()
{
$arr = [
Expand Down Expand Up @@ -383,6 +408,62 @@ function testNamed()
$this->destroyLocaleFilesFrom($arr, $root);
}

function testNamedWithEscaped()
{
$arr = [
'en' => [
'help' => [
'yes' => 'see :link y :lonk at 08!:00',
'no' => [
'one' => 'see :link',
]
]
]
];

$root = $this->generateLocaleFilesFrom($arr);

$this->assertEquals(
'export default {' . PHP_EOL
. ' "en": {' . PHP_EOL
. ' "help": {' . PHP_EOL
. ' "yes": "see {link} y {lonk} at 08:00",' . PHP_EOL
. ' "no": {' . PHP_EOL
. ' "one": "see {link}"' . PHP_EOL
. ' }' . PHP_EOL
. ' }' . PHP_EOL
. ' }' . PHP_EOL
. '}' . PHP_EOL,
(new Generator([]))->generateFromPath($root));

$this->destroyLocaleFilesFrom($arr, $root);
}

function testEscapedEscapeCharacter()
{
$arr = [
'en' => [
'help' => [
'test escaped' => 'escaped escape char not !!:touched',
]
]
];

$root = $this->generateLocaleFilesFrom($arr);

$this->assertEquals(
'export default {' . PHP_EOL
. ' "en": {' . PHP_EOL
. ' "help": {' . PHP_EOL
. ' "test escaped": "escaped escape char not !:touched"' . PHP_EOL
. ' }' . PHP_EOL
. ' }' . PHP_EOL
. '}' . PHP_EOL,
(new Generator([]))->generateFromPath($root));

$this->destroyLocaleFilesFrom($arr, $root);
}

function testShouldNotTouchHtmlTags()
{
$arr = [
Expand Down

0 comments on commit dec5e10

Please sign in to comment.