Member-only story
Use SharpAPI for Translating E-Commerce Product Info
2 min readMay 14, 2024
In Auctibles, we use SharpAPI to translate product info.
Auctibles is built with the Laravel PHP framework.
The Service
We defined a service that will translate a text into a given language.
The service provides a translate
function that translates the text
parameter into the required language:
<?php
namespace App\Services;
use GuzzleHttp\Exception\ClientException;
class SharpAPI
{
private $service;
public function __construct()
{
}
public function translate(string $text, string $to_language)
{
try {
$statusUrl = \SharpApiService::translate($text, $to_language);
} catch(ClientException $e) {
$rsp = $e->getResponse();
return ['flag' => null];
}
$result = \SharpApiService::fetchResults($statusUrl);
$res = json_decode($result->getResultJson());
return ['flag' => true, 'content' => $res->content];
}
}
The function returns an associative array, with:
bool flag
- signifying success or failurecontent
- translated content