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

Handle null database values as null in translations #479

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/HasTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function getTranslation(string $key, string $locale, bool $useFallbackLoc

$translations = $this->getTranslations($key);

$translation = $translations[$normalizedLocale] ?? '';
$translation = is_null(self::getAttributeFromArray($key)) ? null : $translations[$normalizedLocale] ?? '';

$translatableConfig = app(Translatable::class);

Expand Down
4 changes: 2 additions & 2 deletions tests/TranslatableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ public function setAttributesExternally(array $attributes)
[['en' => 'english', 'nl' => 'dutch'], ['en', 'nl'], ['english', 'dutch']],
]);

it('should return empty string when the underlying attribute in database is null', function () {
it('should return null when the underlying attribute in database is null', function () {
// we need to remove the name attribute from the translatable array
// and add it back to make sure the name
// attribute is holding `null` raw value
Expand All @@ -864,7 +864,7 @@ public function setAttributesExternally(array $attributes)

$translation = $this->testModel->getTranslation('name', 'en');

expect($translation)->toBe('');
expect($translation)->toBeNull();
});

it('should return locales with empty string translations when allowEmptyStringForTranslation is true', function () {
Expand Down
Loading