Skip to content

Commit

Permalink
fixes #59
Browse files Browse the repository at this point in the history
  • Loading branch information
potsky committed Mar 17, 2017
1 parent ca43c91 commit a0f7991
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 5 deletions.
Expand Up @@ -119,6 +119,13 @@ public function __construct( Repository $configRepository )
$this->code_style_level = config( Localization::PREFIX_LARAVEL_CONFIG . 'code_style.level' );
$this->dot_notation_split_regex = config( Localization::PREFIX_LARAVEL_CONFIG . 'dot_notation_split_regex' );

if ( ! is_string( $this->dot_notation_split_regex ) )
{
// fallback to dot if provided regex is not a string
$this->dot_notation_split_regex = '/\\./';
}


// @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
Expand Down Expand Up @@ -411,7 +418,7 @@ public function fire()
$i = $i + 1;
}

$key_last_token = explode( '.' , $key );
$key_last_token = preg_split( $this->dot_notation_split_regex , $key );

if ( $this->option( 'translation' ) )
{
Expand All @@ -431,7 +438,7 @@ public function fire()
$translation = str_replace( '%LEMMA' , $translation , $this->option( 'new-value' ) );
}

array_set( $final_lemmas , $key , $translation );
Tools::arraySet( $final_lemmas , $key , $translation , $this->dot_notation_split_regex );
}
}

Expand All @@ -449,7 +456,7 @@ public function fire()

foreach ( $already_lemmas as $key => $value )
{
array_set( $final_lemmas , $key , $value );
Tools::arraySet( $final_lemmas , $key , $value , $this->dot_notation_split_regex );
}
}

Expand Down Expand Up @@ -481,7 +488,7 @@ public function fire()
}

// Given that this lemma is never obsolete, we need to send it back to the final lemma array
array_set( $final_lemmas , $key , $value );
Tools::arraySet( $final_lemmas , $key , $value , $this->dot_notation_split_regex );
}
}
}
Expand Down Expand Up @@ -513,7 +520,7 @@ public function fire()
$this->writeLine( " <comment>" . $key . "</comment>" );
}

array_set( $final_lemmas , $obsolete_prefix . $key , $value );
Tools::arraySet( $final_lemmas , $obsolete_prefix . $key , $value , $this->dot_notation_split_regex );
}
}
}
Expand Down
92 changes: 92 additions & 0 deletions tests/fixes/Gh59Tests.php
@@ -0,0 +1,92 @@
<?php

use Potsky\LaravelLocalizationHelpers\Factory\Localization;

class Gh59Tests extends TestCase
{
private static $langFolder;

private static $langFile;

/**
* Setup the test environment.
*
* - Remove all previous lang files before each test
* - Set custom configuration paths
*/
public function setUp()
{
parent::setUp();

self::$langFolder = self::MOCK_DIR_PATH . '/gh59/lang';
self::$langFile = self::$langFolder . '/en/message.php';
}


/**
* https://github.com/potsky/laravel-localization-helpers/issues/59
*/
public function testDefaultDotNotation()
{
@unlink( self::$langFile );

Config::set( Localization::PREFIX_LARAVEL_CONFIG . 'lang_folder_path' , self::$langFolder );
Config::set( Localization::PREFIX_LARAVEL_CONFIG . 'folders' , self::MOCK_DIR_PATH . '/gh59/code' );
Config::set( Localization::PREFIX_LARAVEL_CONFIG . 'dot_notation_split_regex' , null );

/** @noinspection PhpVoidFunctionResultUsedInspection */
Artisan::call( 'localization:missing' , array(
'--no-interaction' => true ,
'--no-backup' => true ,
'--verbose' => true ,
'--no-date' => true ,
'--no-comment' => true ,
) );

$lemmas = require( self::$langFile );

$this->assertArrayHasKey( "hello" , $lemmas );
$this->assertArrayHasKey( "" , $lemmas[ "hello" ][ "" ][ "" ] );

$this->assertArrayHasKey( "Hello" , $lemmas );
$this->assertArrayHasKey( " How are you?" , $lemmas[ "Hello" ] );
$this->assertArrayHasKey( "How are you?" , $lemmas[ "Hello" ] );
}



/**
* https://github.com/potsky/laravel-localization-helpers/issues/59
*/
public function testAwesomeDotNotation()
{
@unlink( self::$langFile );

Config::set( Localization::PREFIX_LARAVEL_CONFIG . 'lang_folder_path' , self::$langFolder );
Config::set( Localization::PREFIX_LARAVEL_CONFIG . 'folders' , self::MOCK_DIR_PATH . '/gh59/code' );
Config::set( Localization::PREFIX_LARAVEL_CONFIG . 'dot_notation_split_regex' , '/\\.(?=[^ .!?])/' );

/** @noinspection PhpVoidFunctionResultUsedInspection */
Artisan::call( 'localization:missing' , array(
'--no-interaction' => true ,
'--no-backup' => true ,
'--verbose' => true ,
'--no-date' => true ,
'--no-comment' => true ,
) );

$lemmas = require( self::$langFile );

$this->assertArrayHasKey( "Hello. How are you?" , $lemmas );
$this->assertInternalType( 'string' , $lemmas[ 'Hello. How are you?' ] );

$this->assertArrayHasKey( "hello..." , $lemmas );
$this->assertInternalType( 'string' , $lemmas[ 'hello...' ] );

$this->assertArrayHasKey( "Hello" , $lemmas );
$this->assertArrayHasKey( "How are you?" , $lemmas[ "Hello" ] );
}



}
5 changes: 5 additions & 0 deletions tests/mock/gh59/code/trans.php
@@ -0,0 +1,5 @@
<?php

$i = Lang::get( 'message.hello...' );
$i = Lang::get( 'message.Hello. How are you?' );
$i = Lang::get( 'message.Hello.How are you?' );
1 change: 1 addition & 0 deletions tests/mock/gh59/lang/en/.gitignore
@@ -0,0 +1 @@
*.php

0 comments on commit a0f7991

Please sign in to comment.