For example the canTranslate() and isAvailable() methods on translators.
Basically, that object would have two methods, getSuccess() (or similar) and getMessage().
Something like this:
// Ok.
return new TMGMTResponse(TRUE);
// Not ok with a message.
return new TMGMTResponse(FALSE, 'Denied because of @argument', array('@argument' => $bla))
This allows us to remove the TranslatorInterface::getNotAvailableReason() and TranslatorInterface::getNotCanTranslateReason() method and same for TranslatorPluginInterface.
Update:
A new abstract class called TranslatorResult is being added, from which subclasses such as AvailableResult and TranslatableResult can be created and used to simplify the process of retrieving a bool value and a message as well. This would be useful to drop methods from translator. (such as isAvailable and canTranslate)
In this way we can provide a simple response without having a method that is object dependent as well as solution for methods/objects that require a response with a message.
Furthermore this needs to be implemented in TMGMT_microsoft, TMGMT_gengo and TMGMT_google.
| Comment | File | Size | Author |
|---|---|---|---|
| #75 | interdiff-1420364-70-75.txt | 3.52 KB | juanse254 |
| #75 | Response_object-1420364-75.patch | 24.5 KB | juanse254 |
| #71 | Response_object-1420364-71.patch | 22.79 KB | juanse254 |
| #71 | interdiff-1420364-70-71.txt | 1.59 KB | juanse254 |
| #70 | Response_object-1420364-70-interdiff.txt | 11.24 KB | berdir |
Comments
Comment #1
fubhy commentedAye... Captain fubhy wants that, YarrrrRRR
Comment #2
anybodyThe problem still exists, as you can see in the tmgmt_microsoft module. The functionality you describe is very important and much required as general solution! For example see #2009946: Microsoft translator can not translate from English to * but the problem also exists with other translation plugins!
Comment #3
miro_dietikerComment #4
miro_dietikerOh yeah, that's a major 8.x thingy because we want to have clean interfaces.
Comment #5
juanse254 commentedMaybe something like this will do it.
Comment #6
mbovan commentedOne space after class name.
A comment like: "Constructs a new TMGMTResponse object."
publicaccess modifier is missing.Why not use the same variable name (
$bit) as a parameter too?Comment #7
miro_dietiker$bitwise should be named $success
Also $data is typically named different:
SafeMarkup::format and also StringTranslation name it $args
(Monitoring::addMessage() names it $variables)
I'd say we follow core naming.
Also, next step is using the class in the methods Berdir outlined.
While working on this issue here, you should checkout all current (google, microsoft, gengo) translators and create tickets there... And create a similar issue that switches to the new implementation. We want to commit this API change in sync on all current projects, when we see all fixes combined pass locally.
Comment #8
juanse254 commentedApplied, creating followups subsequently.
Comment #9
miro_dietikerTRUE true or True?
You can not put using this response object in a followup. It needs to be used somehow with this patch and test covered.
Comment #10
juanse254 commentedAdded test coverage and implemented object instead of getNotAvailableReason() method.
Comment #11
miro_dietikerI think isAvailable should return the $result with a proper message. The form should just extract the text and display it as error.
Comment #12
juanse254 commentedUploading changes so far, changed the isAvailable, working on canTranslate().
Comment #13
juanse254 commentedSome changes for canTRanslate still some work needed.
Comment #15
berdirCore has an interesting pattern with static methods, for example for access results. "return AccessResult::allowed()".
Wondering if we can use something similar, but I'm not sure how how to call those methods.
Getting there, but now you call that method twice. You need to call it once and keep the returned object to get the message.
Comment #16
berdirI like Result in the class name.. so maybe AvailableResult::yes() or ::no($message) ? and TranslatableResult accordingly.
Comment #17
juanse254 commentedThis is working so far, need to apply the 2. suggestion.
Comment #18
miro_dietikerWe have a $translator in this context. Why get back to the $job?
What @translator? This case should not exist.
Fix the desc.
Too much space.
$translator->label();
Comment #19
juanse254 commentedokay this should solve that.
Comment #20
edurenye commentedI think that you should add a message with this FALSE response.
Should be can not or can't, but not cant
Should only be one emply line between the functions.
Should not delete this empty line.
Comment #21
juanse254 commentedComment #22
edurenye commentedNow it looks fine for me.
Comment #23
LKS90 commentedt()marks the string as translatable and is meant for translatable strings, what you want is[edit:] actually, that marks the string safe for displaying, still too much. You actually just want to concatenate the job id at the of the path:SafeMarkup::format()$this->drupalGet('admin/tmgmt/jobs/' . $job->id());Why not assert the field error itself?
Test translator (auto created) can not translate from English to German.Comment #24
LKS90 commentedComment #25
juanse254 commentedOkay, fixed.
Comment #26
LKS90 commentedMove the bracket up.
nitpick: it's not bit anymore
After that I'll test a little bit and then set it to RTBC.
Comment #27
juanse254 commentedDone.
Comment #28
LKS90 commentedI wonder why you ever changed that.
Missing comma.
Comment #29
LKS90 commentedComment #30
juanse254 commentedokay this may do that as well.
Comment #31
LKS90 commentedStill too much... The line you added in patch #21 was wrong, basically revert that change, have the format just like the coding standards / take a look at the Code Sniffer errors.
Comment #32
juanse254 commentedOkay.
Comment #33
LKS90 commentedCreated followups for the translator plugins, added them to the issue overview.
Comment #34
LKS90 commentedLooks good now.
Comment #35
berdirMost of my feedback above has not been addressed, it still has the wrong class name and unchanged method names, see #15 and #16:
Add a Drupal\tmgmt\Translator namespace, and in there, provide an abstract TranslatorResult with two subclasses AvailableResult, TranslatableResult, yes/no factory methods and getReason() (all generic) and isAvailable()/isTranslatable() methods (on the specific class). And as mentioned there, it doesn't make sense to have isAvailable() method on Translator that returns an object, so as suggested, rename the methods there to checkAvailable() and checkTranslatable(), which then return a result.
This is quite a change for translators and I want to get it right the first time and avoid having to change them three times :)
Also, just like with the language mapping issue, please provide a patch for all the linked translators (thanks for creating the issues) so that we know that this is solid and works for them (and actually simplifies their code).
Issue summary needs to be updated to explain the changes done here.
Comment #36
miro_dietikerBumping this issue.
Note that the issue #2543564: Rename $controller to $plugin with recent activity will break previous patches.
Comment #37
juanse254 commentedSome work still needed, feedback apreciated.
Comment #38
juanse254 commentedComment #39
juanse254 commentedSome small changes based on suggestions.
Edit: that interdiff is totally wrong.
Comment #40
edurenye commentedAdd description of the class and the methods.
Same for these class
Extend description also here.
Comment #41
berdirThis issue made a wrong turn at some point.
* The only methods that need to be removed are the reason methods as they will be part of the result object
* The result objects do *not* implement any logic. They are only value objects.
As I wrote above:
Comment #42
miro_dietikerClarified the summary a bit.
Comment #43
arla commentedRerolled
Comment #44
juanse254 commentedComment #48
juanse254 commentedOkay maybe something like this?
Comment #49
giancarlosotelo commentedSome nitpicks, otherwise looks good to me.
Missing a dot.
Same as 1
Extra space
Same as 3
Comment #50
miro_dietiker:-)
I really don't get why everyone is trying to refactor the whole architecture (and in a different way than what was defined).
The only intention was to introduce the response object and make the methods like $translator->canTranslate() or $translator->isAvailable() return the object instead of TRUE/FALSE. Sascha clearly stated that the only methods that should disappear are getNotAvailableReason and getNotCanTranslateReason.
The response object needs to be an instance as a result of the method. There are no static calls.
The only thing the response object should provide is a factory to simplify construction.
All these static, abstract and derived classes look like a mismatch in abstraction and limiting flexibility: A translator can no more implement an own logic.
Not sure if it makes sense to proceed with the current setup. Possibly Sascha or i should provide the implementation.
Comment #51
juanse254 commentedThis should be the procedure then, up to this point the isAvailable and canTranslate haven't been renamed yet. Will do if following the right track.
Comment #53
juanse254 commentedsorry forgot to make a call to a method.
Comment #57
miro_dietikerYeah we are kindof back on track... :-)
Do not run the check twice. Treat the method like you get back a complex result. Run it first $translator->isAvailable() and then persist it in a var. Same applies for all subsequent cases also canTranslate().
Accidentally unchanged. Search for all calls and fix them! :-)
These static methods should be the only static ones that exist on TranslatorResult and should do:
$result = new static();
The TranslatableResult and AvailableResult are just class definitions without any method.
I don't see why these should be static. The response message and success needs to be inside the instance.
Comment #58
juanse254 commentedFixed that, now i can edit the methods name. Any suggestion?
Comment #60
arla commentedThe latest patch is wrongly created, it's missing some of the latest changes on the main branch.
Comment #61
miro_dietikerOn the way to the solution. :-)
return $this->success;
And all others similar.
Please rebase! You are reverting fixed things!
Comment #63
juanse254 commentedOkay yeah sorry about the rebase, this should be it. :)
Comment #67
miro_dietikerSome test fails are from #2579395: Test fails due to recent core change with FormattableString
No, checkAvailable() provides the result containing the message...
You still call checkTranslatable() twice.
Comment #70
berdirOk, finally found time to work on this. Cleaned things up, renames, fixes. Tests passed for me locally.
Comment #71
juanse254 commentedAfter reviewing with a different translator seems like we missed the auto-escape and the return message of the translatorbase is wrong. When a translator is not available it should be configured, not shown that the target language of translation is not available.
Comment #74
berdirNo, this change is not correct. This is the can translate check, which as you can see in the old removed method below, returned the can not translate from source to target language message.
The tests correctly failed on that.
This check is about whether a given translator can translate a specific job from e.g. english to chinese. If the translator doesn't support that combination, then he just doesn't, it is nothing that you can fix with configuration. (unless it can be done with mappings but that's a different topic).
The link change looks good but we should have a test somewhere that covers that.
Comment #75
juanse254 commentedThis patch adds test coverage as well as the fix and the implementation for not_available in test translator.
Comment #76
berdirOk, time to get this in!
Committed and pushed.