Skip to content

Commit d42e312

Browse files
committed
starts potsky#56
Next things to do : - Il faut stocker final_lemma dans chaque fichier de langue - Il faut stocker les 4 types de lemma dans 4 variables différentes et enlever le final_lemma avec la finte des commentaires - Dans le cas de JSON, il faudra certainement stocker les obsolètes dans un autre fichier et mettre les news en premier et les autres ensuite - Il ne faut plus stocker 564-600 dans le code principal mais dans chaque fichier de langue et son fait quelque chose de différent - La dernière partie backup, création de fichier et code style est aussi à déplacer dans les fichiers de langue
1 parent 9ccdb4c commit d42e312

File tree

18 files changed

+961
-246
lines changed

18 files changed

+961
-246
lines changed

src/Potsky/LaravelLocalizationHelpers/Command/LocalizationMissing.php

Lines changed: 267 additions & 241 deletions
Large diffs are not rendered by default.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Potsky\LaravelLocalizationHelpers\Factory;
4+
5+
6+
use Potsky\LaravelLocalizationHelpers\Object\LangFileTypeGenuine;
7+
use Potsky\LaravelLocalizationHelpers\Object\LangFileTypeGenuineVendor;
8+
use Potsky\LaravelLocalizationHelpers\Object\LangFileTypeJson;
9+
10+
class LangFile
11+
{
12+
13+
/**
14+
* These subfolders are reserved for vendors
15+
*
16+
* https://laravel.com/docs/4.2/localization
17+
* https://laravel.com/docs/5.1/localization
18+
*
19+
* @var array
20+
*/
21+
protected static $vendorsFolders = array( 'vendor' , 'packages' );
22+
23+
24+
/**
25+
* @param string $dir_lang
26+
* @param array $json_langs
27+
*
28+
* @return array
29+
*/
30+
public static function getLangFiles( $dir_lang , $json_langs = null )
31+
{
32+
$langs = array();
33+
34+
// scan genuine folders first
35+
foreach ( scandir( $dir_lang ) as $lang )
36+
{
37+
if ( ! Tools::isValidDirectory( $dir_lang , $lang ) )
38+
{
39+
continue;
40+
}
41+
42+
if ( in_array( $lang , self::$vendorsFolders ) )
43+
{
44+
$langs[] = new LangFileTypeGenuineVendor( $lang );
45+
}
46+
else
47+
{
48+
$langs[] = new LangFileTypeGenuine( $lang );
49+
}
50+
51+
}
52+
53+
// generate json langs
54+
if ( is_array( $json_langs ) )
55+
{
56+
foreach ( $json_langs as $lang )
57+
{
58+
$langs[] = new LangFileTypeJson( $lang );
59+
}
60+
}
61+
62+
return $langs;
63+
}
64+
}

src/Potsky/LaravelLocalizationHelpers/Factory/Localization.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
use Config;
44
use Symfony\Component\Console\Input\ArrayInput;
55
use Symfony\Component\Console\Output\BufferedOutput;
6+
use Symfony\CS\Console\Application;
67

