This repository has been archived by the owner on Jul 16, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e20fda
commit 16d840d
Showing
4 changed files
with
181 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnError="false" | ||
stopOnFailure="false" | ||
syntaxCheck="true" | ||
verbose="true" | ||
> | ||
<testsuites> | ||
<testsuite name="Test Suite"> | ||
<directory suffix="Test.php">./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php namespace MartinLindhe\VueInternationalizationGenerator; | ||
|
||
use DirectoryIterator; | ||
use Exception; | ||
|
||
class Generator | ||
{ | ||
/** | ||
* @param string $path | ||
* @return string | ||
* @throws Exception | ||
*/ | ||
public function generateFromPath($path) | ||
{ | ||
if (!is_dir($path)) { | ||
throw new Exception('Directory not found: '.$path); | ||
} | ||
|
||
$locales = []; | ||
$dir = new DirectoryIterator($path); | ||
foreach ($dir as $fileinfo) { | ||
if (!$fileinfo->isDot() | ||
&& $fileinfo->isDir() | ||
&& !in_array($fileinfo->getFilename(), ['vendor']) | ||
) { | ||
$locales[$fileinfo->getFilename()] = | ||
$this->allocateLocaleArray($path . '/' . $fileinfo->getFilename()); | ||
} | ||
} | ||
|
||
return 'export default ' | ||
. json_encode($locales, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . PHP_EOL; | ||
} | ||
|
||
/** | ||
* @param string $path | ||
* @return array | ||
*/ | ||
private function allocateLocaleArray($path) | ||
{ | ||
$data = []; | ||
|
||
$dir = new DirectoryIterator($path); | ||
foreach ($dir as $fileinfo) { | ||
if (!$fileinfo->isDot()) { | ||
$noExt = $this->removeExtension($fileinfo->getFilename()); | ||
$data[$noExt] = include($path . '/' . $fileinfo->getFilename()); | ||
} | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* @param string $filename | ||
* @return string | ||
*/ | ||
private function removeExtension($filename) | ||
{ | ||
$pos = mb_strrpos($filename, '.'); | ||
if ($pos === false) { | ||
return $filename; | ||
} | ||
|
||
return mb_substr($filename, 0, $pos); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
use MartinLindhe\VueInternationalizationGenerator\Generator; | ||
|
||
class GenerateTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
private function generateLocaleFilesFrom(array $arr) | ||
{ | ||
$root = sys_get_temp_dir() . '/' . sha1(microtime(true) . mt_rand()); | ||
|
||
if (!is_dir($root)) { | ||
mkdir($root, 0777, true); | ||
} | ||
|
||
foreach ($arr as $key => $val) { | ||
|
||
if (!is_dir($root . '/' . $key)) { | ||
mkdir($root . '/' . $key); | ||
} | ||
|
||
foreach ($val as $group => $content) { | ||
$outFile = $root . '/'. $key . '/' . $group . '.php'; | ||
file_put_contents($outFile, '<?php return ' . var_export($content, true) . ';'); | ||
} | ||
} | ||
|
||
return $root; | ||
} | ||
|
||
private function destroyLocaleFilesFrom(array $arr, $root) | ||
{ | ||
foreach ($arr as $key => $val) { | ||
|
||
foreach ($val as $group => $content) { | ||
$outFile = $root . '/'. $key . '/' . $group . '.php'; | ||
if (file_exists($outFile)) { | ||
unlink($outFile); | ||
} | ||
} | ||
|
||
if (is_dir($root . '/' . $key)) { | ||
rmdir($root . '/' . $key); | ||
} | ||
|
||
} | ||
|
||
if (is_dir($root)) { | ||
rmdir($root); | ||
} | ||
} | ||
|
||
function testBasic() | ||
{ | ||
$arr = [ | ||
'en' => [ | ||
'help' => [ | ||
'yes' => 'yes', | ||
'no' => 'no', | ||
] | ||
], | ||
'sv' => [ | ||
'help' => [ | ||
'yes' => 'ja', | ||
'no' => 'nej', | ||
] | ||
] | ||
]; | ||
|
||
$root = $this->generateLocaleFilesFrom($arr); | ||
|
||
$this->assertEquals( | ||
'export default {' . PHP_EOL | ||
. ' "en": {' . PHP_EOL | ||
. ' "help": {' . PHP_EOL | ||
. ' "yes": "yes",' . PHP_EOL | ||
. ' "no": "no"' . PHP_EOL | ||
. ' }' . PHP_EOL | ||
. ' },' . PHP_EOL | ||
. ' "sv": {' . PHP_EOL | ||
. ' "help": {' . PHP_EOL | ||
. ' "yes": "ja",' . PHP_EOL | ||
. ' "no": "nej"' . PHP_EOL | ||
. ' }' . PHP_EOL | ||
. ' }' . PHP_EOL | ||
. '}' . PHP_EOL, | ||
(new Generator)->generateFromPath($root)); | ||
|
||
$this->destroyLocaleFilesFrom($arr, $root); | ||
} | ||
} |