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 null value in translations if allowNullForTranslation is true #488

Merged

Conversation

dont-know-php
Copy link
Contributor

Hi there,

When saving a translation with the null value an empty string will be returned when retrieving a translation. This behavior was discussed here #195 and was attempted to be fixed here #427 , but caused errors in projects using a string as the default value.

These errors were fixed in the PR #465 by adding allowNullForTranslation configuration which is false by default.

But it still returns an empty string when retrieving a translation with a null value. This PR fixes this behavior and returns null instead of an empty string if allowNullForTranslation is true.

@dont-know-php dont-know-php force-pushed the feature/allow-null-value-in-translations branch from db3d95b to 38b3e08 Compare February 12, 2025 21:09
Use existing object instead of retrieving the same singleton object from container
@dont-know-php dont-know-php force-pushed the feature/allow-null-value-in-translations branch from 38b3e08 to 8b4432d Compare February 12, 2025 21:09
@freekmurze freekmurze merged commit 2053c0c into spatie:main Feb 14, 2025
19 checks passed
@freekmurze
Copy link
Member

Thank you!

@ManuelDiazVarelisFC
Copy link

how can I set the allowNullForTranslation flag to false by default in my project?

@dont-know-php
Copy link
Contributor Author

hi @ManuelDiazVarelisFC

how can I set the allowNullForTranslation flag to false by default in my project?

allowNullForTranslation is false by default. You can use the Translatable facade inside the Service Provider (boot method) to change the package settings, for example:

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Spatie\Translatable\Facades\Translatable;
 
class MyServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        Translatable::allowNullForTranslation(false);
    }
}

@ManuelDiazVarelisFC
Copy link

hi @ManuelDiazVarelisFC

how can I set the allowNullForTranslation flag to false by default in my project?

allowNullForTranslation is false by default. You can use the Translatable facade inside the Service Provider (boot method) to change the package settings, for example:

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Spatie\Translatable\Facades\Translatable;
 
class MyServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        Translatable::allowNullForTranslation(false);
    }
}

Exactly what I needed. Thanks!

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.

3 participants