Skip to content

Commit

Permalink
PSR2 fixes and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
tanmuhittin committed Mar 7, 2019
1 parent 163fc4e commit b7408fd
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# laravel-google-translate
Translate translation files (under /resources/lang) from specified base locale to all other available languages using google translate api
Translate translation files (under /resources/lang) from specified base locale to all other available languages using google translate api https://cloud.google.com/translate/

## installation
```console
Expand Down Expand Up @@ -28,10 +28,16 @@ php artisan translate:files --baselocale=tr --exclude=auth,passwords --targetloc
https://stackoverflow.com/a/31830614

## suggested packages
This package can be used with https://github.com/andrey-helldar/laravel-lang-publisher where you have additional translation files to be translated to all other languages
This package can be used with https://github.com/andrey-helldar/laravel-lang-publisher.
Example Scenario: <br>
You would like to add another language support where you have other translation files in addition to base translation files of Laravel. So follow the steps below.
* Add base Laravel translation files using https://github.com/andrey-helldar/laravel-lang-publisher
* Translate your custom files using this package

Done <br>

## todo
* Tranlating one by one takes a long time. Use bulk translate
* Translating one by one takes a long time. Use bulk translate

## finally
Thank you for using laravel-google-translate :)
Thank you for using laravel-google-translate :)
3 changes: 1 addition & 2 deletions src/LaravelGoogleTranslateServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Tanmuhittin\LaravelGoogleTranslate;


use Illuminate\Support\ServiceProvider;
use Tanmuhittin\LaravelGoogleTranslate\Commands\TranslateFilesCommand;

Expand Down Expand Up @@ -34,4 +33,4 @@ public function register()
{
//
}
}
}
23 changes: 15 additions & 8 deletions src/commands/TranslateFilesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

class TranslateFilesCommand extends Command
{
public $locales, $base_locale, $excluded_files;
public $locales;
public $base_locale;
public $excluded_files;
/**
* The name and signature of the console command.
*
Expand Down Expand Up @@ -55,8 +57,9 @@ public function handle()
$bar->start();
// loop target locales
foreach ($this->locales as $locale) {
if ($locale == $this->base_locale)
if ($locale == $this->base_locale) {
continue;
}
$this->line($this->base_locale . " -> " . $locale . " translating...");
if (is_dir(resource_path('lang/' . $locale))) {
$this->translate_php_array_files($locale);
Expand Down Expand Up @@ -92,8 +95,9 @@ private function translate($text, $locale)

if (isset($responseDecoded['error'])) {
$this->error("Google Translate API returned error");
if (isset($responseDecoded["error"]["message"]))
if (isset($responseDecoded["error"]["message"])) {
$this->error($responseDecoded["error"]["message"]);
}
exit;
}

Expand All @@ -104,16 +108,18 @@ private function translate($text, $locale)
* @param $locale
* @throws \Exception
*/
private function translate_php_array_files($locale){
private function translate_php_array_files($locale)
{
$files = preg_grep('/^([^.])/', scandir(resource_path('lang/' . $this->base_locale)));
foreach ($files as $file) {
if(file_exists(resource_path('lang/' . $locale . '/' . $file . '.php')) && $this->option('preventoverwrite') ){
if (file_exists(resource_path('lang/' . $locale . '/' . $file . '.php')) && $this->option('preventoverwrite')) {
$this->line('File already exists: lang/' . $locale . '/' . $file . '.php. Skipping (--preventoverwrite)');
return;
}
$file = substr($file, 0, -4);
if (in_array($file, $this->excluded_files))
if (in_array($file, $this->excluded_files)) {
continue;
}
$to_be_translateds = trans($file, [], $this->base_locale);
$new_lang = [];
foreach ($to_be_translateds as $key => $to_be_translated) {
Expand All @@ -135,9 +141,10 @@ private function translate_php_array_files($locale){
* @param $locale
* @throws \Exception
*/
private function translate_json_array_file($locale){
private function translate_json_array_file($locale)
{
if (file_exists(resource_path('lang/' . $locale . '.json'))) {
if(file_exists(resource_path('lang/' . $locale . '.json')) && $this->option('preventoverwrite') ){
if (file_exists(resource_path('lang/' . $locale . '.json')) && $this->option('preventoverwrite')) {
$this->line('File already exists: lang/' . $locale . '.json. Skipping (--preventoverwrite)');
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/laravel_google_translate.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
return [
'google_translate_api_key'=>null,
];
];

0 comments on commit b7408fd

Please sign in to comment.