-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
need to dev a php sdk for bing translator...
- Loading branch information
Showing
11 changed files
with
369 additions
and
64 deletions.
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
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
42 changes: 42 additions & 0 deletions
42
src/Potsky/LaravelLocalizationHelpers/Factory/Translator.php
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,42 @@ | ||
<?php namespace Potsky\LaravelLocalizationHelpers\Factory; | ||
|
||
class Translator implements TranslatorInterface | ||
{ | ||
/** @var TranslatorInterface */ | ||
protected $translator; | ||
|
||
/** | ||
* @param string $translator The translation service name | ||
* @param array $config The configuration array for the translation service | ||
*/ | ||
public function __construct( $translator , $config ) | ||
{ | ||
$class = 'Potsky\LaravelLocalizationHelpers\Factory\Translator' . $translator; | ||
$translator = new $class( $config ); | ||
|
||
if ( ! $translator instanceof TranslatorInterface ) | ||
{ | ||
throw new Exception( 'Provided translator does not implement TranslatorInterface' ); | ||
} | ||
|
||
$this->translator = $translator; | ||
$this->config = $config; | ||
} | ||
|
||
public function translate( $word , $toLang , $fromLang = null ) | ||
{ | ||
return $this->translator->translate( $word , $toLang , $fromLang ); | ||
} | ||
|
||
/** | ||
* Return the used translator | ||
* | ||
* @return \Potsky\LaravelLocalizationHelpers\Factory\TranslatorInterface | ||
*/ | ||
public function getTranslator() | ||
{ | ||
return $this->translator; | ||
} | ||
} | ||
|
||
|
6 changes: 6 additions & 0 deletions
6
src/Potsky/LaravelLocalizationHelpers/Factory/TranslatorInterface.php
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,6 @@ | ||
<?php namespace Potsky\LaravelLocalizationHelpers\Factory; | ||
|
||
interface TranslatorInterface | ||
{ | ||
public function translate( $word , $toLang , $fromLang = null ); | ||
} |
49 changes: 49 additions & 0 deletions
49
src/Potsky/LaravelLocalizationHelpers/Factory/TranslatorMicrosoft.php
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,49 @@ | ||
<?php namespace Potsky\LaravelLocalizationHelpers\Factory; | ||
|
||
class TranslatorMicrosoft implements TranslatorInterface | ||
{ | ||
protected $bingTranslator; | ||
|
||
/** | ||
* @param array $config | ||
*/ | ||
public function __construct( $config ) | ||
{ | ||
if ( isset( $config[ 'api_key' ] ) ) | ||
{ | ||
$apiKey = $config[ 'api_key' ]; | ||
} | ||
else if ( ( $apiKey = getenv( 'LLH_MICROSOFT_TRANSLATOR_API_KEY' ) ) === false ) | ||
{ | ||
throw new Exception( 'Please provide an API key for Microsoft Bing Translator service' ); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $word Sentence or word to translate | ||
* @param string $toLang Target language | ||
* @param null $fromLang Source language (if set to null, translator will try to guess) | ||
* | ||
* @return string|null The translated sentence or null if an error occurs | ||
*/ | ||
public function translate( $word , $toLang , $fromLang = null ) | ||
{ | ||
try | ||
{ | ||
$translation = $this->bingTranslator->translate( $word , $fromLang , $toLang ); | ||
|
||
if ( is_string( $translation ) ) | ||
{ | ||
return $translation; | ||
} | ||
|
||
return null; | ||
} | ||
catch ( Exception $e ) | ||
{ | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
|
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
Oops, something went wrong.