Documentation

TranslationService

Service class for handling translation operations.

Provides methods for translating text using various services including Google Translate and Glosbe API.

Tags
category

Lwt

author

HugoFara hugo.farajallah@protonmail.com

license

Unlicense http://unlicense.org/

link
https://hugofara.github.io/lwt/docs/php/
since
3.0.0

Table of Contents

Methods

buildGlosbeUrl()  : string
Build the Glosbe API URL for a translation request.
buildGoogleTranslateUrl()  : string
Build Google Translate page URL.
createDictLink()  : string
Create a dictionary link by substituting the term in the URL.
getCurrentLanguageTtsVoice()  : string|null
Get the current language's TTS voice API setting.
getTranslatorUrl()  : array{url: string|null, sentence: string|null, error?: string}
Get the translator URL for a sentence.
translateViaGoogle()  : array{success: bool, translations: string[], error?: string}
Translate text using Google Translate.
validateGlosbeParams()  : array{valid: bool, error?: string}
Validate Glosbe API parameters.

Methods

buildGlosbeUrl()

Build the Glosbe API URL for a translation request.

public buildGlosbeUrl(string $phrase, string $from, string $dest) : string
Parameters
$phrase : string

Phrase to translate

$from : string

Source language code

$dest : string

Destination language code

Return values
string

The Glosbe dictionary URL

buildGoogleTranslateUrl()

Build Google Translate page URL.

public buildGoogleTranslateUrl(string $text, string $srcLang, string $tgtLang) : string
Parameters
$text : string

Text to translate

$srcLang : string

Source language

$tgtLang : string

Target language

Return values
string

Google Translate URL

Create a dictionary link by substituting the term in the URL.

public createDictLink(string $dictUrl, string $term) : string
Parameters
$dictUrl : string

Dictionary URL with placeholder

$term : string

Term to substitute

Return values
string

Formatted dictionary URL

getCurrentLanguageTtsVoice()

Get the current language's TTS voice API setting.

public getCurrentLanguageTtsVoice() : string|null
Return values
string|null

The TTS voice API setting or null if not set

getTranslatorUrl()

Get the translator URL for a sentence.

public getTranslatorUrl(int $textId, int $order) : array{url: string|null, sentence: string|null, error?: string}
Parameters
$textId : int

Text ID

$order : int

Order/position in the text

Return values
array{url: string|null, sentence: string|null, error?: string}

translateViaGoogle()

Translate text using Google Translate.

public translateViaGoogle(string $text, string $srcLang, string $tgtLang[, array<string|int, int>|null $timeToken = null ]) : array{success: bool, translations: string[], error?: string}
Parameters
$text : string

Text to translate

$srcLang : string

Source language code (e.g., "es")

$tgtLang : string

Target language code (e.g., "en")

$timeToken : array<string|int, int>|null = null

Optional time token for Google API

Return values
array{success: bool, translations: string[], error?: string}

validateGlosbeParams()

Validate Glosbe API parameters.

public validateGlosbeParams(string $from, string $dest, string $phrase) : array{valid: bool, error?: string}
Parameters
$from : string

Source language code

$dest : string

Destination language code

$phrase : string

Phrase to translate

Return values
array{valid: bool, error?: string}

        
On this page

Search results