Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow ignoring certain translation keys when running validate command #29

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions config/poeditor-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,18 @@
|
*/
'validate_after_download' => true,

/*
|--------------------------------------------------------------------------
| Ignored Translation Keys During Validation
|--------------------------------------------------------------------------
|
| Define translation keys that should be ignored when running the validate
| command. This can be useful when the replacements of a translation key
| can be different in each language, for example when the replacements
| are suffixed with letter that is different is every a language.
| (e.g. ':hourh' in English and ':houru' in Dutch)
|
*/
'ignored_keys_during_validation' => [],
];
4 changes: 4 additions & 0 deletions src/Validation/HasMatchingPluralization.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class HasMatchingPluralization implements ValidationRule
{
public function validate(string $translationKey, mixed $translations, Closure $fail) : void
{
if (collect(config('poeditor-sync.ignored_keys_during_validation'))->contains($translationKey)) {
return;
}

$expectedPluralizationParts = $this->getPluralizationParts($translations[$this->getFallbackLocale()]);

collect($translations)
Expand Down
4 changes: 4 additions & 0 deletions src/Validation/HasMatchingReplacements.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ class HasMatchingReplacements implements ValidationRule
{
public function validate(string $translationKey, mixed $translations, Closure $fail) : void
{
if (collect(config('poeditor-sync.ignored_keys_during_validation'))->contains($translationKey)) {
return;
}

$expectedReplacements = $this->getReplacements($translations[$this->getFallbackLocale()]);

collect($translations)
Expand Down
21 changes: 21 additions & 0 deletions tests/Validation/HasMatchingPluralizationRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,27 @@ public function it_fails_if_pluralization_order_does_not_match()
$this->assertSame(['Invalid pluralization in locale \'nl\''], $validator->errors()->all());
}

/** @test */
public function it_does_not_fail_even_if_pluralization_does_not_match_because_translation_key_should_be_ignored()
{
config()->set('poeditor-sync.ignored_keys_during_validation', ['some_translation_key']);

$validator = $this->getValidator([
'some_translation_key' => [
'en' => '{0} Some singular translation in English|[1, *] Some plural translation in English',
'nl' => '[1, *] Some plural translation in Dutch|{0} Some singular translation in Dutch',
'fr' => '{0} Some singular translation in French|[1, *] Some plural translation in French',
],
]);

$this->assertFalse($validator->fails());

config()->set('poeditor-sync.ignored_keys_during_validation', ['some_other_translation_key']);

$this->assertTrue($validator->fails());
$this->assertSame(['Invalid pluralization in locale \'nl\''], $validator->errors()->all());
}

public function getValidator(array $data)
{
return Validator::make($data, [
Expand Down
21 changes: 21 additions & 0 deletions tests/Validation/HasMatchingReplacementsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,27 @@ public function it_fails_if_replacement_is_not_in_main_locale()
], $validator->errors()->all());
}

/** @test */
public function it_does_not_fail_even_if_replacements_do_not_match_because_translation_key_should_be_ignored()
{
config()->set('poeditor-sync.ignored_keys_during_validation', ['some_translation_key']);

$validator = $this->getValidator([
'some_translation_key' => [
'en' => 'Some translation :foo in English',
'nl' => 'Some translation in Dutch',
'fr' => 'Some translation :foo in French',
],
]);

$this->assertFalse($validator->fails());

config()->set('poeditor-sync.ignored_keys_during_validation', ['some_other_translation_key']);

$this->assertTrue($validator->fails());
$this->assertSame(['Missing replacement key \':foo\' in nl'], $validator->errors()->all());
}

public function getValidator(array $data)
{
return Validator::make($data, [
Expand Down