-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from oss-tools/feature/add-support-for-libre-tr…
…anslate
- Loading branch information
Showing
5 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters