Skip to content

Commit

Permalink
Merge pull request #38 from Laravel-Lang/1.x
Browse files Browse the repository at this point in the history
Fixed a bug with processing dynamic names when translating
  • Loading branch information
andrey-helldar authored Feb 19, 2023
2 parents 9743762 + ff42f2a commit 45c79f1
Show file tree
Hide file tree
Showing 20 changed files with 204 additions and 28 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"archtechx/enums": "^0.3.0",
"dragon-code/pretty-array": "^4.0",
"dragon-code/simple-dto": "^2.3",
"dragon-code/support": "^6.2",
"dragon-code/support": "^6.11",
"guzzlehttp/guzzle": "^7.4",
"illuminate/console": "^8.0 || ^9.0 || ^10.0",
"illuminate/container": "^8.0 || ^9.0 || ^10.0",
Expand Down
31 changes: 8 additions & 23 deletions src/Objects/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,22 @@ public function compile(string $translated): string

protected function castValue(string $value): string
{
$this->extractReplaces($value);
$this->extract($value);

return $this->replaceValue($value);
return Str::replace($value, array_keys($this->replaces), array_values($this->replaces));
}

protected function extractReplaces(string $value): void
protected function extract(string $value): void
{
Str::of($value)
->trim()
->trim('.?!')
->explode(' ')
->map(function (string $item) {
if (Str::startsWith($item, ':')) {
return $this->numerify($item);
}

return $item;
});
}

protected function replaceValue(string $value): string
{
return Str::replace($value, array_keys($this->replaces), array_values($this->replaces));
->matchAll('/:\w+/')
->tap(fn (string $match) => $this->keyable($match));;
}

protected function numerify(string $value): int
protected function keyable(string $value): void
{
if (isset($this->replaces[$value])) {
return $this->replaces[$value];
if (! isset($this->replaces[$value])) {
$this->replaces[$value] = (count($this->replaces) + 1) * 1000;
}

return $this->replaces[$value] = (count($this->replaces) + 1) * 100;
}
}
5 changes: 5 additions & 0 deletions tests/Fixtures/Resources/Translate/locales/ar/_excludes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
"Administrator",
"This field must be accepted.",
"The :attribute must be accepted."
]
7 changes: 7 additions & 0 deletions tests/Fixtures/Resources/Translate/locales/ar/json.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"Added.": "Added.",
"Administrator": "Administrator",
"Foo": "Foo",
"Bar.": "Bar.",
"Uploading files... (:current/:total)": "Uploading files... (:current/:total)"
}
7 changes: 7 additions & 0 deletions tests/Fixtures/Resources/Translate/locales/ar/php-inline.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"accepted": "This field must be accepted.",
"accepted_if": "This field must be accepted when :other is :value.",
"active_url": "This field is not a valid URL.",
"custom2": "Custom field 2",
"sub.array2": "Sub Array 2"
}
10 changes: 10 additions & 0 deletions tests/Fixtures/Resources/Translate/locales/ar/php.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"0": "Numeric Zero",
"10": "Numeric Ten",
"100": "Numeric One Hundred",
"accepted": "The :attribute must be accepted.",
"accepted_if": "The :attribute must be accepted when :other is :value.",
"active_url": "The :attribute is not a valid URL.",
"custom": "Custom field",
"sub.array": "Sub Array"
}
3 changes: 2 additions & 1 deletion tests/Fixtures/Resources/Translate/locales/de/json.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"Added.": "Added.",
"Administrator": "Administrator",
"Foo": "Foo",
"Bar.": "Bar."
"Bar.": "Bar.",
"Uploading files... (:current/:total)": "Uploading files... (:current/:total)"
}
3 changes: 2 additions & 1 deletion tests/Fixtures/Resources/Translate/locales/en/json.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"Added.": "Added.",
"Administrator": "Administrator",
"Foo": "Foo",
"Bar.": "Bar."
"Bar.": "Bar.",
"Uploading files... (:current/:total)": "Uploading files... (:current/:total)"
}
5 changes: 5 additions & 0 deletions tests/Fixtures/Resources/Translate/locales/es/_excludes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
"Administrator",
"This field must be accepted.",
"The :attribute must be accepted."
]
7 changes: 7 additions & 0 deletions tests/Fixtures/Resources/Translate/locales/es/json.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"Added.": "Added.",
"Administrator": "Administrator",
"Foo": "Foo",
"Bar.": "Bar.",
"Uploading files... (:current/:total)": "Uploading files... (:current/:total)"
}
7 changes: 7 additions & 0 deletions tests/Fixtures/Resources/Translate/locales/es/php-inline.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"accepted": "This field must be accepted.",
"accepted_if": "This field must be accepted when :other is :value.",
"active_url": "This field is not a valid URL.",
"custom2": "Custom field 2",
"sub.array2": "Sub Array 2"
}
10 changes: 10 additions & 0 deletions tests/Fixtures/Resources/Translate/locales/es/php.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"0": "Numeric Zero",
"10": "Numeric Ten",
"100": "Numeric One Hundred",
"accepted": "The :attribute must be accepted.",
"accepted_if": "The :attribute must be accepted when :other is :value.",
"active_url": "The :attribute is not a valid URL.",
"custom": "Custom field",
"sub.array": "Sub Array"
}
3 changes: 2 additions & 1 deletion tests/Fixtures/Resources/Translate/locales/fr/json.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"Added.": "Added.",
"Administrator": "Administrator",
"Foo": "Foo",
"Bar.": "Bar."
"Bar.": "Bar.",
"Uploading files... (:current/:total)": "Uploading files... (:current/:total)"
}
3 changes: 2 additions & 1 deletion tests/Fixtures/Resources/Translate/source/custom.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"Added.": "Added.",
"Administrator": "Administrator"
"Administrator": "Administrator",
"Uploading files... (:current/:total)": "Uploading files... (:current/:total)"
}
58 changes: 58 additions & 0 deletions tests/Unit/Commands/Translate/ArabicTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Tests\Unit\Commands\Translate;

