Skip to content

Commit

Permalink
Change translations to DB translations and add json support (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanmuhittin authored and barryvdh committed Apr 4, 2019
1 parent 45206db commit 9e5548a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"illuminate/support": "5.5.x|5.6.x|5.7.x|5.8.x",
"illuminate/translation": "5.5.x|5.6.x|5.7.x|5.8.x",
"symfony/finder": "~3.0|~4.0",
"tanmuhittin/laravel-google-translate": "^0.4.0"
"tanmuhittin/laravel-google-translate": "^0.11.0"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 3 additions & 3 deletions resources/views/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@
<input type="submit" value="Add keys" class="btn btn-primary">
</div>
</form>
<?php if($group!=='_json'): ?>
<div class="row">
<div class="col-sm-2">
<span class="btn btn-default enable-auto-translate-group">Use Auto Translate</span>
Expand All @@ -215,7 +214,9 @@
<input type="text" name="new-locale" class="form-control" id="new-locale" placeholder="Enter target locale key" />
</div>
<?php if(!config('laravel_google_translate.google_translate_api_key')): ?>
<code>Please enter your Google Translate API key for auto-translating custom translation files</code>
<p>
<code>Translating using stichoza/google-translate-php. If you would like to use Google Translate API enter your Google Translate API key to config file laravel_google_translate</code>
</p>
<?php endif; ?>
<div class="form-group">
<input type="hidden" name="with-translations" value="1">
Expand All @@ -225,7 +226,6 @@
</div>
</div>
</form>
<?php endif; ?>
<hr>
<h4>Total: <?= $numTranslations ?>, changed: <?= $numChanged ?></h4>
<table class="table">
Expand Down
38 changes: 21 additions & 17 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Illuminate\Routing\Controller as BaseController;
use Barryvdh\TranslationManager\Models\Translation;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Artisan;
use Tanmuhittin\LaravelGoogleTranslate\Commands\TranslateFilesCommand;

class Controller extends BaseController
{
Expand Down Expand Up @@ -174,24 +174,28 @@ public function postRemoveLocale(Request $request)
public function postTranslateMissing(Request $request){
$locales = $this->manager->getLocales();
$newLocale = str_replace([], '-', trim($request->input('new-locale')));
if($request->has('with-translations') && $request->has('base-locale') && in_array($request->input('base-locale'),$locales) && $request->has('file')){
$json = false;
if($request->has('with-translations') && $request->has('base-locale') && in_array($request->input('base-locale'),$locales) && $request->has('file') && in_array($newLocale, $locales)){
$base_locale = $request->get('base-locale');
$group = $request->get('file');
if($group === '_json'){
//$json = true;
//$file_name = $newLocale.'_json';
return redirect()->back();
}else{
$file_name = $group.'.php';
$base_strings = Translation::where('group', $group)->where('locale', $base_locale)->get();
foreach ($base_strings as $base_string) {
$base_query = Translation::where('group', $group)->where('locale', $newLocale)->where('key', $base_string->key);
if ($base_query->exists() && $base_query->whereNotNull('value')->exists()) {
// Translation already exists. Skip
continue;
}
$translated_text = TranslateFilesCommand::translate($base_locale, $newLocale, $base_string->value);
request()->replace([
'value' => $translated_text,
'name' => $newLocale . '|' . $base_string->key,
]);
app()->call(
'Barryvdh\TranslationManager\Controller@postEdit',
[
'group' => $group
]
);
}
$this->manager->addLocale($newLocale);
$this->manager->exportTranslations($group, $json);
Artisan::call("translate:files",[
'--baselocale' => $request->input('base-locale'),
'--targetlocales' => $newLocale,
'--targetfiles' => $file_name
]);
$this->manager->importTranslations(false,null,$group);
return redirect()->back();
}
return redirect()->back();
Expand Down

0 comments on commit 9e5548a

Please sign in to comment.