Skip to content

Commit

Permalink
Support sub-directories (barryvdh#99)
Browse files Browse the repository at this point in the history
* Fix getView support sub-directory

* Fix method import can get file in sub-directory

* Fix other method use request group

Fix bug when create / edit / and delete key.

* Update View

* Fix method import get All Files

* Update separate for sub-directory
  • Loading branch information
ALTELMA authored and barryvdh committed Apr 8, 2016
1 parent 3a376a1 commit 980a410
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 23 deletions.
11 changes: 7 additions & 4 deletions resources/views/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Translation Manager</title>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<link href="//cdnjs.cloudflare.com/ajax/libs/x-editable/1.5.0/bootstrap3-editable/css/bootstrap-editable.css" rel="stylesheet"/>
<script src="//cdnjs.cloudflare.com/ajax/libs/x-editable/1.5.0/bootstrap3-editable/js/bootstrap-editable.min.js"></script>
<script>//https://github.com/rails/jquery-ujs/blob/master/src/rails.js
Expand Down Expand Up @@ -63,7 +63,7 @@
$('div.success-import strong.counter').text(data.counter);
$('div.success-import').slideDown();
});

$('.form-find').on('ajax:success', function (e, data) {
$('div.success-find strong.counter').text(data.counter);
$('div.success-find').slideDown();
Expand Down Expand Up @@ -106,13 +106,15 @@
</form>
<form class="form-inline form-find" method="POST" action="<?= action('\Barryvdh\TranslationManager\Controller@postFind') ?>" data-remote="true" role="form" data-confirm="Are you sure you want to scan you app folder? All found translation keys will be added to the database.">
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
<p></p>
<button type="submit" class="btn btn-info" data-disable-with="Searching.." >Find translations in files</button>
</form>
<?php endif; ?>
<?php if(isset($group)) : ?>
<form class="form-inline form-publish" method="POST" action="<?= action('\Barryvdh\TranslationManager\Controller@postPublish', $group) ?>" data-remote="true" role="form" data-confirm="Are you sure you want to publish the translations group '<?= $group ?>? This will overwrite existing language files.">
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
<button type="submit" class="btn btn-info" data-disable-with="Publishing.." >Publish translations</button>
<a href="<?= action('\Barryvdh\TranslationManager\Controller@getIndex') ?>" class="btn btn-default">Back</a>
</form>
<?php endif; ?>
</p>
Expand All @@ -130,9 +132,10 @@
<form action="<?= action('\Barryvdh\TranslationManager\Controller@postAdd', array($group)) ?>" method="POST" role="form">
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
<textarea class="form-control" rows="3" name="keys" placeholder="Add 1 key per line, without the group prefix"></textarea>
<p></p>
<input type="submit" value="Add keys" class="btn btn-primary">
</form>

<hr>
<h4>Total: <?= $numTranslations ?>, changed: <?= $numChanged ?></h4>
<table class="table">
<thead>
Expand Down
28 changes: 20 additions & 8 deletions src/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ public function getIndex($group = null)
->with('deleteEnabled', $this->manager->getConfig('delete_enabled'));
}

public function getView($group)
public function getView($group, $sub_group = null)
{
if ($sub_group) {
return $this->getIndex($group.'/'.$sub_group);
}

return $this->getIndex($group);
}

Expand All @@ -66,10 +70,14 @@ protected function loadLocales()
return array_unique($locales);
}

public function postAdd(Request $request, $group)
public function postAdd(Request $request, $group, $sub_group = null)
{
$keys = explode("\n", $request->get('keys'));

if ($sub_group) {
$group = $group . "/" . $sub_group;
}

foreach($keys as $key){
$key = trim($key);
if($group && $key){
Expand All @@ -79,7 +87,7 @@ public function postAdd(Request $request, $group)
return redirect()->back();
}

public function postEdit(Request $request, $group)
public function postEdit(Request $request, $group, $sub_group = null)
{
if(!in_array($group, $this->manager->getConfig('exclude_groups'))) {
$name = $request->get('name');
Expand All @@ -88,7 +96,7 @@ public function postEdit(Request $request, $group)
list($locale, $key) = explode('|', $name, 2);
$translation = Translation::firstOrNew([
'locale' => $locale,
'group' => $group,
'group' => $sub_group ? $group . "/" . $sub_group: $group,
'key' => $key,
]);
$translation->value = (string) $value ?: null;
Expand All @@ -98,7 +106,7 @@ public function postEdit(Request $request, $group)
}
}

public function postDelete($group, $key)
public function postDelete($group, $sub_group = null, $key)
{
if(!in_array($group, $this->manager->getConfig('exclude_groups')) && $this->manager->getConfig('delete_enabled')) {
Translation::where('group', $group)->where('key', $key)->delete();
Expand All @@ -113,17 +121,21 @@ public function postImport(Request $request)

return ['status' => 'ok', 'counter' => $counter];
}

public function postFind()
{
$numFound = $this->manager->findTranslations();

return ['status' => 'ok', 'counter' => (int) $numFound];
}

public function postPublish($group)
public function postPublish($group, $sub_group = null)
{
$this->manager->exportTranslations($group);
if ($sub_group) {
$this->manager->exportTranslations($group.'/'.$sub_group);
} else {
$this->manager->exportTranslations($group);
}

return ['status' => 'ok'];
}
Expand Down
25 changes: 14 additions & 11 deletions src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function importTranslations($replace = false)
foreach($this->files->directories($this->app->langPath()) as $langPath){
$locale = basename($langPath);

foreach($this->files->files($langPath) as $file){
foreach($this->files->allfiles($langPath) as $file) {

$info = pathinfo($file);
$group = $info['filename'];
Expand All @@ -52,41 +52,44 @@ public function importTranslations($replace = false)
continue;
}

$subLangPath = str_replace($langPath . "\\", "", $info['dirname']);
if ($subLangPath != $langPath) {
$group = $subLangPath . "/" . $group;
}

$translations = \Lang::getLoader()->load($locale, $group);
if ($translations && is_array($translations)) {
foreach(array_dot($translations) as $key => $value){
$value = (string) $value;
$translation = Translation::firstOrNew(array(
$translation = Translation::firstOrNew(array(
'locale' => $locale,
'group' => $group,
'key' => $key,
));

// Check if the database is different then the files
$newStatus = $translation->value === $value ? Translation::STATUS_SAVED : Translation::STATUS_CHANGED;
if($newStatus !== (int) $translation->status){
$translation->status = $newStatus;
}

// Only replace when empty, or explicitly told so
if($replace || !$translation->value){
$translation->value = $value;
}

$translation->save();

$counter++;
}
}
}
}
return $counter;
}

public function findTranslations($path = null)
{


$path = $path ?: base_path();
$keys = array();
$functions = array('trans', 'trans_choice', 'Lang::get', 'Lang::choice', 'Lang::trans', 'Lang::transChoice', '@lang', '@choice');
Expand Down Expand Up @@ -128,7 +131,7 @@ public function findTranslations($path = null)
// Return the number of found translations
return count($keys);
}

public function exportTranslations($group)
{
if(!in_array($group, $this->config['exclude_groups'])) {
Expand All @@ -148,7 +151,7 @@ public function exportTranslations($group)
Translation::where('group', $group)->whereNotNull('value')->update(array('status' => Translation::STATUS_SAVED));
}
}

public function exportAllTranslations()
{
$groups = Translation::whereNotNull('value')->select(DB::raw('DISTINCT `group`'))->get('group');
Expand Down

0 comments on commit 980a410

Please sign in to comment.