Skip to content

Commit

Permalink
Make laravel-gettext work with components
Browse files Browse the repository at this point in the history
  • Loading branch information
WimDeMeester committed Nov 21, 2020
1 parent 087a849 commit 170636b
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 50 deletions.
7 changes: 4 additions & 3 deletions src/deepskylog/LaravelGettext/Commands/GettextUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -43,7 +43,7 @@ public function handle()
}

$count = [
'added' => 0,
'added' => 0,
'updated' => 0,
];

Expand Down Expand Up @@ -86,6 +86,7 @@ public function handle()
$count['updated']++;
}
}
// dd('test', $localePath);

$this->info('Finished');

Expand Down Expand Up @@ -127,7 +128,7 @@ protected function getOptions()
InputOption::VALUE_OPTIONAL,
'Update files only for this domain',
null,
]
],
];
}
}
91 changes: 60 additions & 31 deletions src/deepskylog/LaravelGettext/FileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 <x-media-library-attachment /> 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 = '<x-' . $componentName;

// Get the full component, not only the name
$componentString = substr($file, strpos($file, $componentName));
$endOfComponent = strpos($componentString, '>') + 1;
$componentString = substr($componentString, 0, $endOfComponent);

// Remove from the <x-componentName to '/>
$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 <x-media-library-attachment /> 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 = '<x-' . $componentName;

// Get the full component, not only the name
$componentString = substr($file, strpos($file, $componentName));
$endOfComponent = strpos($componentString, '>') + 1;
$componentString = substr($componentString, 0, $endOfComponent);

// Remove from the <x-componentName to '/>
$newString = str_replace($componentString, '', $file);
// Compile the new string
}
}
$compiledPath = $compiler->getCompiledPath($compiler->getPath());

$fs->put(
$compiledPath . '.php',
$contents
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -236,7 +265,7 @@ public function addLocale($localePath, $locale)
{
$data = [
$localePath,
'LC_MESSAGES'
'LC_MESSAGES',
];

if (!file_exists($localePath)) {
Expand Down Expand Up @@ -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)) {
Expand All @@ -310,7 +338,6 @@ public function updateLocale($localePath, $locale, $domain)
$domain,
false
);

// Header replacement
$localeContents = preg_replace('/^([^#])+:?/', $newHeader, $localeContents);

Expand All @@ -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;
}

Expand All @@ -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) {
Expand Down Expand Up @@ -610,7 +639,7 @@ public function makeFilePath($locale, $domain, $type = 'po')
[
$locale,
'LC_MESSAGES',
$domain . '.' . $type
$domain . '.' . $type,
]
);

Expand Down
12 changes: 6 additions & 6 deletions src/deepskylog/LaravelGettext/Translators/BaseTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/deepskylog/LaravelGettext/Translators/Gettext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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());
}
Expand Down
10 changes: 5 additions & 5 deletions src/deepskylog/LaravelGettext/Translators/Symfony.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 170636b

Please sign in to comment.