78
class Localization
89
{
910
const NO_LANG_FOLDER_FOUND_IN_THESE_PATHS = 2;
1011
const NO_LANG_FOLDER_FOUND_IN_YOUR_CUSTOM_PATH = 3;
1112
const BACKUP_DATE_FORMAT = "Ymd_His";
1213
const PREFIX_LARAVEL_CONFIG = 'laravel-localization-helpers.';
14+
const JSON_HEADER = '//JSON//';
1315

1416
private static $PHP_CS_FIXER_LEVELS = array( 'psr0' , 'psr1' , 'psr2' , 'symfony' );
1517
private static $PHP_CS_FIXER_FIXERS = array(
@@ -382,9 +384,11 @@ public function convertLemmaToStructuredArray( $lemmas , $dot_notation_split_reg
382384

383385
foreach ( $lemmas as $key => $value )
384386
{
385-
if ( strpos( $key , '.' ) === false )
387+
$keys = preg_split( $dot_notation_split_regex , $key , $level );
388+
389+
if ( count( $keys ) <= 1 )
386390
{
387-
$this->messageBag->writeLine( ' <error>' . $key . '</error> in file <comment>' . $this->getShortPath( $value ) . '</comment> <error>will not be included because it has no family</error>' );
391+
Tools::arraySet( $lemmas_structured , self::JSON_HEADER . '.' . $key , $value , $dot_notation_split_regex , $level );
388392
}
389393
else
390394
{
@@ -410,7 +414,7 @@ public function convertLemmaToStructuredArray( $lemmas , $dot_notation_split_reg
410414
*/
411415
public function convertLemmaToFlatArray( $lemmas )
412416
{
413-
return $this->convertLemmaToStructuredArray( $lemmas , null , null , 2 );
417+
return $this->convertLemmaToStructuredArray( $lemmas , null , 2 );
414418
}
415419

416420
/**
@@ -675,6 +679,7 @@ public function translate( $word , $to , $from = null )
675679
{
676680
if ( is_null( $this->translator ) )
677681
{
682+
/** @var string $translator */
678683
$translator = config( self::PREFIX_LARAVEL_CONFIG . 'translator' );
679684
$this->translator = new Translator( 'Microsoft' , array(
680685
'client_id' => config( self::PREFIX_LARAVEL_CONFIG . 'translators.' . $translator . '.client_id' ) ,
@@ -696,6 +701,11 @@ public function translate( $word , $to , $from = null )
696701
/**
697702
* Fix Code Style for a file or a directory
698703
*
704+
* @param $filePath
705+
* @param array $fixers
706+
* @param null $level
707+
*
708+
* @return string
699709
* @throws \Exception
700710
* @throws \Potsky\LaravelLocalizationHelpers\Factory\Exception
701711
*/
@@ -743,7 +753,7 @@ public function fixCodeStyle( $filePath , array $fixers , $level = null )
743753

744754
$input = new ArrayInput( $options );
745755
$output = new BufferedOutput();
746-
$application = new \Symfony\CS\Console\Application();
756+
$application = new Application();
747757
$application->setAutoExit( false );
748758
$application->run( $input , $output );
749759

Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
<?php
2+
3+
namespace Potsky\LaravelLocalizationHelpers\Object;
4+
5+
abstract class LangFileAbstract
6+
{
7+
protected $typeVendor;
8+
9+
protected $typeJson;
10+
11+
protected $filePath;
12+
13+
protected $shortFilePath;
14+
15+
protected $lang;
16+
17+
protected $dir;
18+
19+
protected $family;
20+
21+
protected $package;
22+
23+
/**
24+
* LangFileAbstract constructor.
25+
*
26+
* @param string $dir
27+
* @param string $lang
28+
*/
29+
public function __construct( $dir , $lang )
30+
{
31+
$this->setDir( $dir )->setLang( $lang );
32+
}
33+
34+
/**
35+
* @param boolean $typeVendor
36+
*
37+
* @return LangFileAbstract
38+
*/
39+
public function setTypeVendor( $typeVendor )
40+
{
41+
$this->typeVendor = $typeVendor;
42+
43+
return $this;
44+
}
45+
46+
/**
47+
* @return boolean
48+
*/
49+
public function getTypeVendor()
50+
{
51+
return $this->typeVendor;
52+
}
53+
54+
/**
55+
* @param boolean $typeJson
56+
*
57+
* @return LangFileAbstract
58+
*/
59+
public function setTypeJson( $typeJson )
60+
{
61+
$this->typeJson = $typeJson;
62+
63+
return $this;
64+
}
65+
66+
/**
67+
* @return boolean
68+
*/
69+
public function getTypeJson()
70+
{
71+
return $this->typeJson;
72+
}
73+
74+
/**
75+
* @param string $filePath
76+
*
77+
* @return LangFileAbstract
78+
*/
79+
public function setFilePath( $filePath )
80+
{
81+
$this->filePath = $filePath;
82+
$this->shortFilePath = str_replace( base_path() , '' , $filePath );
83+
84+
return $this;
85+
}
86+
87+
/**
88+
* @return string
89+
*/
90+
public function getFilePath()
91+
{
92+
return $this->filePath;
93+
}
94+
95+
96+
/**
97+
* @return string
98+
*/
99+
public function getShortFilePath()
100+
{
101+
return $this->shortFilePath;
102+
}
103+
104+
/**
105+
* @param mixed $lang
106+
*
107+
* @return LangFileAbstract
108+
*/
109+
public function setLang( $lang )
110+
{
111+
$this->lang = $lang;
112+
113+
return $this;
114+
}
115+
116+
/**
117+
* @return mixed
118+
*/
119+
public function getLang()
120+
{
121+
return $this->lang;
122+
}
123+
124+
/**
125+
* @return mixed
126+
*/
127+
public function getDir()
128+
{
129+
return $this->dir;
130+
}
131+
132+
/**
133+
* @param mixed $dir
134+
*
135+
* @return LangFileAbstract
136+
*/
137+
public function setDir( $dir )
138+
{
139+
$this->dir = $dir;
140+
141+
return $this;
142+
}
143+
144+
/**
145+
* @param mixed $family
146+
*
147+
* @return LangFileAbstract
148+
*/
149+
public function setFamily( $family )
150+
{
151+
$this->family = $family;
152+
153+
return $this;
154+
}
155+
156+
/**
157+
* @return mixed
158+
*/
159+
public function getFamily()
160+
{
161+
return $this->family;
162+
}
163+
164+
/**
165+
* @param mixed $package
166+
*
167+
* @return LangFileAbstract
168+
*/
169+
public function setPackage( $package )
170+
{
171+
$this->package = $package;
172+
173+
return $this;
174+
}
175+
176+
/**
177+
* @return mixed
178+
*/
179+
public function getPackage()
180+
{
181+
return $this->package;
182+
}
183+
184+
/**
185+
* @return string
186+
*/
187+
public function getFileFolderPath()
188+
{
189+
return dirname( $this->filePath );
190+
}
191+
192+
193+
/**
194+
* @return bool
195+
*/
196+
public function isFolderWritable()
197+
{
198+
return is_writable( dirname( $this->getFilePath() ) );
199+
}
200+
201+
/**
202+
* @return bool
203+
*/
204+
public function ensureFolder()
205+
{
206+
$dir = dirname( $this->getFilePath() );
207+
208+
if ( is_dir( $dir ) )
209+
{
210+
return true;
211+
}
212+
213+
return mkdir( $dir );
214+
}
215+
216+
/**
217+
* @return bool
218+
*/
219+
public function fileExists()
220+
{
221+
return file_exists( $this->getFilePath() );
222+
}
223+
224+
/**
225+
* @return bool
226+
*/
227+
public function touch()
228+
{
229+
return touch( $this->getFilePath() );
230+
}
231+
232+
/**
233+
* @return bool
234+
*/
235+
public function isReadable()
236+
{
237+
return is_readable( $this->getFilePath() );
238+
}
239+
240+
/**
241+
* @return bool
242+
*/
243+
public function isWritable()
244+
{
245+
return is_writable( $this->getFilePath() );
246+
}
247+
248+
/**
249+
* @return mixed
250+
*/
251+
public function load()
252+
{
253+
if ( $this->fileExists() )
254+
{
255+
/** @noinspection PhpIncludeInspection */
256+
return include( $this->filePath );
257+
}
258+
else
259+
{
260+
return null;
261+
}
262+
}
263+
264+
}

0 commit comments

Comments
 (0)