-
Notifications
You must be signed in to change notification settings - Fork 917
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
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: António Almeida <promatik@gmail.com>
[ci skip] [skip ci]
[ci skip] [skip ci]
If this gets merged #3932 should be closed! |
[ci skip] [skip ci]
There was a problem hiding this 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) |
There was a problem hiding this comment.
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
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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...
private static function getNonTranslableAttributes($model, $attributes) | |
private function getNonTranslableAttributes($attributes) |
Co-authored-by: Cristian Tabacitu <cristitabacitu@gmail.com>
[ci skip] [skip ci]
@tabacitu kindly review the changes 👍 I've refactored method signatures and cleaned up the functions. |
Hello @pxpm |
Hi @pxpm did this need merge with master? because i get an error. Cheers. |
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 |
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: In database was created: {"en": "value en", "fr": "value fr"} 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 Idk if this need to be fixed by this branch or is something who developer need to prevent. Please let me know, thanks. Cheers. |
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']])
.