Skip to content

Commit

Permalink
Hello, universe. 🥳
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnathonKoster committed Jul 9, 2023
0 parents commit 0c8d81d
Show file tree
Hide file tree
Showing 10 changed files with 314 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: JohnathonKoster
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
vendor
mix-manifest.json
21 changes: 21 additions & 0 deletions LICENSE.md
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.
33 changes: 33 additions & 0 deletions README.md
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.
23 changes: 23 additions & 0 deletions composer.json
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"
}
}
63 changes: 63 additions & 0 deletions src/AntlersExporter.php
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);
}
}
19 changes: 19 additions & 0 deletions src/Extractor/AntlersFileFinder.php
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;
}
}
}
102 changes: 102 additions & 0 deletions src/Extractor/AntlersParser.php
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;
}
}
36 changes: 36 additions & 0 deletions src/Extractor/TranslationStringExtractor.php
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);
}
}
13 changes: 13 additions & 0 deletions src/ServiceProvider.php
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);
});
}
}

0 comments on commit 0c8d81d

Please sign in to comment.