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

[9.x] Translation of the default message of the validator #43837

Merged
merged 1 commit into from
Aug 24, 2022

Conversation

andrey-helldar
Copy link
Contributor

@andrey-helldar andrey-helldar commented Aug 24, 2022

Before:

{
    "message": "The given data was invalid.",
    "errors": {
        "email": [
            "Taki adres e-mail już występuje."
        ]
    }
}

{
    "message": "Taki adres e-mail już występuje. (and 1 more error)",
    "errors": {
        "email": [
            "Taki adres e-mail już występuje."
        ],
        "name": [
            "Taki nazwa już występuje."
        ]
    }
}

After

{
    "message": "Podane dane były nieprawidłowe.",
    "errors": {
        "email": [
            "Taki adres e-mail już występuje."
        ]
    }
}

{
    "message": "Taki adres e-mail już występuje. (i jeszcze 1 błąd)",
    "errors": {
        "email": [
            "Taki adres e-mail już występuje."
        ],
        "name": [
            "Taki nazwa już występuje."
        ]
    }
}

On line 99, I deliberately put the space character outside the translation function, as it can be confusing for developers, namely, they can compose translations as "(and :count more errors)" instead of " (and :count more errors)" and will not understand why the translation does not work.

@andrey-helldar andrey-helldar force-pushed the patch/2022-08-24/14-46 branch from 74b5122 to 68fabaa Compare August 24, 2022 12:13
@andrey-helldar andrey-helldar marked this pull request as draft August 24, 2022 12:16
@andrey-helldar andrey-helldar force-pushed the patch/2022-08-24/14-46 branch from 68fabaa to 6941ecf Compare August 24, 2022 12:20
@andrey-helldar andrey-helldar marked this pull request as ready for review August 24, 2022 12:20
@igorgaming
Copy link

Finally it was accepted.
Before that there were a bunch of attempts at the same changes that were closed.

@andrey-helldar andrey-helldar deleted the patch/2022-08-24/14-46 branch August 24, 2022 20:20
@Elnadrion
Copy link

It's a cool thing, thanks!

@Pablo-Camara
Copy link

Pablo-Camara commented Sep 29, 2022

Tried this and none worked ( in validation.php translation file ):

' (and :count more errors)' => 'Test',
'(and :count more errors)' => 'Test',
' (and :count more error)' => 'Test',
'(and :count more error)' => 'Test',
' (and 1 more error)' => 'Test',
'(and 1 more error)' => 'Test',

@andrey-helldar
Copy link
Contributor Author

andrey-helldar commented Sep 29, 2022

@Pablo-Camara, you need to add this to json file like fr.json, not a validation.php.

For example, lang/fr.json:

{
    "(and :count more error)": "(et :count erreur en plus)",
    "(and :count more errors)": "(et :count erreurs en plus)",
}

You can also install a localization publisher for your project: publisher.laravel-lang.com

@arg2009
Copy link

arg2009 commented Sep 30, 2022

For anyone else who like us uses PHP files rather than JSON files for translations.

We used a Service Provider to inject language strings from validation.php into the Lang lines so that the translation system would pick them up as if they were sourced from JSON files.

class ValidationServiceProvider extends ServiceProvider
{
    public const JSON_INJECTION_KEY = 'json-injection';

    public function boot(): void
    {
        // ...
        $this->injectJsonTranslations();
    }

    /**
     * Because of Laravel's ValidationException::summarize function only supports JSON translations,
     * we are injecting some strings into Laravel's Lang engine to make the function's translation
     * work with php lang files.
     *
     * @see ValidationException::summarize()
     */
    protected function injectJsonTranslations(): void
    {
        $locales = collect(scandir(lang_path()))
            ->filter(fn (string $locale) => ! Str::startsWith($locale, '.'));

        $locales->map(function (string $locale): void {
            $langFilePath = lang_path("{$locale}/validation.php");

            if (! file_exists($langFilePath)) {
                return;
            }

            $languageStrings = require $langFilePath;

            if (array_key_exists(self::JSON_INJECTION_KEY, $languageStrings)) {
                foreach ($languageStrings[self::JSON_INJECTION_KEY] as $key => $value) {
                    Lang::addLines(["*.{$key}" => $value], $locale);
                }
            }
        });
    }
}

So our validation.php file looks like this:

<?php

return [
    // ...
    
    /**
     * @see ValidationServiceProvider::injectJsonTranslations
     */
    \App\Providers\ValidationServiceProvider::JSON_INJECTION_KEY => [
        '(and :count more error)' => '(and :count more error)',
        '(and :count more errors)' => '(and :count more errors)',
    ],
];

@Pablo-Camara
Copy link

@andrey-helldar that worked! My mistake trying to add in the validation.php , thanks for the help!

taylorotwell pushed a commit that referenced this pull request Mar 13, 2023
…tor" (#46378)

* [9.x] Update CHANGELOG.md

* Update facade docblocks

* [9.x] Update CHANGELOG.md

* Revert "[9.x] Translation of the default message of the validator (#43837)"

This reverts commit a833a88.

---------

Co-authored-by: Tetiana Blindaruk <t.blindaruk@gmail.com>
Co-authored-by: TBlindaruk <TBlindaruk@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants