I have a method that translates HTML into another language. Inside that method is a try, catch block that handles network errors. I have another method that returns true if an error occurred. And another that displays the error message.
However, I have not found a way to let either method know when the translation fails.
Here’s the method that handles the actual translation (It is part of a trait called Translate). $status is an array that is a variable in the class TranslateContent that uses this trait.
protected function translateHTML(object $translator, string $content, string $target_language, array $status):string {
if($target_language == 'en'){
return $content;
}
if($content):
try{
$content_translated = $translator->translateText($content, 'en', $target_language,
['tag_handling' => 'html'],
['formality', 'prefer_less'],
['split_sentences', 0]
);
return $content_translated;
} catch(Exception $error){
$status['status'] = true;
$status['message'] = $error->getMessage();
return $content;
}
endif;
return $content;
}
At the top of the class called TranslateContent that uses trait Translate, I declare $status:
protected $status = [
'error' => false,
'message' => ''
];
Then here is the method that uses the translateHTML() method where the exception is thrown. The last argument I’ve added is the status array created above. (Note: I am passing an incorrect target language here to trigger the exception.)
public function translatePostContent(string $content):string {
$post_language = $this->getPostLanguage($this->post_id);
$target_language = $post_language['deepl_language_code'];
return $this->translateHTML($this->translator, $content, 'doesnt-exist', $this->status );
}
And lastly that methods that return the status:
public function hasError():bool{
return $this->status['error'];
}
public function getErrorMessage():string{
return $this->status['message'];
}
My thinking was, if an exception is thrown during the translation attempt, I need to update the state of $status from the within the catch block. But the $status is not being updated. Instead the code where TranslateContent is instantiated runs as if there was no error at all.
I thought this was a scoping issue initially but from what I’ve read, catch blocks do not have scoping in PHP.
What am I missing?
2
Answers
it looks like what you want is to pass "by reference" the
$status
variable.You can do so by redefining your
translateHTML
signature prefixing with&
your$status
variable:That way when you "edit" the variable you will be editing the actual variable passed to the method (and not a copy, which is the default).
The above should work, but maybe you might want to move the $status property to the trait itself. That way you can access it by using
$this->status
and you won’t need to pass it at all.You declare
$status
like this:but your
catch
block makes two mistakes:You should be updating
$this->status
, and you should be setting$this->status['error']
, like this:Then your call to
translateHTML()
has no need to pass in the$status
variable.Your methods to retrieve the error status and message are correctly accessing the class property, so they should now work.
There’s no need to pass
$status
by reference, or at all. That’s what class properties are for.