| 1 |
<?php |
| 2 |
namespace Depicter\Services; |
| 3 |
|
| 4 |
class I18nService |
| 5 |
{ |
| 6 |
/** |
| 7 |
* @var string |
| 8 |
*/ |
| 9 |
public $phpFileUrl; |
| 10 |
|
| 11 |
/** |
| 12 |
* @var string |
| 13 |
*/ |
| 14 |
public $jsonUrl; |
| 15 |
|
| 16 |
/** |
| 17 |
* @var string |
| 18 |
*/ |
| 19 |
public $json; |
| 20 |
|
| 21 |
/** |
| 22 |
* I18nService constructor. |
| 23 |
* |
| 24 |
* @param $phpFileUrl |
| 25 |
* @param $jsonUrl |
| 26 |
*/ |
| 27 |
public function __construct( $phpFileUrl, $jsonUrl ) |
| 28 |
{ |
| 29 |
$this->phpFileUrl = $phpFileUrl; |
| 30 |
$this->jsonUrl = $jsonUrl; |
| 31 |
$this->readJson(); |
| 32 |
$this->run(); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* read json file |
| 37 |
* |
| 38 |
* @return void |
| 39 |
*/ |
| 40 |
public function readJson() { |
| 41 |
$this->json = file_get_contents( $this->jsonUrl ); |
| 42 |
} |
| 43 |
|
| 44 |
public function run() { |
| 45 |
$data = json_decode( $this->json, true ); |
| 46 |
|
| 47 |
$newTexts = "\t\t\t/** ==EditorLocalizationList==START== **/\n\t\t\t"; |
| 48 |
foreach ( $data as $key => $value ) { |
| 49 |
|
| 50 |
$key = str_replace(array("\r\n", "\n", "\r"), '\n', $key); |
| 51 |
$key = str_replace(array("'"), "\'", $key); |
| 52 |
$newTexts .= "'".$key."' => __('".$key."', 'depicter'),\n\t\t\t"; |
| 53 |
} |
| 54 |
$newTexts .= "/** ==EditorLocalizationList==END== **/\n"; |
| 55 |
|
| 56 |
$this->searchAndReplacePhpFile( $newTexts ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* search and replace php file for new texts |
| 61 |
* @param $newTexts |
| 62 |
*/ |
| 63 |
public function searchAndReplacePhpFile( $newTexts ) { |
| 64 |
$lines = file( $this->phpFileUrl ); |
| 65 |
$startLine = 0; |
| 66 |
$endLine = 0; |
| 67 |
foreach ( $lines as $lineNumber => $line ) { |
| 68 |
if ( $lineNumber == 0 ) { |
| 69 |
continue; |
| 70 |
} |
| 71 |
|
| 72 |
if ( strpos( "'" . $line ."'", '/** ==EditorLocalizationList==START== **/' ) !== false ) { |
| 73 |
$startLine = $lineNumber; |
| 74 |
} |
| 75 |
|
| 76 |
if ( strpos( $line, '/** ==EditorLocalizationList==END== **/' ) !== false ) { |
| 77 |
$endLine = $lineNumber; |
| 78 |
} |
| 79 |
|
| 80 |
if ( $lineNumber >= $startLine && $startLine ) { |
| 81 |
unset( $lines[ $lineNumber ] ); |
| 82 |
} |
| 83 |
|
| 84 |
if ( $lineNumber == $endLine ) { |
| 85 |
$lines[ $lineNumber ] = $newTexts; |
| 86 |
break; |
| 87 |
} |
| 88 |
} |
| 89 |
ksort( $lines ); |
| 90 |
file_put_contents( $this->phpFileUrl, implode( '', $lines ) ); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
$editorLocalizeFile = 'app/src/Editor/EditorLocalization.php'; |
| 95 |
$jsonFile = 'resources/scripts/i18n/en/locale.json'; |
| 96 |
$translateHandler = new I18nService( $editorLocalizeFile, $jsonFile ); |
| 97 |
|