Skip to content

Commit 9e5548a

Browse files
tanmuhittinbarryvdh
authored andcommitted
Change translations to DB translations and add json support (#314)
1 parent 45206db commit 9e5548a

File tree

3 files changed

+25
-21
lines changed

3 files changed

+25
-21
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"illuminate/support": "5.5.x|5.6.x|5.7.x|5.8.x",
1919
"illuminate/translation": "5.5.x|5.6.x|5.7.x|5.8.x",
2020
"symfony/finder": "~3.0|~4.0",
21-
"tanmuhittin/laravel-google-translate": "^0.4.0"
21+
"tanmuhittin/laravel-google-translate": "^0.11.0"
2222
},
2323
"autoload": {
2424
"psr-4": {

resources/views/index.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@
192192
<input type="submit" value="Add keys" class="btn btn-primary">
193193
</div>
194194
</form>
195-
<?php if($group!=='_json'): ?>
196195
<div class="row">
197196
<div class="col-sm-2">
198197
<span class="btn btn-default enable-auto-translate-group">Use Auto Translate</span>
@@ -215,7 +214,9 @@
215214
<input type="text" name="new-locale" class="form-control" id="new-locale" placeholder="Enter target locale key" />
216215
</div>
217216
<?php if(!config('laravel_google_translate.google_translate_api_key')): ?>
218-
<code>Please enter your Google Translate API key for auto-translating custom translation files</code>
217+
<p>
218+
<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>
219+
</p>
219220
<?php endif; ?>
220221
<div class="form-group">
221222
<input type="hidden" name="with-translations" value="1">
@@ -225,7 +226,6 @@
225226
</div>
226227
</div>
227228
</form>
228-
<?php endif; ?>
229229
<hr>
230230
<h4>Total: <?= $numTranslations ?>, changed: <?= $numChanged ?></h4>
231231
<table class="table">

src/Controller.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use Illuminate\Routing\Controller as BaseController;
55
use Barryvdh\TranslationManager\Models\Translation;
66
use Illuminate\Support\Collection;
7-
use Illuminate\Support\Facades\Artisan;
7+
use Tanmuhittin\LaravelGoogleTranslate\Commands\TranslateFilesCommand;
88

99
class Controller extends BaseController
1010
{
@@ -174,24 +174,28 @@ public function postRemoveLocale(Request $request)
174174
public function postTranslateMissing(Request $request){
175175
$locales = $this->manager->getLocales();
176176
$newLocale = str_replace([], '-', trim($request->input('new-locale')));
177-
if($request->has('with-translations') && $request->has('base-locale') && in_array($request->input('base-locale'),$locales) && $request->has('file')){
178-
$json = false;
177+
if($request->has('with-translations') && $request->has('base-locale') && in_array($request->input('base-locale'),$locales) && $request->has('file') && in_array($newLocale, $locales)){
178+
$base_locale = $request->get('base-locale');
179179
$group = $request->get('file');
180-
if($group === '_json'){
181-
//$json = true;
182-
//$file_name = $newLocale.'_json';
183-
return redirect()->back();
184-
}else{
185-
$file_name = $group.'.php';
180+
$base_strings = Translation::where('group', $group)->where('locale', $base_locale)->get();
181+
foreach ($base_strings as $base_string) {
182+
$base_query = Translation::where('group', $group)->where('locale', $newLocale)->where('key', $base_string->key);
183+
if ($base_query->exists() && $base_query->whereNotNull('value')->exists()) {
184+
// Translation already exists. Skip
185+
continue;
186+
}
187+
$translated_text = TranslateFilesCommand::translate($base_locale, $newLocale, $base_string->value);
188+
request()->replace([
189+
'value' => $translated_text,
190+
'name' => $newLocale . '|' . $base_string->key,
191+
]);
192+
app()->call(
193+
'Barryvdh\TranslationManager\Controller@postEdit',
194+
[
195+
'group' => $group
196+
]
197+
);
186198
}
187-
$this->manager->addLocale($newLocale);
188-
$this->manager->exportTranslations($group, $json);
189-
Artisan::call("translate:files",[
190-
'--baselocale' => $request->input('base-locale'),
191-
'--targetlocales' => $newLocale,
192-
'--targetfiles' => $file_name
193-
]);
194-
$this->manager->importTranslations(false,null,$group);
195199
return redirect()->back();
196200
}
197201
return redirect()->back();

0 commit comments

Comments
 (0)