Skip to content

Commit

Permalink
Merge pull request #2 from oss-tools/feature/add-support-for-libre-tr…
Browse files Browse the repository at this point in the history
…anslate
  • Loading branch information
blessingdube committed Dec 28, 2021
2 parents 6c183ba + 85b656d commit 4f941bb
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .php-cs-fixer.cache
@@ -1 +1 @@
{"php":"8.1.0","version":"3.4.0","indent":" ","lineEnding":"\n","rules":{"blank_line_after_namespace":true,"braces":true,"class_definition":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline","keep_multiple_spaces_after_comma":true},"no_break_comment":true,"no_closing_tag":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":{"elements":["method","property"]},"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"ordered_imports":{"sort_algorithm":"alpha"},"no_unused_imports":true,"not_operator_with_successor_space":true,"trailing_comma_in_multiline":true,"phpdoc_scalar":true,"unary_operator_spaces":true,"binary_operator_spaces":true,"blank_line_before_statement":{"statements":["break","continue","declare","return","throw","try"]},"phpdoc_single_line_var_spacing":true,"phpdoc_var_without_name":true,"class_attributes_separation":{"elements":{"method":"one"}},"single_trait_insert_per_statement":true},"hashes":{"src\/AutoTranslate.php":4154997387,"src\/AutoTranslateServiceProvider.php":2632110928,"src\/Translators\/TranslatorInterface.php":1760706323,"src\/Translators\/SimpleGoogleTranslator.php":3165034847,"src\/Translators\/DeeplTranslator.php":805475982,"src\/Translators\/GoogleCloudTranslator.php":261199226,"src\/Exceptions\/LanguageCodeNotExist.php":2915396789,"src\/AutoTranslateFacade.php":3069628600,"src\/Commands\/AllCommand.php":1668401347,"src\/Commands\/MissingCommand.php":779960117,"tests\/TestCase.php":2035294003,"tests\/AutoTranslateTest.php":3614791415}}
{"php":"8.1.0","version":"3.4.0","indent":" ","lineEnding":"\n","rules":{"blank_line_after_namespace":true,"braces":true,"class_definition":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline","keep_multiple_spaces_after_comma":true},"no_break_comment":true,"no_closing_tag":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":{"elements":["method","property"]},"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"ordered_imports":{"sort_algorithm":"alpha"},"no_unused_imports":true,"not_operator_with_successor_space":true,"trailing_comma_in_multiline":true,"phpdoc_scalar":true,"unary_operator_spaces":true,"binary_operator_spaces":true,"blank_line_before_statement":{"statements":["break","continue","declare","return","throw","try"]},"phpdoc_single_line_var_spacing":true,"phpdoc_var_without_name":true,"class_attributes_separation":{"elements":{"method":"one"}},"single_trait_insert_per_statement":true},"hashes":{"src\/AutoTranslate.php":4154997387,"src\/AutoTranslateServiceProvider.php":2632110928,"src\/Translators\/TranslatorInterface.php":1760706323,"src\/Translators\/SimpleGoogleTranslator.php":3165034847,"src\/Translators\/DeeplTranslator.php":805475982,"src\/Translators\/GoogleCloudTranslator.php":261199226,"src\/Exceptions\/LanguageCodeNotExist.php":2915396789,"src\/AutoTranslateFacade.php":3069628600,"src\/Commands\/AllCommand.php":1668401347,"src\/Commands\/MissingCommand.php":1730225701,"tests\/TestCase.php":2035294003,"tests\/AutoTranslateTest.php":453318271,"src\/Translators\/LibreTranslateTranslator.php":1471250717}}
6 changes: 6 additions & 0 deletions config/config.php
Expand Up @@ -41,4 +41,10 @@
// Your DeepL API Key. See https://www.deepl.com/pro.html#developer
'api_key' => '',
],

'libretranslate' => [
'host' => 'https://translate.argosopentech.com',
'api_key' => null,
'default_source' => \OSSTools\LibreTranslate\Translation\LanguageCodes::ENGLISH,
],
];
4 changes: 4 additions & 0 deletions src/Commands/MissingCommand.php
Expand Up @@ -54,6 +54,10 @@ public function handle()
$missing = $this->autoTranslator->getMissingTranslations($targetLanguage);
$missingCount += $missing->count();
$strLen += $missing->map(function ($value) {
if (! $value) {
return 0;
}

return strlen($value);
})->sum();
$this->line('Found '.$missing->count().' missing keys in '.$targetLanguage);
Expand Down
48 changes: 48 additions & 0 deletions src/Translators/LibreTranslateTranslator.php
@@ -0,0 +1,48 @@
<?php

namespace OSSTools\AutoTranslate\Translators;

use Exception;
use OSSTools\AutoTranslate\Exceptions\LanguageCodeNotExist;
use OSSTools\LibreTranslate\Client;

class LibreTranslateTranslator implements TranslatorInterface
{
protected $translator;
protected $source;
protected $target;

public function __construct()
{
$this->translator = new Client(config('auto-translate.libretranslate.host'), config('auto-translate.libretranslate.api_key'), config('auto-translate.libretranslate.default_source'));
}

public function setSource(string $source): self
{
$this->source = strtolower($source);

$this->translator = new Client(config('auto-translate.libretranslate.host'), config('auto-translate.libretranslate.api_key'), $this->source);

return $this;
}

public function setTarget(string $target): self
{
$this->target = strtolower($target);

return $this;
}

public function translate(string $string) : string
{
try {
return $this->translator->translate($string, $this->target, $this->source)->first()->getText() ?? '';
} catch (Exception $th) {
if ($th->getMessage() === 'The supplied target is invalid') {
throw LanguageCodeNotExist::throw($this->source, $this->target);
}

throw $th;
}
}
}
14 changes: 14 additions & 0 deletions tests/AutoTranslateTest.php
Expand Up @@ -5,7 +5,9 @@
use Illuminate\Support\Arr;
use Mockery;
use OSSTools\AutoTranslate\AutoTranslateFacade;
use OSSTools\AutoTranslate\Translators\LibreTranslateTranslator;
use OSSTools\AutoTranslate\Translators\TranslatorInterface;
use OSSTools\LibreTranslate\Translation\LanguageCodes;

class AutoTranslateTest extends TestCase
{
Expand Down Expand Up @@ -171,4 +173,16 @@ public function test_translate_with_no_content()
],
]);
}

public function test_translate_with_libretranslate()
{
$translatedText = (new LibreTranslateTranslator())
->setSource(LanguageCodes::ENGLISH)
->setTarget(LanguageCodes::SPANISH)
->translate('This is a test');

$expectedResult = 'Esta es una prueba';

$this->assertSame($translatedText, $expectedResult);
}
}

0 comments on commit 4f941bb

Please sign in to comment.