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

Implement translatable collection. #360

Open
wants to merge 4 commits into
base: 3.x
Choose a base branch
from

Conversation

sten
Copy link

@sten sten commented Mar 14, 2025

I implemented the translatable collection.

I had to remove the cast to collection of the values column, because laravel-translatable casts all translatable columns to array.

As @atmonshi mentioned in #359, this feature is not compatible with existing data. I looked a bit at options to fix this via the booted() function of a model, but there is no event when data is loaded into the model. One option would be override the getTranslation() function on the model, but this is a complicated function, causing this implementation to break in the future.

I think the cleanest way is probably to create a migration script. This should be quite easy and can be done on query level, see spatie/laravel-translatable#111
What do you think?

Copy link

what-the-diff bot commented Mar 14, 2025

PR Summary

  • Enhanced Translation Support
    Introduced Translatable concern and HasTranslations trait in CreateCollection.php, EditCollection.php, ListCollections.php, and Collection.php. These modifications build a foundation to support different languages for our users.

  • Added Locale Switching Functionality
    Incorporated the LocaleSwitcher in various places (getHeaderActions method) within CreateCollection.php, EditCollection.php, and ListCollections.php. This new feature will allow users to switch languages easily.

  • Updated Data Structure
    In Collection.php, updated the format for name and values. Both are now under the public $translatable instead of the $casts. This helps in keeping translated elements in a unified manner.

  • Enhanced Data Management
    A new getValuesAttribute method has been added in Collection.php which aids in casting values into a collection format. This implementation improves data handling overall and augments the usability of translated values.

@sten sten mentioned this pull request Mar 14, 2025
@atmonshi
Copy link
Member

I'll get into it this Friday, in the mean time, I added to the demo app and let the community helps with this
I'll keep an eye if we got any issues
https://demo.larazeus.com/admin/collections

@atmonshi
Copy link
Member

how the values stored on the db in your app?
I got this:

{"en":{"fb8d4b1f-0f21-457f-a055-de42cbdc9856":{"itemValue":"key 1","itemKey":"1","itemIsDefault":false},"1a2c6a3e-66c9-47e8-8a9b-7789c3a9b619":{"itemValue":"key 2","itemKey":"2","itemIsDefault":false}},"es":[{"itemValue":"key es","itemKey":"1","itemIsDefault":false},{"itemValue":"key es 2","itemKey":"2","itemIsDefault":false}],"pt":[{"itemValue":"en","itemKey":"1","itemIsDefault":false}],"ko":[{"itemValue":"en","itemKey":"1","itemIsDefault":false}]}

i think its related to the Translatable plugin bug. so I have to check that first.
another point: if the item key is different per lang, the values wont show in the show response page.
lets say the user fill the form with the active lang: ES. and in the admin I view the response with EN, the value wont show.

I think now I remember why I didnt enable the translatable for collection haha.

either way I'll try my best to address these issues, in the mean time, you may consider using the fork.

@atmonshi
Copy link
Member

also check this one:
I managed to handle the old values without the need for a migration or a command:

protected function values(): Attribute

@sten
Copy link
Author

sten commented Mar 21, 2025

@atmonshi I have the same format:

{"nl":{"5389a60a-7773-42a0-ba8a-f1a8f87705d9":{"itemValue":"Test NL","itemKey":"test","itemIsDefault":false}},"fr":[{"itemValue":"Test FR","itemKey":"test","itemIsDefault":false},{"itemValue":"Test 2 FR","itemKey":"test2","itemIsDefault":false}]}

What spatie-translatable bug are you talking about?

With regard to having all items in each language, I have also been pondering the options. But it is complicated, you can consider adding them automatically to each language (e.g. on the saving event), but for deletion it is maybe less save to remove them in all languages, or we need to add a tooltip on the delete button that explains this.

@atmonshi
Copy link
Member

What spatie-translatable bug are you talking about?

this one: lara-zeus/translatable#23

adding them automatically

this sound like a good idea, also was thinking about enable the keys for the default language only and disable it for others.

will continue after the weekend, happy friday :)

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