Skip to content

Allow manual translating field attributes #4271

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

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open

Allow manual translating field attributes #4271

wants to merge 26 commits into from

Conversation

pxpm
Copy link
Contributor

@pxpm pxpm commented Mar 16, 2022

WHY

BEFORE - What was wrong? What was happening before this PR?

This is a follow-up of #3932 , that PR made it work in simpler scenarios, but would fail if your translatable attribute value was a simple array and not the expected [en => smt, fr => smt_fr]

I pushed the main PR so @kolaente and @promatik show as contributors here too.

AFTER - What is happening after this PR?

This PR checks if the array keys correspond to available locales, if they do it sets the translations, if not just follow the regular saving process.

Is it a breaking change?

I don't think so, no. There was a bug previously that we are fixing here.

How can we test the before & after?

Try manually assigning translations to a model when creating it: Model::create(['translatable_field' => ['en' => 'value en', 'fr' => 'value fr']]).

@pxpm pxpm added the Bug label Mar 16, 2022
@pxpm
Copy link
Contributor Author

pxpm commented Mar 16, 2022

If this gets merged #3932 should be closed!

@tabacitu tabacitu requested review from promatik and removed request for promatik March 17, 2022 08:48
@tabacitu tabacitu assigned pxpm and unassigned tabacitu Mar 17, 2022
@pxpm pxpm assigned tabacitu and unassigned pxpm Mar 17, 2022
pxpm and others added 2 commits March 17, 2022 12:00
Copy link
Member

@tabacitu tabacitu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very very clean PR, good job @pxpm 👏👏👏 It leaves the campsite cleaner than we found it 😉

I only noticed on thing about the static methods, other than that... looks good to me!

* @param string $locale
* @return \Illuminate\Database\Eloquent\Model
*/
private static function fillModelWithTranslations($model, array $attributes, string $locale)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... I don think it's appropriate to make this a static method... If it requires the $model as a parameter, it kind of defeats the purpose of static methods... those would work without an instance.

Is there a reason for it to be static, that I'm missing here Pedro?

Otherwise, I say let's make this

Suggested change
private static function fillModelWithTranslations($model, array $attributes, string $locale)
private function fillModelWithTranslations(array $attributes, string $locale)

and use $this inside it. Which would also clean up the code in create() and update() a bit, too. No that it needed it... very clean code in this PR (👏👏👏 )... but yeah.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @tabacitu

I don't think it's possible to remove the static from create, I recall when working on this I did a research on why the update was not static but create was. You can call Model::create() because Laravel uses the __callStatic method that handle the call for the public method create in the Builder (yes, create is not a "direct" model method), since we are overriding the create() method, the static synthax Model::create() would not be available and fail saying that a non-static method should not be called statically.

What happens is: you call ::create(), it will hit __callStatic(), since create() is not a model method, then will hit __call() and foward the call with the newQuery that's the builder where create exists.

So nop, I don't think we can remove the static from there, or am I missing something ?

Cheers

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I... don't understand 😅 sorry

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're talking about our own custom methods here, aren't we? Not ::create.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, as you can see, I was specifically talking about create. Kindly disregard, if you didn't know about this static interchange in Laravel model with Builder now you know 👍

* @param array $attributes
* @return array
*/
private static function getNonTranslableAttributes($model, $attributes)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here... it's not really a static method if it has the instance as a parameter)... We either make it not need an instance... or...

Suggested change
private static function getNonTranslableAttributes($model, $attributes)
private function getNonTranslableAttributes($attributes)

@tabacitu tabacitu assigned pxpm and unassigned tabacitu Apr 4, 2022
@pxpm
Copy link
Contributor Author

pxpm commented Apr 4, 2022

@tabacitu kindly review the changes 👍 I've refactored method signatures and cleaned up the functions.

@lmout82
Copy link

lmout82 commented Aug 9, 2022

Hello @pxpm
I tested the changes on a model with translatable and non-translatable attributes. Everything seems to work fine.
Cheers

@pxpm pxpm assigned jcastroa87 and unassigned tabacitu Aug 17, 2022
@jcastroa87
Copy link
Member

Hi @pxpm did this need merge with master? because i get an error.

screenshot-backpack test-2022 09 24-11_22_34

Cheers.

@jcastroa87 jcastroa87 assigned pxpm and unassigned jcastroa87 Sep 24, 2022
@pxpm
Copy link
Contributor Author

pxpm commented Sep 26, 2022

I just merged master here. Next time feel free to do it yourself, if there are no code changes/conflicts you can merge master on my PR's, I am ok with it.

Cheers

@pxpm pxpm assigned jcastroa87 and unassigned pxpm Sep 26, 2022
@jcastroa87
Copy link
Member

Hello @pxpm i do merge with main again because in my end was needed.

I was checking on this branch, everything look to working normally, but there still something strange, let me explain.

I choose Products, and i add this:
\App\Models\Product::create(['name' => ['en' => 'value en', 'fr' => 'value fr']]);

In database was created: {"en": "value en", "fr": "value fr"}

screenshot-backpack test-2022 10 03-09_54_20

As you see everything look great on this example, but now i tried to add translation from a language who is nos enable:

\App\Models\Product::create(['name' => ['en' => 'value en', 'fr' => 'value fr', 'de' => 'value de']]);

In database was created: {"en": {"de": "value de", "en": "value en", "fr": "value fr"}}

And i get wrong results

screenshot-backpack test-2022 10 03-09_54_49

Idk if this need to be fixed by this branch or is something who developer need to prevent.

Please let me know, thanks.

Cheers.

@jcastroa87 jcastroa87 assigned pxpm and unassigned jcastroa87 Oct 3, 2022
@pxpm pxpm assigned tabacitu and unassigned pxpm Nov 1, 2022
@tabacitu tabacitu assigned pxpm and unassigned tabacitu Apr 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

5 participants