Laravel Package to translate with a translation API
Today I would like to share about a laravel package that I wrote recently. That package is to translate languages easily using a translation API. I had to use a translation api in my project. Then I wanted to write short code for translation feature and reuse it for future. So I created this package locally.
Methods that can be used
After creating Translation Object, you can use the following methods.
$obj = new Translate();
Translation
$translated_text = $obj->translate('Hello World", 'EN', 'JA');
echo $translated_text;
Getting supported languages
$languages = $obj->languages();
foreach($languages as $lang){
echo $lang['language']."-".$lang['name'].'<br>';
}
Getting supported source–languages
$source_languages = $obj->languages('source');
foreach($source_languages as $lang){
echo $lang['language']."-".$lang['name'].'<br>';
}
Getting supported target–languages
$target_languages = $obj->languages('target');
foreach($target_languages as $lang){
echo $lang['language']."-".$lang['name'].'<br>';
}
You can monitor your usage of translation API
$usage= $obj->usage();
echo $usage['character_count'].' characters have been used. Maximum number of characters that can be translated in the current billing period are '.$usage['character_limit'];
You can setup a timeout in requesting api
$obj->setTimeout(10);
Hope you enjoyed that.
By Asahi
waithaw at 2021年10月26日 10:00:00