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

Fallback to existing translation when translating attributes #1919

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
17 changes: 10 additions & 7 deletions packages/core/src/Base/Traits/HasTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function translate($attribute, $locale = null)
}

$locale = $locale ?: app()->getLocale();

$value = Arr::accessible($values) ?
Arr::get($values, $locale) :
get_object_vars($values)[$locale] ?? null;
Expand All @@ -40,12 +41,8 @@ public function translate($attribute, $locale = null)

/**
* Translate a value from attribute data.
*
* @param string $attribute
* @param string $locale
* @return string|null
*/
public function translateAttribute($attribute, $locale = null)
public function translateAttribute(string $attribute, ?string $locale = null): mixed
{
$field = Arr::get($this->getAttribute('attribute_data'), $attribute);

Expand Down Expand Up @@ -74,15 +71,21 @@ public function translateAttribute($attribute, $locale = null)
return $field->getValue();
}

return $value ? $value->getValue() : null;
if (! $value->getValue()) {
return $translations->first(
fn ($value) => $value->getValue()
)?->getValue();
}

return $value->getValue();
}

/**
* Shorthand to translate an attribute.
*
* @return string|null
*/
public function attr(...$params)
public function attr(...$params): mixed
{
return $this->translateAttribute(...$params);
}
Expand Down
27 changes: 27 additions & 0 deletions tests/core/Unit/Base/Traits/HasTranslationsTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

uses(\Lunar\Tests\Core\TestCase::class);

use Lunar\FieldTypes\Dropdown;
use Lunar\FieldTypes\ListField;
use Lunar\FieldTypes\Text;
Expand Down Expand Up @@ -78,6 +79,32 @@
expect($product->translateAttribute('name', 'dk'))->toEqual('English Name');
});

test('can fallback to existing translation when current is missing', function () {
$attributeGroup = AttributeGroup::factory()->create([
'name' => [
'en' => 'English',
'fr' => '',
],
]);

expect($attributeGroup->translate('name', 'fr'))->toEqual('English');

$product = Product::factory()->create([
'attribute_data' => [
'name' => new TranslatedText(collect([
'en' => new Text('English Name'),
'fr' => new Text(''),
])),
'description' => new TranslatedText(collect([
'en' => new Text('English Description'),
'fr' => new Text(''),
])),
],
]);

expect($product->attr('name', 'fr'))->toEqual('English Name');
});

test('can handle null values', function () {
$attributeGroup = AttributeGroup::factory()->create([
'name' => [
Expand Down
Loading