Skip to content

Commit

Permalink
add in place edit of translations in the whole site
Browse files Browse the repository at this point in the history
  • Loading branch information
vsch committed Mar 22, 2015
1 parent 55d9845 commit 2d76c9a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 24 deletions.
25 changes: 18 additions & 7 deletions src/Barryvdh/TranslationManager/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,31 @@ function __construct(Application $app, Filesystem $files, Dispatcher $events)
$this->app = $app;
$this->files = $files;
$this->events = $events;
$this->config = $app['config']['laravel-translation-manager::config'];

// when instantiated from the service provider, config info is not yet loaded, trying to get it here
// causes a problem since none of the keys are defined.
$this->config = null;
}

protected
function config()
{
return $this->config ?: $this->config = $this->app['config']['laravel-translation-manager::config'];
}

public
function missingKey($namespace, $group, $key)
{
if (!in_array($group, $this->config['exclude_groups']))
if (!in_array($group, $this->config()['exclude_groups']))
{
Translation::firstOrCreate(array(
$translation = Translation::firstOrCreate(array(
'locale' => $this->app['config']['app.locale'],
'group' => $group,
'key' => $key,
));
return $translation;
}
return null;
}

public
Expand All @@ -56,7 +67,7 @@ function importTranslations($replace = false)
$info = pathinfo($file);
$group = $info['filename'];

if (in_array($group, $this->config['exclude_groups']))
if (in_array($group, $this->config()['exclude_groups']))
{
continue;
}
Expand Down Expand Up @@ -204,7 +215,7 @@ function formatForExport($trans, $indent = 0)
public
function exportTranslations($group)
{
if (!in_array($group, $this->config['exclude_groups']))
if (!in_array($group, $this->config()['exclude_groups']))
{
if ($group == '*')
$this->exportAllTranslations();
Expand Down Expand Up @@ -264,11 +275,11 @@ function getConfig($key = null)
{
if ($key == null)
{
return $this->config;
return $this->config();
}
else
{
return $this->config[$key];
return $this->config()[$key];
}
}
}
8 changes: 4 additions & 4 deletions src/Barryvdh/TranslationManager/ManagerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ManagerServiceProvider extends ServiceProvider {
*
* @var bool
*/
protected $defer = false;
protected $defer = true;

/**
* Bootstrap the application events.
Expand All @@ -19,9 +19,9 @@ class ManagerServiceProvider extends ServiceProvider {
public function boot()
{
$this->package('barryvdh/laravel-translation-manager');
}
}

/**
/**
* Register the service provider.
*
* @return void
Expand Down Expand Up @@ -71,7 +71,7 @@ public function register()
*/
public function provides()
{
return array('translation-manager',
return array('translation-manager', 'translator', 'translation.loader',
'command.translation-manager.reset',
'command.translation-manager.import',
'command.translation-manager.find',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class TranslationServiceProvider extends BaseTranslationServiceProvider {
*/
public function register()
{

$this->registerLoader();

$this->app->bindShared('translator', function($app)
Expand All @@ -24,7 +23,7 @@ public function register()
// configuration so we can easily get both of these values from there.
$locale = $app['config']['app.locale'];

$trans = new Translator($loader, $locale);
$trans = new \Barryvdh\TranslationManager\Translator($loader, $locale);

$trans->setFallback($app['config']['app.fallback_locale']);

Expand Down
64 changes: 53 additions & 11 deletions src/Barryvdh/TranslationManager/Translator.php
Original file line number Diff line number Diff line change
@@ -1,42 +1,84 @@
<?php namespace Barryvdh\TranslationManager;

use Illuminate\Support\Facades\URL;
use Illuminate\Translation\LoaderInterface;
use Illuminate\Translation\Translator as LaravelTranslator;
use Illuminate\Events\Dispatcher;

class Translator extends LaravelTranslator {
class Translator extends LaravelTranslator
{

/** @var Dispatcher */
protected $events;

/**
* Translator constructor.
*/
public
function __construct(LoaderInterface $loader, $locale)
{
parent::__construct($loader, $locale);
}

/**
* Get the translation for the given key.
*
* @param string $key
* @param array $replace
* @param string $locale
* @param string $key
* @param array $replace
* @param string $locale
*
* @return string
*/
public function get($key, array $replace = array(), $locale = null)
public
function get($key, array $replace = array(), $locale = null)
{
$thisLocale = $this->parseLocale($locale);

if ($thisLocale[0] !== 'dbg' && $thisLocale[1] === 'dbg')
{
list($namespace, $group, $item) = $this->parseKey($key);

// TODO: add config to translation manager to define exclude groups for in page edit
if ($this->manager && $namespace === '*' && $group && $group !== 'page-titles' && $item)
{
$t = $this->manager->missingKey($namespace, $group, $item);
if ($t)
{
if (is_null($t->value)) $t->value = parent::get($key, $replace, $locale);

$result = '<a href="#edit" class="editable status-' . ($t ? $t->status : 0) . ' locale-' . $t->locale . '" data-locale="' . $t->locale . '"
data-name="' . $t->locale . '"|" . ' . $t->key . '" id="username" data-type="textarea" data-pk="' . ($t ? $t->id : 0) . '"
data-url="' . URL::action('Barryvdh\TranslationManager\Controller@postEdit', array($t->group)) . '"
data-inputclass="editable-input"
data-title="' . parent::trans('laravel-translation-manager::translations.enter-translation') . ': [' . $t->locale . '] ' . $key . '">'
. ($t ? htmlentities($t->value, ENT_QUOTES, 'UTF-8', false) : '') . '</a> '//. (!$t ? '' : ($t->saved_value === $t->value ? '' : ' [' . \Barryvdh\TranslationManager\Controller::mb_renderDiffHtml($t->saved_value, $t->value) . ']'));
;
return $result;
}
}
}

$result = parent::get($key, $replace, $locale);
if($result === $key){
if ($result === $key)
{
$this->notifyMissingKey($key);
}

return $result;
}

public function setTranslationManager(Manager $manager)
public
function setTranslationManager(Manager $manager)
{
$this->manager = $manager;
}

protected function notifyMissingKey($key)
protected
function notifyMissingKey($key)
{
list($namespace, $group, $item) = $this->parseKey($key);
if($this->manager && $namespace === '*' && $group && $item ){
if ($this->manager && $namespace === '*' && $group && $item)
{
$this->manager->missingKey($namespace, $group, $item);
}
}

}

0 comments on commit 2d76c9a

Please sign in to comment.