Skip to content

Commit 606fc90

Browse files
author
Ilya Sakovich
committed
Added command for publishing translations
1 parent 982c083 commit 606fc90

8 files changed

+223
-0
lines changed

.gitattributes

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/.gitattributes export-ignore
2+
/.gitignore export-ignore
3+
/.styleci.yml export-ignore

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor
2+
composer.lock

.styleci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
preset: laravel

CONTRIBUTING.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Contributing
2+
3+
Contributions are **welcome** and will be fully **credited**.
4+
5+
Please read and understand the contribution guide before creating an issue or pull request.
6+
7+
## Requirements
8+
9+
If the project maintainer has any additional requirements, you will find them listed here.
10+
11+
12+
- **[PSR-2 Coding Standard.](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** The easiest way to apply the conventions is to install [PHP CS Fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer).
13+
- **Add tests!** Your patch won't be accepted if it doesn't have tests.
14+
- **Document any change in behaviour.** Make sure the `README.md` and any other relevant documentation are kept up-to-date.
15+
- **Consider our release cycle.** We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.
16+
- **Create feature branches.** Don't ask us to pull from your master branch.
17+
- **One pull request per feature.** If you want to do more than one thing, send multiple pull requests.
18+
- **Send coherent history.** Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.
19+
20+
**Happy coding!**

LICENSE.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Coderello
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

composer.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "coderello/laravel-nova-lang",
3+
"description": "Language support for Laravel Nova.",
4+
"keywords": ["laravel", "nova", "language", "lang", "support"],
5+
"type": "library",
6+
"license": "MIT",
7+
"require": {
8+
"php": ">=7.1",
9+
"laravel/framework": "5.5.*|5.6.*"
10+
},
11+
"autoload": {
12+
"psr-4": {
13+
"Coderello\\LaravelNovaLang\\": "src/"
14+
}
15+
},
16+
"extra": {
17+
"laravel": {
18+
"providers": [
19+
"Coderello\\LaravelNovaLang\\Providers\\LaravelNovaLangServiceProvider"
20+
]
21+
}
22+
}
23+
}

src/Commands/NovaLangPublish.php

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
namespace Coderello\LaravelNovaLang\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Filesystem\Filesystem;
7+
use Illuminate\Support\Collection;
8+
use SplFileInfo;
9+
10+
class NovaLangPublish extends Command
11+
{
12+
/**
13+
* The name and signature of the console command.
14+
*
15+
* @var string
16+
*/
17+
protected $signature = 'nova-lang:publish
18+
{locales : Comma separated list of languages}
19+
{--force : Override existing files}';
20+
21+
/**
22+
* The console command description.
23+
*
24+
* @var string
25+
*/
26+
protected $description = 'Publish Laravel Nova language files to resource folder.';
27+
28+
/**
29+
* @var Filesystem
30+
*/
31+
protected $filesystem;
32+
33+
/**
34+
* Create a new command instance.
35+
*
36+
* @param Filesystem $filesystem
37+
*/
38+
public function __construct(Filesystem $filesystem)
39+
{
40+
$this->filesystem = $filesystem;
41+
42+
parent::__construct();
43+
}
44+
45+
/**
46+
* Execute the console command.
47+
*
48+
* @return mixed
49+
*/
50+
public function handle()
51+
{
52+
$availableLocales = $this->getAvailableLocales();
53+
54+
$this->getRequestedLocales()->each(function (string $locale) use ($availableLocales) {
55+
if (! $availableLocales->contains($locale)) {
56+
$this->error(sprintf('Unfortunately, translations for [%s] locale doesn\'t exist. Feel free to send a PR to add them and help other people :)', $locale));
57+
58+
return;
59+
}
60+
61+
$inputDirectory = $this->directoryFrom().'/'.$locale;
62+
63+
$outputDirectory = $this->directoryTo().'/'.$locale;
64+
65+
$inputFile = $inputDirectory.'.json';
66+
67+
$outputFile = $outputDirectory.'.json';
68+
69+
if (($this->filesystem->exists($outputDirectory)
70+
|| $this->filesystem->exists($outputFile))
71+
&& ! $this->isForce()) {
72+
$this->error(sprintf('Translations for [%s] locale already exist.', $locale));
73+
74+
return;
75+
}
76+
77+
$this->filesystem->makeDirectory($outputDirectory, 0777, true, true);
78+
79+
$this->filesystem->copyDirectory($inputDirectory, $outputDirectory);
80+
81+
$this->filesystem->copy($inputFile, $outputFile);
82+
83+
$this->info(sprintf('Translations for [%s] locale have been published successfully.', $locale));
84+
});
85+
}
86+
87+
protected function getRequestedLocales(): Collection
88+
{
89+
return collect(explode(',', $this->argument('locales')));
90+
}
91+
92+
protected function getAvailableLocales(): Collection
93+
{
94+
$localesByDirectories = collect($this->filesystem->directories($this->directoryFrom()))
95+
->map(function (string $path) {
96+
return $this->filesystem->name($path);
97+
});
98+
99+
$localesByFiles = collect($this->filesystem->files($this->directoryFrom()))
100+
->map(function (SplFileInfo $splFileInfo) {
101+
return str_replace('.'.$splFileInfo->getExtension(), '', $splFileInfo->getFilename());
102+
});
103+
104+
return $localesByDirectories->intersect($localesByFiles)->values();
105+
}
106+
107+
protected function isForce(): bool
108+
{
109+
return $this->option('force');
110+
}
111+
112+
protected function directoryFrom(): string
113+
{
114+
return base_path('vendor/coderello/laravel-nova-lang/resources/lang');
115+
}
116+
117+
protected function directoryTo(): string
118+
{
119+
return resource_path('lang/vendor/nova');
120+
}
121+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Coderello\LaravelNovaLang\Providers;
4+
5+
use Coderello\LaravelNovaLang\Commands\NovaLangPublish;
6+
use Illuminate\Support\ServiceProvider;
7+
8+
class LaravelNovaLangServiceProvider extends ServiceProvider
9+
{
10+
/**
11+
* Bootstrap any application services.
12+
*
13+
* @return void
14+
*/
15+
public function boot()
16+
{
17+
//
18+
}
19+
20+
/**
21+
* Register any application services.
22+
*
23+
* @return void
24+
*/
25+
public function register()
26+
{
27+
$this->app->singleton('command.publish.nova-lang', NovaLangPublish::class);
28+
$this->commands([
29+
'command.publish.nova-lang',
30+
]);
31+
}
32+
}

0 commit comments

Comments
 (0)