PHP code example of ferdiunal / laravel-translator
1. Go to this page and download the library: Download ferdiunal/laravel-translator library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ferdiunal / laravel-translator example snippets
return [
'deepl' => [
'api_key' => env('DEEPL_API_KEY'),
],
'nlpCloud' => [
'api_key' => env('NLPCLOUD_API_KEY'),
'model' => env('NLPCLOUD_MODEL', 'nllb-200-3-3b'),
'languages' => [
'az' => 'azj_Latn',
'de' => 'deu_Latn',
'en' => 'eng_Latn',
'es' => 'spa_Latn',
'it' => 'ita_Latn',
'pt' => 'por_Latn',
'tr' => 'tur_Latn',
'ru' => 'rus_Cyrl',
],
],
'openai' => [
'api_key' => env('OPENAI_API_KEY'),
'base_url' => env('OPENAI_BASE_URL', 'https://api.openai.com/v1'),
'model' => env('OPENAI_MODEL', 'gpt-4'),
'messages' => [
[
'role' => 'system',
'content' => 'You are an assistant who translates the text from English to Turkish. Just return the translated output.',
],
],
],
];
use Ferdiunal\LaravelTranslator\Facades\Translator;
// Translate a single text
$translatedText = Translator::translate('Hello World', 'tr');
// Translate multiple texts
$translations = Translator::translate(['Hello', 'World'], 'tr');
// Specify source language
$translatedText = Translator::from('en')->translate('Hello World', 'tr');
// Use specific translation service
$translatedText = Translator::using('openai')->translate('Hello World', 'tr');
$translatedText = Translator::using('deepl')->translate('Hello World', 'tr');
$translatedText = Translator::using('nlpcloud')->translate('Hello World', 'tr');
use Ferdiunal\LaravelTranslator\Facades\Translator;
$collection = collect([
'title' => 'Hello World',
'description' => 'This is a description',
]);
// Translate all values in a collection
$translatedCollection = $collection->map(function ($text) {
return Translator::translate($text, 'tr');
});
{{-- Basic translation directive --}}
{{ translate('Hello World', 'tr') }}
{{-- Use specific translation service --}}
{{ translate('Hello World', 'tr', 'openai') }}