From 170636b75b0fd4dd894418086e7818426882c256 Mon Sep 17 00:00:00 2001 From: Wim De Meester Date: Sat, 21 Nov 2020 20:45:04 +0100 Subject: [PATCH] Make laravel-gettext work with components --- .../LaravelGettext/Commands/GettextUpdate.php | 7 +- src/deepskylog/LaravelGettext/FileSystem.php | 91 ++++++++++++------- .../Translators/BaseTranslator.php | 12 +-- .../LaravelGettext/Translators/Gettext.php | 10 +- .../LaravelGettext/Translators/Symfony.php | 10 +- 5 files changed, 80 insertions(+), 50 deletions(-) diff --git a/src/deepskylog/LaravelGettext/Commands/GettextUpdate.php b/src/deepskylog/LaravelGettext/Commands/GettextUpdate.php index a798623..aa99f83 100644 --- a/src/deepskylog/LaravelGettext/Commands/GettextUpdate.php +++ b/src/deepskylog/LaravelGettext/Commands/GettextUpdate.php @@ -3,8 +3,8 @@ namespace deepskylog\LaravelGettext\Commands; use Exception; -use deepskylog\LaravelGettext\Exceptions\DirectoryNotFoundException; use Symfony\Component\Console\Input\InputOption; +use deepskylog\LaravelGettext\Exceptions\DirectoryNotFoundException; class GettextUpdate extends BaseCommand { @@ -43,7 +43,7 @@ public function handle() } $count = [ - 'added' => 0, + 'added' => 0, 'updated' => 0, ]; @@ -86,6 +86,7 @@ public function handle() $count['updated']++; } } + // dd('test', $localePath); $this->info('Finished'); @@ -127,7 +128,7 @@ protected function getOptions() InputOption::VALUE_OPTIONAL, 'Update files only for this domain', null, - ] + ], ]; } } diff --git a/src/deepskylog/LaravelGettext/FileSystem.php b/src/deepskylog/LaravelGettext/FileSystem.php index f138f88..b6307a7 100644 --- a/src/deepskylog/LaravelGettext/FileSystem.php +++ b/src/deepskylog/LaravelGettext/FileSystem.php @@ -2,11 +2,11 @@ namespace deepskylog\LaravelGettext; -use RecursiveDirectoryIterator; use RecursiveIteratorIterator; +use RecursiveDirectoryIterator; use deepskylog\LaravelGettext\Config\Models\Config; -use deepskylog\LaravelGettext\Exceptions\DirectoryNotFoundException; use deepskylog\LaravelGettext\Exceptions\FileCreationException; +use deepskylog\LaravelGettext\Exceptions\DirectoryNotFoundException; use deepskylog\LaravelGettext\Exceptions\LocaleFileNotFoundException; class FileSystem @@ -55,22 +55,13 @@ class FileSystem public function __construct(Config $config, $basePath, $storagePath) { $this->configuration = $config; - $this->basePath = $basePath; + $this->basePath = $basePath; - $this->storagePath = $storagePath; + $this->storagePath = $storagePath; $this->storageContainer = 'framework'; - $this->folderName = 'i18n'; + $this->folderName = 'i18n'; } - /** - * Build views in order to parse php files. - * - * @param Array $viewPaths - * @param String $domain - * - * @return Boolean status - */ - /** * Build views in order to parse php files. * @@ -100,19 +91,57 @@ public function compileViews(array $viewPaths, $domain) throw new Exceptions\DirectoryNotFoundException("Failed to resolve $path, please check that it exists"); } - $fs = new \Illuminate\Filesystem\Filesystem($path); + $fs = new \Illuminate\Filesystem\Filesystem($path); $files = $fs->allFiles($realPath); $compiler = new \Illuminate\View\Compilers\BladeCompiler($fs, $domainDir); foreach ($files as $file) { + // try { $filePath = $file->getRealPath(); $compiler->setPath($filePath); - $contents = $compiler->compileString($fs->get($filePath)); - + try { + $contents = $compiler->compileString($fs->get($filePath)); + } catch (\Throwable $th) { + // If we have an exception, we should remove the from the file + $file = $fs->get($filePath); + // Get the name of the component from the exception + $totalLength = strlen($th->getMessage()); + $startOfComponent = strpos($th->getMessage(), '[') + 1; + $componentName = substr($th->getMessage(), $startOfComponent, $totalLength - $startOfComponent - 2); + $componentName = '') + 1; + $componentString = substr($componentString, 0, $endOfComponent); + + // Remove from the + $newString = str_replace($componentString, '', $file); + // Compile the new string + try { + $contents = $compiler->compileString($newString); + } catch (\Throwable $th) { + // If we have an exception, we should remove the from the file + $file = $fs->get($filePath); + // Get the name of the component from the exception + $totalLength = strlen($th->getMessage()); + $startOfComponent = strpos($th->getMessage(), '[') + 1; + $componentName = substr($th->getMessage(), $startOfComponent, $totalLength - $startOfComponent - 2); + $componentName = '') + 1; + $componentString = substr($componentString, 0, $endOfComponent); + + // Remove from the + $newString = str_replace($componentString, '', $file); + // Compile the new string + } + } $compiledPath = $compiler->getCompiledPath($compiler->getPath()); - $fs->put( $compiledPath . '.php', $contents @@ -156,10 +185,10 @@ public function getDomainPath($append = null) */ public function createPOFile($path, $locale, $domain, $write = true) { - $project = $this->configuration->getProject(); - $timestamp = date('Y-m-d H:iO'); + $project = $this->configuration->getProject(); + $timestamp = date('Y-m-d H:iO'); $translator = $this->configuration->getTranslator(); - $encoding = $this->configuration->getEncoding(); + $encoding = $this->configuration->getEncoding(); $relativePath = $this->configuration->getRelativePath(); @@ -203,7 +232,7 @@ public function createPOFile($path, $locale, $domain, $write = true) } // File creation - $file = fopen($path, 'w'); + $file = fopen($path, 'w'); $result = fwrite($file, $template); fclose($file); @@ -236,7 +265,7 @@ public function addLocale($localePath, $locale) { $data = [ $localePath, - 'LC_MESSAGES' + 'LC_MESSAGES', ]; if (!file_exists($localePath)) { @@ -295,7 +324,6 @@ public function updateLocale($localePath, $locale, $domain) $customLocale = ['C']; array_splice($data, 1, 0, $customLocale); } - $localePOPath = implode(DIRECTORY_SEPARATOR, $data); if (!file_exists($localePOPath) || !$localeContents = file_get_contents($localePOPath)) { @@ -310,7 +338,6 @@ public function updateLocale($localePath, $locale, $domain) $domain, false ); - // Header replacement $localeContents = preg_replace('/^([^#])+:?/', $newHeader, $localeContents); @@ -319,7 +346,9 @@ public function updateLocale($localePath, $locale, $domain) sprintf('Can\'t write on %s', $localePOPath) ); } - + if ($locale == 'nl_NL') { + // dd($localePOPath, $localeContents); + } return true; } @@ -335,12 +364,12 @@ public function getRelativePath($from, $to) { // Compatibility fixes for Windows paths $from = is_dir($from) ? rtrim($from, '\/') . '/' : $from; - $to = is_dir($to) ? rtrim($to, '\/') . '/' : $to; + $to = is_dir($to) ? rtrim($to, '\/') . '/' : $to; $from = str_replace('\\', '/', $from); - $to = str_replace('\\', '/', $to); + $to = str_replace('\\', '/', $to); - $from = explode('/', $from); - $to = explode('/', $to); + $from = explode('/', $from); + $to = explode('/', $to); $relPath = $to; foreach ($from as $depth => $dir) { @@ -610,7 +639,7 @@ public function makeFilePath($locale, $domain, $type = 'po') [ $locale, 'LC_MESSAGES', - $domain . '.' . $type + $domain . '.' . $type, ] ); diff --git a/src/deepskylog/LaravelGettext/Translators/BaseTranslator.php b/src/deepskylog/LaravelGettext/Translators/BaseTranslator.php index 7bd7745..40d5fbf 100644 --- a/src/deepskylog/LaravelGettext/Translators/BaseTranslator.php +++ b/src/deepskylog/LaravelGettext/Translators/BaseTranslator.php @@ -2,11 +2,11 @@ namespace deepskylog\LaravelGettext\Translators; -use deepskylog\LaravelGettext\Adapters\AdapterInterface; -use deepskylog\LaravelGettext\Config\Models\Config; -use deepskylog\LaravelGettext\Exceptions\UndefinedDomainException; use deepskylog\LaravelGettext\FileSystem; use deepskylog\LaravelGettext\Storages\Storage; +use deepskylog\LaravelGettext\Config\Models\Config; +use deepskylog\LaravelGettext\Adapters\AdapterInterface; +use deepskylog\LaravelGettext\Exceptions\UndefinedDomainException; abstract class BaseTranslator implements TranslatorInterface { @@ -53,9 +53,9 @@ public function __construct( ) { // Sets the package configuration and session handler $this->configuration = $config; - $this->adapter = $adapter; - $this->fileSystem = $fileSystem; - $this->storage = $storage; + $this->adapter = $adapter; + $this->fileSystem = $fileSystem; + $this->storage = $storage; } /** diff --git a/src/deepskylog/LaravelGettext/Translators/Gettext.php b/src/deepskylog/LaravelGettext/Translators/Gettext.php index 487754b..2cbd42f 100644 --- a/src/deepskylog/LaravelGettext/Translators/Gettext.php +++ b/src/deepskylog/LaravelGettext/Translators/Gettext.php @@ -3,11 +3,11 @@ namespace deepskylog\LaravelGettext\Translators; use deepskylog\LaravelGettext\FileSystem; -use deepskylog\LaravelGettext\Adapters\AdapterInterface; +use deepskylog\LaravelGettext\Storages\Storage; use deepskylog\LaravelGettext\Config\Models\Config; -use deepskylog\LaravelGettext\Exceptions\LocaleNotSupportedException; +use deepskylog\LaravelGettext\Adapters\AdapterInterface; use deepskylog\LaravelGettext\Exceptions\UndefinedDomainException; -use deepskylog\LaravelGettext\Storages\Storage; +use deepskylog\LaravelGettext\Exceptions\LocaleNotSupportedException; /** * Class implemented by the php-gettext module translator. @@ -91,7 +91,7 @@ public function setLocale($locale) } try { - $customLocale = $this->configuration->getCustomLocale() ? 'C.' : $locale . '.'; + $customLocale = $this->configuration->getCustomLocale() ? 'C.' : $locale . '.'; $gettextLocale = $customLocale . $this->getEncoding(); // Update all categories set in config @@ -109,7 +109,7 @@ public function setLocale($locale) return $this->getLocale(); } catch (\Exception $e) { - $this->locale = $this->configuration->getFallbackLocale(); + $this->locale = $this->configuration->getFallbackLocale(); $exceptionPosition = $e->getFile() . ':' . $e->getLine(); throw new \Exception($exceptionPosition . $e->getMessage()); } diff --git a/src/deepskylog/LaravelGettext/Translators/Symfony.php b/src/deepskylog/LaravelGettext/Translators/Symfony.php index f5a2d36..8c7b115 100644 --- a/src/deepskylog/LaravelGettext/Translators/Symfony.php +++ b/src/deepskylog/LaravelGettext/Translators/Symfony.php @@ -2,14 +2,14 @@ namespace deepskylog\LaravelGettext\Translators; +use deepskylog\LaravelGettext\FileSystem; +use deepskylog\LaravelGettext\Storages\Storage; +use deepskylog\LaravelGettext\Config\Models\Config; +use deepskylog\LaravelGettext\FileLoader\MoFileLoader; use Symfony\Component\Translation\Loader\PoFileLoader; -use Symfony\Component\Translation\Translator as SymfonyTranslator; use deepskylog\LaravelGettext\Adapters\AdapterInterface; -use deepskylog\LaravelGettext\Config\Models\Config; +use Symfony\Component\Translation\Translator as SymfonyTranslator; use deepskylog\LaravelGettext\FileLoader\Cache\ApcuFileCacheLoader; -use deepskylog\LaravelGettext\FileLoader\MoFileLoader; -use deepskylog\LaravelGettext\FileSystem; -use deepskylog\LaravelGettext\Storages\Storage; /** * Class implemented by Symfony translation component.