Skip to content

Commit

Permalink
fixes #21 and #22 for L50
Browse files Browse the repository at this point in the history
  • Loading branch information
potsky committed Feb 2, 2016
1 parent 4b56ca7 commit eeaf6ad
Show file tree
Hide file tree
Showing 13 changed files with 490 additions and 32 deletions.
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -273,13 +273,22 @@ Use the [github issue tool](https://github.com/potsky/laravel-localization-helpe

## 5. Upgrade notices

### From `v2.x.1` to `v2.x.2`

- Parameter `obsolete_array_key` has been added in the [configuration file](https://github.com/potsky/laravel-localization-helpers/tree/master/src/config). Add it in your configuration file.

### From `v1.x.x` to `v2.x.x`

- First you need to update your composer file to set the correct version
- Take a look at the [configuration file](https://github.com/potsky/laravel-localization-helpers/tree/master/src/config) in the package to add new parameters you don't have in your current package configuration file.

## 6. Change Log

### v2.x.2

- show obsolete lemma when it is in array ([#21](https://github.com/potsky/laravel-localization-helpers/issues/21))
- fix a bug when using obsolete option ([#22](https://github.com/potsky/laravel-localization-helpers/issues/22))

### v2.x.1

- fix a bug when using backup files and when a dot is in your laravel installation path ([#20](https://github.com/potsky/laravel-localization-helpers/issues/20))
Expand Down
Expand Up @@ -12,20 +12,24 @@ abstract class LocalizationAbstract extends Command implements MessageBagInterfa
const SUCCESS = 0;
const ERROR = 1;

/**
* Init log file for first log
*
* @var boolean
*/
protected static $logInFileFirst = true;
/**
* Config repository.
*
* @var \Illuminate\Config\Repository
*/
protected $configRepository;

/**
* The localization manager
*
* @var Localization
*/
protected $manager;

/**
* Should commands display something
*
Expand Down Expand Up @@ -122,4 +126,34 @@ public function writeError( $s )
parent::error( $s );
}
}

/**
* Log in a file for debug purpose only
*
* @param mixed $txt
* @param string $logFile
*
* @codeCoverageIgnore
*/
protected function logInFile( $txt = '' , $logFile = '/tmp/llh.log' )
{
if ( ! is_string( $txt ) )
{
$txt = print_r( $txt , true );
}

$txt = '==> ' . date( 'Y/m/d H:i:s' ) . ' ==> ' . $txt . "\n";

if ( self::$logInFileFirst === true )
{
file_put_contents( $logFile , $txt );

self::$logInFileFirst = false;
}
else
{
file_put_contents( $logFile , $txt , FILE_APPEND );
}
}

}
129 changes: 99 additions & 30 deletions src/Potsky/LaravelLocalizationHelpers/Command/LocalizationMissing.php
Expand Up @@ -82,6 +82,15 @@ class LocalizationMissing extends LocalizationAbstract
*/
protected $code_style_level = null;

/**
* The obsolete lemma array key in which to store obsolete lemma
*
* @var string
*
* @since 2.x.2
*/
protected $obsolete_array_key = 'LLH:obsolete';

/**
* Create a new command instance.
*
Expand All @@ -99,6 +108,14 @@ public function __construct( Repository $configRepository )
$this->editor = Config::get( Localization::PREFIX_LARAVEL_CONFIG . 'editor_command_line' );
$this->code_style_fixers = Config::get( Localization::PREFIX_LARAVEL_CONFIG . 'code_style.fixers' );
$this->code_style_level = Config::get( Localization::PREFIX_LARAVEL_CONFIG . 'code_style.level' );

// @since 2.x.2
// Users who have not upgraded their configuration file must have a default
// but users may want to set it to null to keep the old buggy behaviour
if ( Config::has( Localization::PREFIX_LARAVEL_CONFIG . 'obsolete_array_key' ) )
{
$this->obsolete_array_key = Config::get( Localization::PREFIX_LARAVEL_CONFIG . 'obsolete_array_key' );
}
}

/**
Expand All @@ -108,9 +125,10 @@ public function __construct( Repository $configRepository )
*/
public function fire()
{
$folders = $this->manager->getPath( $this->folders );
$this->display = ! $this->option( 'silent' );
$extension = $this->option( 'php-file-extension' );
$folders = $this->manager->getPath( $this->folders );
$this->display = ! $this->option( 'silent' );
$extension = $this->option( 'php-file-extension' );
$obsolete_prefix = ( empty( $this->obsolete_array_key ) ) ? '' : $this->obsolete_array_key . '.';

//////////////////////////////////////////////////
// Display where translations are searched in //
Expand Down Expand Up @@ -195,13 +213,15 @@ public function fire()
{
switch ( $e->getCode() )
{
//@codeCoverageIgnoreStart
case Localization::NO_LANG_FOLDER_FOUND_IN_THESE_PATHS:
$this->writeError( "No lang folder found in these paths:" );
foreach ( $e->getParameter() as $path )
{
$this->writeError( "- " . $path );
}
break;
//@codeCoverageIgnoreEnd

case Localization::NO_LANG_FOLDER_FOUND_IN_YOUR_CUSTOM_PATH:
$this->writeError( 'No lang folder found in your custom path: "' . $e->getParameter() . '"' );
Expand Down Expand Up @@ -241,6 +261,7 @@ public function fire()
{
$this->writeLine( '' );
}

$this->writeLine( ' ' . $this->manager->getShortPath( $file_lang_path ) );

if ( ! is_writable( dirname( $file_lang_path ) ) )
Expand Down Expand Up @@ -287,16 +308,37 @@ public function fire()
}

/** @noinspection PhpIncludeInspection */
$a = include( $file_lang_path );
$old_lemmas = ( is_array( $a ) ) ? array_dot( $a ) : array();
$new_lemmas = array_dot( $array );
$final_lemmas = array();
$display_already_comment = false;
$something_to_do = false;
$i = 0;
$obsolete_lemmas = array_diff_key( $old_lemmas , $new_lemmas );
$welcome_lemmas = array_diff_key( $new_lemmas , $old_lemmas );
$already_lemmas = array_intersect_key( $old_lemmas , $new_lemmas );
$a = include( $file_lang_path );
$old_lemmas_with_obsolete = ( is_array( $a ) ) ? array_dot( $a ) : array();
$new_lemmas = array_dot( $array );
$final_lemmas = array();
$display_already_comment = false;
$something_to_do = false;
$i = 0;

// Remove the obsolete prefix key
$old_lemmas = array();
$obsolete_prefix_length = strlen( $obsolete_prefix );
foreach ( $old_lemmas_with_obsolete as $key => $value )
{
if ( starts_with( $key , $obsolete_prefix ) )
{
$key = substr( $key , $obsolete_prefix_length );
if ( ! isset( $old_lemmas[ $key ] ) )
{
$old_lemmas[ $key ] = $value;
}
}
else
{
$old_lemmas[ $key ] = $value;
}
}

$obsolete_lemmas = array_diff_key( $old_lemmas , $new_lemmas );
$welcome_lemmas = array_diff_key( $new_lemmas , $old_lemmas );
$already_lemmas = array_intersect_key( $old_lemmas , $new_lemmas );

ksort( $obsolete_lemmas );
ksort( $welcome_lemmas );
ksort( $already_lemmas );
Expand All @@ -306,12 +348,13 @@ public function fire()
//////////////////////////
if ( count( $welcome_lemmas ) > 0 )
{
$display_already_comment = true;
$something_to_do = true;
$there_are_new = true;
$this->writeInfo( " " . count( $welcome_lemmas ) . " new strings to translate" );
$display_already_comment = true;
$something_to_do = true;
$there_are_new = true;
$final_lemmas[ "POTSKY___NEW___POTSKY" ] = "POTSKY___NEW___POTSKY";

$this->writeInfo( ' ' . ( $c = count( $welcome_lemmas ) ) . ' new string' . Tools::getPlural( $c ) . ' to translate' );

foreach ( $welcome_lemmas as $key => $value )
{
if ( $this->option( 'verbose' ) )
Expand Down Expand Up @@ -348,7 +391,7 @@ public function fire()
{
if ( $this->option( 'verbose' ) )
{
$this->writeLine( " " . count( $already_lemmas ) . " already translated strings" );
$this->writeLine( ' ' . ( $c = count( $already_lemmas ) ) . ' already translated string' . Tools::getPlural( $c ) );
}

$final_lemmas[ "POTSKY___OLD___POTSKY" ] = "POTSKY___OLD___POTSKY";
Expand All @@ -364,38 +407,62 @@ public function fire()
///////////////////////////////
if ( count( $obsolete_lemmas ) > 0 )
{
$protected_already_included = false;

// Remove all dynamic fields
foreach ( $obsolete_lemmas as $key => $value )
{
foreach ( $this->never_obsolete_keys as $remove )
{
if ( ( strpos( $key , '.' . $remove . '.' ) !== false ) || starts_with( $key , $remove . '.' ) )
{
if ( $this->option( 'verbose' ) )
{
$this->writeLine( " <comment>" . $key . "</comment> is protected as a dynamic lemma" );
}

unset( $obsolete_lemmas[ $key ] );

if ( $protected_already_included === false )
{
$final_lemmas[ "POTSKY___PROTECTED___POTSKY" ] = "POTSKY___PROTECTED___POTSKY";
$protected_already_included = true;
}

// Given that this lemma is never obsolete, we need to send it back to the final lemma array
array_set( $final_lemmas , $key , $value );
}
}
}
}


/////////////////////////////////////
// Fill the final lemmas array now //
/////////////////////////////////////
if ( count( $obsolete_lemmas ) > 0 )
{
$display_already_comment = true;
$something_to_do = true;
$this->writeComment( $this->option( 'no-obsolete' )
? " " . count( $obsolete_lemmas ) . " obsolete strings (will be deleted)"
: " " . count( $obsolete_lemmas ) . " obsolete strings (can be deleted manually in the generated file)"
);
$final_lemmas[ "POTSKY___OBSOLETE___POTSKY" ] = "POTSKY___OBSOLETE___POTSKY";

foreach ( $obsolete_lemmas as $key => $value )
if ( $this->option( 'no-obsolete' ) )
{
if ( $this->option( 'verbose' ) )
{
$this->writeLine( " <comment>" . $key . "</comment>" );
}
if ( ! $this->option( 'no-obsolete' ) )
$this->writeComment( " " . ( $c = count( $obsolete_lemmas ) ) . ' obsolete string' . Tools::getPlural( $c ) . ' (will be deleted)' );
}
else
{
$this->writeComment( " " . ( $c = count( $obsolete_lemmas ) ) . ' obsolete string' . Tools::getPlural( $c ) . ' (can be deleted manually in the generated file)' );

$final_lemmas[ "POTSKY___OBSOLETE___POTSKY" ] = "POTSKY___OBSOLETE___POTSKY";

foreach ( $obsolete_lemmas as $key => $value )
{
array_set( $final_lemmas , $key , $value );
if ( $this->option( 'verbose' ) )
{
$this->writeLine( " <comment>" . $key . "</comment>" );
}

array_set( $final_lemmas , $obsolete_prefix . $key , $value );
}
}
}
Expand All @@ -414,11 +481,13 @@ public function fire()
array(
"'POTSKY___NEW___POTSKY' => 'POTSKY___NEW___POTSKY'," ,
"'POTSKY___OLD___POTSKY' => 'POTSKY___OLD___POTSKY'," ,
"'POTSKY___PROTECTED___POTSKY' => 'POTSKY___PROTECTED___POTSKY'," ,
"'POTSKY___OBSOLETE___POTSKY' => 'POTSKY___OBSOLETE___POTSKY'," ,
) ,
array(
'//============================== New strings to translate ==============================//' ,
( $display_already_comment === true ) ? '//==================================== Translations ====================================//' : '' ,
'//============================== Dynamic protected strings =============================//' ,
'//================================== Obsolete strings ==================================//' ,
) ,
$content
Expand Down
Expand Up @@ -158,12 +158,14 @@ public function getLangPath( $lang_folder_path = null )
if ( file_exists( $path ) )
{
return $path;
//@codeCoverageIgnoreStart
}
}

$e = new Exception( '' , self::NO_LANG_FOLDER_FOUND_IN_THESE_PATHS );
$e->setParameter( $paths );
throw $e;
//@codeCoverageIgnoreEnd
}
else
{
Expand Down Expand Up @@ -450,13 +452,15 @@ public function deleteBackupFiles( $lang_folder_path , $days = 0 , $dryRun = fal
{
switch ( $e->getCode() )
{
//@codeCoverageIgnoreStart
case self::NO_LANG_FOLDER_FOUND_IN_THESE_PATHS:
$this->messageBag->writeError( "No lang folder found in these paths:" );
foreach ( $e->getParameter() as $path )
{
$this->messageBag->writeError( "- " . $path );
}
break;
//@codeCoverageIgnoreEnd

case self::NO_LANG_FOLDER_FOUND_IN_YOUR_CUSTOM_PATH:
$this->messageBag->writeError( 'No lang folder found in your custom path: "' . $e->getParameter() . '"' );
Expand Down
25 changes: 25 additions & 0 deletions src/config/config-laravel5.php
Expand Up @@ -118,6 +118,31 @@
) ,


/*
|--------------------------------------------------------------------------
| Obsolete lemma prefix
|--------------------------------------------------------------------------
|
| If you want to keep obsolete lemma in your lang file, they will be stored
| in a sub-array. If not stored in a sub-array, LLH will not be able to
| separate child lemma.
|
| eg : message.section.dog is used
| message.section.cat is not used anymore
| then : message.section.dog will be kept
| LLH:obsolete.message.section.cat will be generated
|
| : is used in the key because : is a special char in laravel lang lemma and
| : are automatically not scanned by LLH.
|
| Do not change this parameter between 2 commands launch because LLH will not
| be able to find obsolete lemma in the second pass and you will need to
| clean up obsolete lemma manually
|
*/
'obsolete_array_key' => 'LLH:obsolete',


/*
|--------------------------------------------------------------------------
| Editor
Expand Down

0 comments on commit eeaf6ad

Please sign in to comment.