Documentation

TranslationController extends BaseController
in package

Controller for translation API endpoints.

Handles:

  • Google Translate API (/api/google)
  • Glosbe API (/api/glosbe)
  • Generic translation (/api/translate)
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

Properties

$dictionaryService  : DictionaryAdapter
Dictionary adapter instance
$translationService  : TranslationService
Translation service instance

Methods

__construct()  : mixed
Create a new TranslationController.
getTranslationService()  : TranslationService
Get the translation service.
glosbe()  : void
Glosbe API endpoint.
google()  : void
Google Translate endpoint.
translate()  : void
Generic translation endpoint.
endRender()  : void
End page rendering with standard LWT footer.
execute()  : int
Execute an INSERT/UPDATE/DELETE query.
get()  : string
Get a string GET parameter.
getMarkedIds()  : array<string|int, int>
Get IDs from marked checkboxes.
getValue()  : mixed
Get a single value from the database.
hasParam()  : bool
Check if a parameter exists in the request.
isGet()  : bool
Check if the request is a GET request.
isPost()  : bool
Check if the request is a POST request.
json()  : JsonResponse
Return JSON response.
message()  : void
Display a message (success/error) to the user using Bulma notifications.
param()  : string
Get a string request parameter (GET, POST, or REQUEST).
paramArray()  : array<string|int, mixed>
Get an array request parameter.
paramInt()  : int|null
Get an integer request parameter.
post()  : string
Get a string POST parameter.
processTranslationRequest()  : void
Process a translation request.
query()  : mysqli_result|bool
Execute a database query using the LWT query wrapper.
redirect()  : RedirectResponse
Redirect to another URL.
render()  : void
Start page rendering with standard LWT header.
renderGlosbeScript()  : void
Render the JavaScript for Glosbe translation.
renderGoogleTranslation()  : void
Render Google translation results.
renderSentenceTranslation()  : void
Render sentence translation result.
renderTermTranslation()  : void
Render term translation results.
requireInt()  : int
Get a required integer request parameter.

Properties

Methods

glosbe()

Glosbe API endpoint.

public glosbe(array<string|int, mixed> $params) : void

Displays the Glosbe dictionary interface for word translation.

Request parameters:

  • from: Source language code (Glosbe format)
  • dest: Target language code (Glosbe format)
  • phrase: Word or expression to translate
Parameters
$params : array<string|int, mixed>

Route parameters

google()

Google Translate endpoint.

public google(array<string|int, mixed> $params) : void

Translates text using Google Translate API.

Request parameters:

  • text: Text to translate
  • sl: Source language code
  • tl: Target language code
  • sent: (optional) If set, use sentence mode
Parameters
$params : array<string|int, mixed>

Route parameters

translate()

Generic translation endpoint.

public translate(array<string|int, mixed> $params) : void

Handles sentence translation and dictionary lookups.

Request parameters:

  • x: Operation type (1=sentence translation, 2=dictionary lookup)
  • t: Text ID (for x=1) or text to translate (for x=2)
  • i: Position (for x=1) or dictionary URI (for x=2)
Parameters
$params : array<string|int, mixed>

Route parameters

endRender()

End page rendering with standard LWT footer.

protected endRender() : void

execute()

Execute an INSERT/UPDATE/DELETE query.

protected execute(string $sql) : int
Parameters
$sql : string

SQL query

Tags
psalm-suppress

PossiblyUnusedReturnValue

Return values
int

Number of affected rows

get()

Get a string GET parameter.

protected get(string $key[, string $default = '' ]) : string
Parameters
$key : string

Parameter name

$default : string = ''

Default value if not set

Return values
string

Parameter value or default

getMarkedIds()

Get IDs from marked checkboxes.

protected getMarkedIds(string|array<string|int, mixed> $marked) : array<string|int, int>
Parameters
$marked : string|array<string|int, mixed>

The 'marked' request parameter value

Tags
psalm-return

array

Return values
array<string|int, int>

Array of integer IDs

getValue()

Get a single value from the database.

protected getValue(string $sql) : mixed
Parameters
$sql : string

SQL query (should return single value as 'value')

Return values
mixed

The value or null

hasParam()

Check if a parameter exists in the request.

protected hasParam(string $key) : bool
Parameters
$key : string

