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

Improve setAttribute to handle array list as value for translation #469

Merged

Conversation

alipadron
Copy link
Contributor

@alipadron alipadron commented Dec 9, 2024

This PR enhances the setAttribute method in the HasTranslations trait to handle array values for translatable attributes.

Motivation

Previously, setting an array value for a translatable attribute would result in unexpected behavior, treating array indexes as locale keys. For example:

$model->additional_info = ['Feature 1', 'Feature 2', 'Feature 3'];

Would store:

{
  "0": "Feature 1",
  "1": "Feature 2",
  "2": "Feature 3"
}

With this change, the array is now stored as a translation for the current locale:

{
  "en": ["Feature 1", "Feature 2", "Feature 3"]
}

Tests were added to verify the correct behavior of the setAttribute method with array values.

@freekmurze freekmurze merged commit a7c6aa1 into spatie:main Dec 11, 2024
19 checks passed
@freekmurze
Copy link
Member

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.

2 participants