/**
* @group Translate
*/
class ArabicTest extends Base
{
public function testJson(): void
{
$this->assertJsonFileEqualsJson([
'Added.' => 'مضاف.',
'Administrator' => 'Administrator',

'Uploading files... (:current/:total)' => 'جاري تحميل الملفات ... (:current/:total)',
], 'locales/ar/json.json', __FUNCTION__);
}

public function testPhp(): void
{
$values = $this->filesystem->load($this->tempPath('locales/ar/php.json'));

$this->assertContainsEquals($values[0], ['رقمي صفر']);
$this->assertContainsEquals($values[10], ['رقم عشرة']);
$this->assertContainsEquals($values[100], ['مائة رقمية']);

$this->assertSame($values['accepted'], 'The :attribute must be accepted.');

$this->assertContainsEquals($values['accepted_if'], ['يجب قبول :attribute عندما تكون :other هي :value.', 'يجب قبول :attribute عندما يكون :other هو :value.']);

$this->assertContainsEquals($values['active_url'], [':attribute ليس عنوان URL صالحًا.']);

$this->assertContainsEquals($values['between.array'], ['The :attribute must have between :min and :max items.']);
$this->assertContainsEquals($values['between.file'], ['The :attribute must be between :min and :max kilobytes.']);
}

public function testPhpInline(): void
{
$values = $this->filesystem->load($this->tempPath('locales/ar/php-inline.json'));

$this->assertSame($values['accepted'], 'This field must be accepted.');

$this->assertContainsEquals($values['accepted_if'], ['يجب قبول هذا الحقل عندما يكون :other هو :value.']);

$this->assertContainsEquals($values['active_url'], ['هذا الحقل ليس عنوان URL صالحًا.']);

$this->assertContainsEquals($values['between.array'], ['This field must have between :min and :max items.']);
$this->assertContainsEquals($values['between.file'], ['This field must be between :min and :max kilobytes.']);
}

public function testExcludes(): void
{
$this->assertFileExists($this->tempPath('locales/ar/_excludes.json'));
}
}
2 changes: 2 additions & 0 deletions tests/Unit/Commands/Translate/EnglishTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public function testJson(): void
$this->assertJsonFileEqualsJson([
'Added.' => 'Added.',
'Administrator' => 'Administrator',

'Uploading files... (:current/:total)' => 'Uploading files... (:current/:total)',
], 'locales/en/json.json', __FUNCTION__);
}

Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/Commands/Translate/FrenchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public function testJson(): void
$this->assertJsonFileEqualsJson([
'Added.' => 'Ajoutée.',
'Administrator' => 'Administrator',

'Uploading files... (:current/:total)' => 'Téléchargement de fichiers... (:current/:total)',
], 'locales/fr/json.json', __FUNCTION__);
}

Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/Commands/Translate/GermanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public function testJson(): void
$this->assertJsonFileEqualsJson([
'Added.' => 'Hinzugefügt.',
'Administrator' => 'Administrator',

'Uploading files... (:current/:total)' => 'Dateien werden hochgeladen... (:current/:total)',
], 'locales/de/json.json', __FUNCTION__);
}