Parameter name

Return values
bool

True if the parameter exists

isGet()

Check if the request is a GET request.

protected isGet() : bool
Return values
bool

isPost()

Check if the request is a POST request.

protected isPost() : bool
Return values
bool

json()

Return JSON response.

protected json(mixed $data[, int $status = 200 ]) : JsonResponse

Return this from a controller method; the router will send it.

Parameters
$data : mixed

Data to encode as JSON

$status : int = 200

HTTP status code (default: 200)

Return values
JsonResponse

message()

Display a message (success/error) to the user using Bulma notifications.

protected message(string $message[, bool $autoHide = true ]) : void
Parameters
$message : string

The message to display

$autoHide : bool = true

Whether to auto-hide the message (default: true)

param()

Get a string request parameter (GET, POST, or REQUEST).

protected param(string $key[, string $default = '' ]) : string
Parameters
$key : string

Parameter name

$default : string = ''

Default value if not set

Return values
string

Parameter value or default

paramArray()

Get an array request parameter.

protected paramArray(string $key[, array<string|int, mixed> $default = [] ]) : array<string|int, mixed>
Parameters
$key : string

Parameter name

$default : array<string|int, mixed> = []

Default value if not set

Return values
array<string|int, mixed>

Parameter value or default

paramInt()

Get an integer request parameter.

protected paramInt(string $key[, int|null $default = null ][, int|null $min = null ][, int|null $max = null ]) : int|null
Parameters
$key : string

Parameter name

$default : int|null = null

Default value if not set

$min : int|null = null

Minimum allowed value

$max : int|null = null

Maximum allowed value

Return values
int|null

Parameter value or default

post()

Get a string POST parameter.

protected post(string $key[, string $default = '' ]) : string
Parameters
$key : string

Parameter name

$default : string = ''

Default value if not set

Return values
string

Parameter value or default

processTranslationRequest()

Process a translation request.

protected processTranslationRequest(int $type, int $t, int $i) : void
Parameters
$type : int

Operation type

$t : int

Text ID or text parameter

$i : int

Position or dictionary URI parameter

query()

Execute a database query using the LWT query wrapper.

protected query(string $sql) : mysqli_result|bool
Parameters
$sql : string

SQL query

Return values
mysqli_result|bool

Query result

redirect()

Redirect to another URL.

protected redirect(string $url[, int $statusCode = 302 ]) : RedirectResponse

Return this from a controller method; the router will send it.

Parameters
$url : string

URL to redirect to

$statusCode : int = 302

HTTP status code (default: 302)

Return values
RedirectResponse

render()

Start page rendering with standard LWT header.

protected render(string $title[, bool $showMenu = true ]) : void
Parameters
$title : string

Page title

$showMenu : bool = true

Whether to show navigation menu (default: true)

renderGlosbeScript()

Render the JavaScript for Glosbe translation.

protected renderGlosbeScript(string $from, string $dest, string $phrase) : void
Parameters
$from : string

Source language code

$dest : string

Target language code

$phrase : string

Phrase to translate

renderGoogleTranslation()

Render Google translation results.

protected renderGoogleTranslation(string $text, string $srcLang, string $tgtLang, bool $sentenceMode) : void
Parameters
$text : string

Text to translate

$srcLang : string

Source language code

$tgtLang : string

Target language code

$sentenceMode : bool

Whether in sentence translation mode

renderSentenceTranslation()

Render sentence translation result.

protected renderSentenceTranslation(string $text, string $translation) : void
Parameters
$text : string

Original text

$translation : string

Translated text

renderTermTranslation()

Render term translation results.

protected renderTermTranslation(string $text, array<string|int, string> $translations, string $srcLang, string $tgtLang) : void
Parameters
$text : string

Original text

$translations : array<string|int, string>

Array of translations

$srcLang : string

Source language

$tgtLang : string

Target language

requireInt()

Get a required integer request parameter.

protected requireInt(string $key[, int|null $min = null ][, int|null $max = null ]) : int
Parameters
$key : string

Parameter name

$min : int|null = null

Minimum allowed value

$max : int|null = null

Maximum allowed value

Tags
throws
InvalidArgumentException

If parameter is missing or invalid

Return values
int

Parameter value


        
On this page

Search results