-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit 0c8d81d
Showing
10 changed files
with
314 additions
and
0 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 @@ | ||
github: JohnathonKoster |
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,3 @@ | ||
node_modules | ||
vendor | ||
mix-manifest.json |
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,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) <Johnathon Koster> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
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,33 @@ | ||
# Antlers Translation Strings Exporter | ||
|
||
This package extends the capabilities of the [kkomelin/laravel-translatable-string-exporter](https://github.com/kkomelin/laravel-translatable-string-exporter) package, allowing you to export translation strings for both Blade and Antlers templates. | ||
|
||
## How to Install | ||
|
||
This package can be installed by running the following command from the root of your project: | ||
|
||
``` bash | ||
composer require stillat/antlers-translation-strings-exporter --dev | ||
``` | ||
|
||
## How to Use | ||
|
||
Since this package adds additional functionality to [kkomelin/laravel-translatable-string-exporter](https://github.com/kkomelin/laravel-translatable-string-exporter), the usage steps (and configuration) guides are the same. | ||
|
||
At a high level, you can export translation strings by running the following Artisan command from the root of your project: | ||
|
||
``` | ||
php artisan translatable:export <lang> | ||
``` | ||
|
||
For full usage and configuration steps, please refer to [kkomelin/laravel-translatable-string-exporter](https://github.com/kkomelin/laravel-translatable-string-exporter). | ||
|
||
## Credits | ||
|
||
This package simply adds additional functionality to the following package, which does all of the hard work: | ||
|
||
https://github.com/kkomelin/laravel-translatable-string-exporter | ||
|
||
## License | ||
|
||
This package is free software released under the MIT License. |
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,23 @@ | ||
{ | ||
"name": "stillat/antlers-translation-strings-exporter", | ||
"autoload": { | ||
"psr-4": { | ||
"Stillat\\AntlersTranslationStringsExporter\\": "src" | ||
} | ||
}, | ||
"extra": { | ||
"statamic": { | ||
"name": "Antlers Translation Strings Exporter", | ||
"description": "Provides utilities for exporting translation strings from Antlers and Blade template files." | ||
}, | ||
"laravel": { | ||
"providers": [ | ||
"Stillat\\AntlersTranslationStringsExporter\\ServiceProvider" | ||
] | ||
} | ||
}, | ||
"require": { | ||
"kkomelin/laravel-translatable-string-exporter": "^1.21", | ||
"statamic/cms": "^4" | ||
} | ||
} |
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,63 @@ | ||
<?php | ||
|
||
namespace Stillat\AntlersTranslationStringsExporter; | ||
|
||
use KKomelin\TranslatableStringExporter\Core\Exporter; | ||
use KKomelin\TranslatableStringExporter\Core\StringExtractor; | ||
use KKomelin\TranslatableStringExporter\Core\Utils\IO; | ||
use KKomelin\TranslatableStringExporter\Core\Utils\JSON; | ||
use Stillat\AntlersTranslationStringsExporter\Extractor\TranslationStringExtractor; | ||
|
||
class AntlersExporter extends Exporter | ||
{ | ||
/** | ||
* @var StringExtractor | ||
*/ | ||
private $bladeExtractor; | ||
|
||
/** | ||
* @var TranslationStringExtractor | ||
*/ | ||
private $antlersExtractor; | ||
|
||
public function __construct() | ||
{ | ||
$this->bladeExtractor = new StringExtractor(); | ||
$this->antlersExtractor = new TranslationStringExtractor(); | ||
} | ||
|
||
public function export(string $language) | ||
{ | ||
$bladeStrings = $this->bladeExtractor->extract(); | ||
$antlersStrings = $this->antlersExtractor->extract(); | ||
$new_strings = array_merge($bladeStrings, $antlersStrings); | ||
|
||
// Do all the things the package normally does. | ||
|
||
$language_path = IO::languageFilePath($language); | ||
|
||
// Read existing translation file for the chosen language. | ||
$existing_strings = IO::readTranslationFile($language_path); | ||
|
||
// Get the persistent strings. | ||
$persistent_strings_path = | ||
IO::languageFilePath(self::PERSISTENT_STRINGS_FILENAME_WO_EXT); | ||
$persistent_strings = IO::readTranslationFile($persistent_strings_path); | ||
|
||
// Add persistent strings to the export if enabled. | ||
$new_strings = $this->addPersistentStringsIfEnabled($new_strings, $persistent_strings); | ||
|
||
// Merge old an new translations preserving existing translations and persistent strings. | ||
$resulting_strings = $this->mergeStrings($new_strings, $existing_strings, $persistent_strings); | ||
|
||
// Exclude translation keys if enabled through the config. | ||
$resulting_strings = $this->excludeTranslationKeysIfEnabled($resulting_strings, $language); | ||
|
||
// Wisely sort the translations if enabled through the config. | ||
$sorted_strings = $this->advancedSortIfEnabled($resulting_strings); | ||
|
||
// Prepare JSON string and dump it to the translation file. | ||
$content = JSON::jsonEncode($sorted_strings); | ||
IO::write($content, $language_path); | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
|
||
namespace Stillat\AntlersTranslationStringsExporter\Extractor; | ||
|
||
use KKomelin\TranslatableStringExporter\Core\FileFinder; | ||
use Statamic\View\Antlers\Engine; | ||
|
||
class AntlersFileFinder extends FileFinder | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
$this->patterns = []; | ||
|
||
foreach (Engine::EXTENSIONS as $extension) { | ||
$this->patterns[] = '*.'.$extension; | ||
} | ||
} | ||
} |
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,102 @@ | ||
<?php | ||
|
||
namespace Stillat\AntlersTranslationStringsExporter\Extractor; | ||
|
||
use Statamic\View\Antlers\Language\Analyzers\NodeTypeAnalyzer; | ||
use Statamic\View\Antlers\Language\Nodes\AntlersNode; | ||
use Statamic\View\Antlers\Language\Parser\DocumentParser; | ||
use Statamic\View\Antlers\Language\Runtime\EnvironmentDetails; | ||
|
||
class AntlersParser | ||
{ | ||
private $documentParser; | ||
|
||
private $antlersTranslationTags = [ | ||
'trans', | ||
]; | ||
|
||
public function __construct() | ||
{ | ||
$this->documentParser = new DocumentParser(); | ||
|
||
if (NodeTypeAnalyzer::$environmentDetails == null) { | ||
$envDetails = new EnvironmentDetails(); | ||
|
||
$envDetails->setTagNames(app()->make('statamic.tags')->keys()->all()); | ||
$envDetails->setModifierNames(app()->make('statamic.modifiers')->keys()->all()); | ||
|
||
NodeTypeAnalyzer::$environmentDetails = $envDetails; | ||
} | ||
} | ||
|
||
public function parse($file) | ||
{ | ||
$this->documentParser->parse(file_get_contents($file)); | ||
|
||
$result = collect($this->documentParser->getNodes())->where(function ($node) { | ||
return $node instanceof AntlersNode; | ||
}); | ||
|
||
$keys = []; | ||
|
||
/** @var AntlersNode $node */ | ||
foreach ($result as $node) { | ||
if (! empty($node->processedInterpolationRegions)) { | ||
$keys = array_merge($keys, $this->locateInterpolatedTranslationKeys($node)); | ||
} | ||
|
||
if (in_array($node->name->name, $this->antlersTranslationTags)) { | ||
$keyParam = $this->getParameterByName($node, 'key'); | ||
|
||
if ($keyParam == null || $keyParam->isModifierParameter || $keyParam->isVariableReference) { | ||
$keys[] = $node->name->methodPart; | ||
|
||
continue; | ||
} | ||
|
||
$keys[] = $keyParam->value; | ||
} | ||
} | ||
|
||
return $keys; | ||
} | ||
|
||
private function locateInterpolatedTranslationKeys(AntlersNode $rootNode, $foundKeys = []) | ||
{ | ||
foreach ($rootNode->processedInterpolationRegions as $interpolationRegion) { | ||
/** @var AntlersNode $node */ | ||
$node = $interpolationRegion[0]; | ||
|
||
if (! empty($node->processedInterpolationRegions)) { | ||
$foundKeys = array_merge($foundKeys, $this->locateInterpolatedTranslationKeys($node, $foundKeys)); | ||
} | ||
|
||
if (in_array($node->name->name, $this->antlersTranslationTags)) { | ||
$keyParam = $this->getParameterByName($node, 'key'); | ||
|
||
if ($keyParam == null || $keyParam->isModifierParameter || $keyParam->isVariableReference) { | ||
$foundKeys[] = $node->name->methodPart; | ||
|
||
continue; | ||
} | ||
|
||
$foundKeys[] = $keyParam->value; | ||
} | ||
} | ||
|
||
return $foundKeys; | ||
} | ||
|
||
private function getParameterByName(AntlersNode $node, $paramName) | ||
{ | ||
if ($node->hasParameters) { | ||
foreach ($node->parameters as $parameter) { | ||
if ($parameter->name == $paramName) { | ||
return $parameter; | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
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,36 @@ | ||
<?php | ||
|
||
namespace Stillat\AntlersTranslationStringsExporter\Extractor; | ||
|
||
use KKomelin\TranslatableStringExporter\Core\StringExtractor; | ||
|
||
class TranslationStringExtractor extends StringExtractor | ||
{ | ||
/** | ||
* @var AntlersFileFinder | ||
*/ | ||
private $finder; | ||
|
||
/** | ||
* @var AntlersParser | ||
*/ | ||
private $parser; | ||
|
||
public function __construct() | ||
{ | ||
$this->finder = new AntlersFileFinder(); | ||
$this->parser = new AntlersParser(); | ||
} | ||
|
||
public function extract() | ||
{ | ||
$strings = []; | ||
|
||
$files = $this->finder->find(); | ||
foreach ($files as $file) { | ||
$strings = array_merge($strings, $this->parser->parse($file)); | ||
} | ||
|
||
return $this->formatArray($strings); | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
namespace Stillat\AntlersTranslationStringsExporter; | ||
|
||
class ServiceProvider extends \Illuminate\Support\ServiceProvider | ||
{ | ||
public function register() | ||
{ | ||
$this->app->singleton('translatable-string-exporter-exporter', function ($app) { | ||
return $app->make(AntlersExporter::class); | ||
}); | ||
} | ||
} |