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

Commit 354bb64

Browse files
committed
transform laravel named parameters ':link' into vue-i18n style {link}
1 parent 16d840d commit 354bb64

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

src/Generator.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,36 @@ private function allocateLocaleArray($path)
4444
foreach ($dir as $fileinfo) {
4545
if (!$fileinfo->isDot()) {
4646
$noExt = $this->removeExtension($fileinfo->getFilename());
47-
$data[$noExt] = include($path . '/' . $fileinfo->getFilename());
47+
$tmp = include($path . '/' . $fileinfo->getFilename());
48+
$res = [];
49+
50+
foreach ($tmp as $key => $val) {
51+
$res[$key] = $this->adjustString($val);
52+
}
53+
54+
$data[$noExt] = $res;
4855
}
4956
}
5057

5158
return $data;
5259
}
5360

61+
/**
62+
* Turn Laravel style ":link" into vue-i18n style "{link}"
63+
* @param string $s
64+
* @return string
65+
*/
66+
private function adjustString($s)
67+
{
68+
return preg_replace_callback(
69+
'/:\w*/',
70+
function ($matches) {
71+
return '{' . mb_substr($matches[0], 1) . '}';
72+
},
73+
$s
74+
);
75+
}
76+
5477
/**
5578
* @param string $filename
5679
* @return string

tests/GenerateTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,39 @@ function testBasic()
8787

8888
$this->destroyLocaleFilesFrom($arr, $root);
8989
}
90+
91+
function testNamed()
92+
{
93+
$arr = [
94+
'en' => [
95+
'help' => [
96+
'yes' => 'see :link y :lonk',
97+
]
98+
],
99+
'sv' => [
100+
'help' => [
101+
'yes' => 'se :lonk a :link',
102+
]
103+
]
104+
];
105+
106+
$root = $this->generateLocaleFilesFrom($arr);
107+
108+
$this->assertEquals(
109+
'export default {' . PHP_EOL
110+
. ' "en": {' . PHP_EOL
111+
. ' "help": {' . PHP_EOL
112+
. ' "yes": "see {link} y {lonk}"' . PHP_EOL
113+
. ' }' . PHP_EOL
114+
. ' },' . PHP_EOL
115+
. ' "sv": {' . PHP_EOL
116+
. ' "help": {' . PHP_EOL
117+
. ' "yes": "se {lonk} a {link}"' . PHP_EOL
118+
. ' }' . PHP_EOL
119+
. ' }' . PHP_EOL
120+
. '}' . PHP_EOL,
121+
(new Generator)->generateFromPath($root));
122+
123+
$this->destroyLocaleFilesFrom($arr, $root);
124+
}
90125
}

0 commit comments

Comments
 (0)