Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
barryvdh committed Apr 2, 2014
0 parents commit 2043429
Show file tree
Hide file tree
Showing 17 changed files with 682 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor
composer.phar
composer.lock
.DS_Store
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (C) 2014 Barry vd. Heuvel

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
26 changes: 26 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "barryvdh/laravel-translation-manager",
"description": "Manage Laravel Translations",
"keywords": ["laravel", "translations", "translator"],
"license": "MIT",
"authors": [
{
"name": "Barry vd. Heuvel",
"email": "barryvdh@gmail.com"
}
],
"require": {
"php": ">=5.3.0",
"illuminate/support": "4.1.*",
"illuminate/translation": "4.1.*"
},
"autoload": {
"classmap": [
"src/migrations"
],
"psr-0": {
"Barryvdh\\TranslationManager\\": "src/"
}
},
"minimum-stability": "stable"
}
84 changes: 84 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
## Laravel Translation Manager

This is a package to manage Laravel translation files.
It does not replace the Translation system, only import/export the php files to a database and make them editable through a webinterface.
The workflow would be:
- Import translations: Read alle translation files and save them in the databse
- Optionally: Listen to missing translation with the custom Translator
- Translate all keys through the webinterface
- Export: Write all translations back to the translation files.

This way, translations can be saved in git history and no overhead is introduced in production.

## Installation

Require this package in your composer.json and run composer update (or run `composer require barryvdh/laravel-translation-master:*` directly):

"barryvdh/laravel-translation-master": "*"

After updating composer, add the ServiceProvider to the providers array in app/config/app.php

'Barryvdh\TranslationManager\ManagerServiceProvider',

You need to run the migrations for this package

$ php artisan migrate --package="barryvdh/laravel-translation-manager"

You have to add the Controller to your routes.php, so you can set your own url/filters.

Route::controller('translations', 'Barryvdh\TranslationManager\Controller');

This example will make the translatio manager availbale at `http://yourdomain.com/translations`

## Usage

### Import command

The import command will search through app/lang and load all strings in the database, so you can easily manage them.

$ php artisan translations:import

### Web interface

When you have imported your translation, you can view them in the webinterface (on the url you defined the with the controller).
You can click on a translation and an edit field will popup. Just click save and it is saved :)
When a translation is not yet created in a different locale, you can also just edit it to create it.

### Export command

The export command will write the contents of the database back to app/lang php files.
This will overwrite existing translations and remove all comments, so make sure to backup your data before using.
Supply the group name to define which groups you want to publish.

$ php artisan translations:export <group>

For example, `php artisan translations:export reminders` when you have 2 locales (en/nl), will write to `app/lang/en/reminders.php` and `app/lang/nl/reminders.php`

### Reset command

The reset command simply clears all translation in the database, so you can start fresh (by a new import). Make sure to export your work if needed before doing this.

$ php artisan translations:reset


### Detect missing translations

To detect missing translations, we can swap the Laravel TranslationServicepProvider with a custom provider.
In your config/app.php, comment out the original TranslationServiceProvider and add the one from this package:

//'Illuminate\Translation\TranslationServiceProvider',
'Barryvdh\TranslationManager\TranslationServiceProvider',

This will extend the Translator and will create a new database entry, whenever a key is not found, so you have to visit the pages that use them.
This way it shows up in the webinterface and can be edited and later exported.
You shouldn't use this in production, just in production to translate your views, then just switch back.

## TODO

This package is still very alpha. Few thinks that are on the todo-list:

* Delete strings via webinterface
* Add locales/groups via webinterface
* Import/export via webinterface
* Improve webinterface (more selection/filtering, behavior of popup after save etc)
* Suggestions are welcome :)
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php namespace Barryvdh\TranslationManager\Console;

use Barryvdh\TranslationManager\Manager;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputArgument;
use Barryvdh\TranslationManager\Models\Translation;
use Illuminate\Filesystem\Filesystem;

class TranslationExportCommand extends Command {

/**
* The console command name.
*
* @var string
*/
protected $name = 'translation:export';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Export translations';

/** @var \Barryvdh\TranslationManager\Manager */
protected $manager;

public function __construct(Manager $manager)
{
$this->manager = $manager;
parent::__construct();
}

/**
* Execute the console command.
*
* @return void
*/
public function fire()
{
$group = $this->argument('group');

$this->manager->exportTranslations($group);

$this->info("Done writing language files for group $group");

}

/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return array(
array('group', InputArgument::REQUIRED, 'The group to export.'),
);
}




}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php namespace Barryvdh\TranslationManager\Console;

use Barryvdh\TranslationManager\Manager;
use Illuminate\Console\Command;
use Barryvdh\TranslationManager\Models\Translation;

class TranslationImportCommand extends Command {

/**
* The console command name.
*
* @var string
*/
protected $name = 'translation:import';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Import translations';

/** @var \Barryvdh\TranslationManager\Manager */
protected $manager;

public function __construct(Manager $manager)
{
$this->manager = $manager;
parent::__construct();
}


/**
* Execute the console command.
*
* @return void
*/
public function fire()
{
$counter = $this->manager->importTranslations();

$this->info('Done importing, processed '.$counter. ' items!');

}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php namespace Barryvdh\TranslationManager\Console;

use Illuminate\Console\Command;
use Barryvdh\TranslationManager\Models\Translation;

class TranslationResetCommand extends Command {

/**
* The console command name.
*
* @var string
*/
protected $name = 'translation:reset';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Reset translations';


/**
* Execute the console command.
*
* @return void
*/
public function fire()
{
Translation::truncate();
$this->info("All translations are deleted");

}


}
55 changes: 55 additions & 0 deletions src/Barryvdh/TranslationManager/Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php namespace Barryvdh\TranslationManager;

use Illuminate\Routing\Controller as BaseController;

use Barryvdh\TranslationManager\Models\Translation;

class Controller extends BaseController
{

public function getIndex($group = null)
{

$locales = $this->loadLocales();
$groups = Translation::groupBy('group')->lists('group', 'group');
$groups = array(''=>'Choose a group') + $groups;

$translations = array();
foreach(Translation::where('group', $group)->get() as $translation){
$translations[$translation->key][$translation->locale] = $translation;
}

return \View::make('laravel-translation-manager::index')
->with('translations', $translations)
->with('locales', $locales)
->with('groups', $groups)
->with('group', $group)
->with('editUrl', \URL::action(get_class($this).'@postEdit', [$group]))
;
}

protected function loadLocales(){
//Set the default locale as the first one.

$locales = array_merge(array(\Config::get('app.locale')), Translation::groupBy('locale')->lists('locale'));
return array_unique($locales);
}

public function postEdit($group){
$name = \Input::get('name');
$value = \Input::get('value');


list($locale, $key) = explode('|', $name, 2);
$translation = Translation::firstOrNew(array(
'locale' => $locale,
'group' => $group,
'key' => $key,
));
$translation->value = (string) $value;
$translation->save();
return array('status' => 'ok');
}


}
Loading

0 comments on commit 2043429

Please sign in to comment.