Expand Down
4 changes: 4 additions & 0 deletions tests/Unit/Commands/Translate/PerLocaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ public function testJson(): void
$this->assertJsonFileEqualsJson([
'Added.' => 'Hinzugefügt.',
'Administrator' => 'Administrator',

'Uploading files... (:current/:total)' => 'Dateien werden hochgeladen... (:current/:total)',
], 'locales/de/json.json', __FUNCTION__);

$this->assertJsonFileEqualsJson([
'Added.' => 'Added.',
'Administrator' => 'Administrator',
'Foo' => 'Foo',
'Bar.' => 'Bar.',

'Uploading files... (:current/:total)' => 'Uploading files... (:current/:total)',
], 'locales/fr/json.json', __FUNCTION__);
}

Expand Down
61 changes: 61 additions & 0 deletions tests/Unit/Commands/Translate/SpanishTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace Tests\Unit\Commands\Translate;

/**
* @group Translate
*/
class SpanishTest extends Base
{
public function testJson(): void
{
$this->assertJsonFileEqualsJson([
'Added.' => 'Agregado.',
'Administrator' => 'Administrator',

'Uploading files... (:current/:total)' => 'Subiendo archivos... (:current/:total)',
], 'locales/es/json.json', __FUNCTION__);
}

public function testPhp(): void
{
$values = $this->filesystem->load($this->tempPath('locales/es/php.json'));

$this->assertContainsEquals($values[0], ['Cero numérico', 'cero numérico']);
$this->assertContainsEquals($values[10], ['Numérico Diez']);
$this->assertContainsEquals($values[100], ['Cent numérique', 'Numérico Cien']);

$this->assertSame($values['accepted'], 'The :attribute must be accepted.');

$this->assertContainsEquals(
$values['accepted_if'],
['El campo :attribute debe ser aceptado cuando :other sea :value.', 'El :attribute debe aceptarse cuando :other es :value.']
);

$this->assertContainsEquals($values['active_url'], ['El campo :attribute debe ser una URL válida.', 'El :attribute no es una URL válida.']);

$this->assertContainsEquals($values['between.array'], ['The :attribute must have between :min and :max items.']);
$this->assertContainsEquals($values['between.file'], ['The :attribute must be between :min and :max kilobytes.']);
}

public function testPhpInline(): void
{
$values = $this->filesystem->load($this->tempPath('locales/es/php-inline.json'));

$this->assertSame($values['accepted'], 'This field must be accepted.');

$this->assertContainsEquals($values['accepted_if'], ['Este campo debe ser aceptado cuando :other sea :value.', 'Este campo debe aceptarse cuando :other es :value.']);

$this->assertContainsEquals($values['active_url'], ['Este campo debe ser una URL válida.', 'Este campo no es una URL válida.']);

$this->assertContainsEquals($values['between.array'], ['This field must have between :min and :max items.']);
$this->assertContainsEquals($values['between.file'], ['This field must be between :min and :max kilobytes.']);
}

public function testExcludes(): void
{
$this->assertFileExists($this->tempPath('locales/es/_excludes.json'));
}
}

0 comments on commit 45c79f1

Please sign in to comment.