Skip to content

Commit f5db914

Browse files
committed
fix dot
1 parent 2578fa8 commit f5db914

File tree

4 files changed

+21
-24
lines changed

4 files changed

+21
-24
lines changed

src/Collections/PhpTranslations.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use Elegantly\Translator\Drivers\PhpDriver;
88
use Illuminate\Support\Arr;
9-
use Illuminate\Support\Collection;
109
use Illuminate\Support\Enumerable;
1110
use Illuminate\Support\Str;
1211

@@ -177,31 +176,29 @@ public function only($keys)
177176
*/
178177
public static function toDot(array $values): static
179178
{
180-
$translations = collect($values)
181-
->mapWithKeys(fn ($value, $key) => [static::prepareTranslations($key) => static::prepareTranslations($value)])
182-
->dot();
183-
184-
return new static($translations);
179+
return new static(
180+
Arr::dot(static::prepareTranslations($values))
181+
);
185182
}
186183

187184
/**
188-
* @param array<array-key, mixed>|Translations $translations
189-
* @return Collection<string, mixed>
185+
* @param Translations|array<array-key, mixed> $translations
186+
* @return array<array-key, mixed> $values
190187
*/
191-
public static function toUndot(Translations|array $translations): Collection
188+
public static function toUndot(Translations|array $translations): array
192189
{
193-
return collect($translations)
194-
->undot()
195-
->mapWithKeys(fn ($value, $key) => [static::unprepareTranslations($key) => static::unprepareTranslations($value)]);
190+
$translations = $translations instanceof Translations ? $translations->all() : $translations;
191+
192+
return static::unprepareTranslations(Arr::undot((array) $translations));
196193
}
197194

198195
/**
199196
* Dot in translations keys might break the initial array structure
200197
* To prevent that, we encode the dots in unicode
201198
*/
202-
public static function prepareTranslations(mixed $values): mixed
199+
public static function prepareTranslations(mixed $values, bool $escape = false): mixed
203200
{
204-
if (is_string($values)) {
201+
if ($escape && is_string($values)) {
205202
return Str::replace('.', '&#46;', $values);
206203
}
207204

@@ -215,7 +212,7 @@ public static function prepareTranslations(mixed $values): mixed
215212

216213
return collect($values)
217214
->mapWithKeys(fn ($value, $key) => [
218-
static::prepareTranslations($key) => static::prepareTranslations($value),
215+
static::prepareTranslations($key, true) => static::prepareTranslations($value),
219216
])
220217
->all();
221218
}
@@ -224,9 +221,9 @@ public static function prepareTranslations(mixed $values): mixed
224221
* Dot in translations keys might break the initial array structure
225222
* To prevent that, we encode the dots in unicode
226223
*/
227-
public static function unprepareTranslations(mixed $values): mixed
224+
public static function unprepareTranslations(mixed $values, bool $unescape = false): mixed
228225
{
229-
if (is_string($values)) {
226+
if ($unescape && is_string($values)) {
230227
return Str::replace('&#46;', '.', $values);
231228
}
232229

@@ -240,7 +237,7 @@ public static function unprepareTranslations(mixed $values): mixed
240237

241238
return collect($values)
242239
->mapWithKeys(fn ($value, $key) => [
243-
static::unprepareTranslations($key) => static::unprepareTranslations($value),
240+
static::unprepareTranslations($key, true) => static::unprepareTranslations($value),
244241
])
245242
->all();
246243
}

src/Drivers/PhpDriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public function getTranslationsInNamespace(string $locale, string $namespace): a
130130

131131
public function saveTranslations(string $locale, Translations $translations): Translations
132132
{
133-
$undot = PhpTranslations::toUndot($translations)->all();
133+
$undot = PhpTranslations::toUndot($translations);
134134

135135
foreach ($undot as $namespace => $values) {
136136

tests/Feature/PhpDriverTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
$translations = $translator->getTranslations('fr_CA');
128128

129129
expect($translations->toArray())->toBe([
130-
'dotted.This key contains a dot&#46; In the middle.And it &#46; ha&#46;s children&#46;' => 'And it has children&#46;',
130+
'dotted.This key contains a dot&#46; In the middle.And it &#46; ha&#46;s children&#46;' => 'And it has children.',
131131
]);
132132

133133
});
@@ -142,7 +142,7 @@
142142
$translator->saveTranslations('fr_CA', $translations);
143143

144144
expect($translator->getTranslations('fr_CA')->toArray())->toBe([
145-
'dotted.This key contains a dot&#46; In the middle.And it &#46; ha&#46;s children&#46;' => 'And it has children&#46;',
145+
'dotted.This key contains a dot&#46; In the middle.And it &#46; ha&#46;s children&#46;' => 'And it has children.',
146146
]);
147147

148148
});

tests/Unit/PhpTranslationsTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,18 @@
147147
]);
148148

149149
expect($translations->toArray())->toBe([
150-
'This key contains a dot&#46; In the middle.And it&#46;has children&#46;' => 'And it has children&#46;',
150+
'This key contains a dot&#46; In the middle.And it&#46;has children&#46;' => 'And it has children.',
151151
]);
152152

153153
});
154154

155155
it('decodes dot from unicode', function () {
156156

157157
$translations = PhpTranslations::toUndot([
158-
'This key contains a dot&#46; In the middle.And it&#46;has children&#46;' => 'And it has children&#46;',
158+
'This key contains a dot&#46; In the middle.And it&#46;has children&#46;' => 'And it has children.',
159159
]);
160160

161-
expect($translations->toArray())->toBe([
161+
expect($translations)->toBe([
162162
'This key contains a dot. In the middle' => [
163163
'And it.has children.' => 'And it has children.',
164164
],

0 commit comments

Comments
 (0)