Skip to content

Commit

Permalink
Rework everything
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarpsvo committed Nov 3, 2020
1 parent f57bd76 commit 2c0b991
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.0.0] - 03-11-2020

### Changed

- Fixed translations publishing
- Changed the logic from class with static functions to a trait

## [1.0.5] - 22-10-2020

### Changed
Expand Down
41 changes: 15 additions & 26 deletions src/NovaTranslationsLoader.php → src/LoadsNovaTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,13 @@
use Laravel\Nova\Nova;
use Laravel\Nova\Events\ServingNova;
use Illuminate\Support\Facades\File;
use Illuminate\Support\ServiceProvider;

class NovaTranslationsLoader extends ServiceProvider
trait LoadsNovaTranslations
{
protected $packageTranslationsDir;
protected $packageName;
protected $publishTranslations;

public function __construct($packageTranslationsDir, $packageName, $publishTranslations = true)
{
$this->app = app();
$this->packageTranslationsDir = $packageTranslationsDir ?? __DIR__ . '/../resources/lang';
$this->packageName = $packageName;
$this->publishTranslations = $publishTranslations;
}

/**
* Loads translations into the Nova system.
*
Expand All @@ -29,42 +20,40 @@ public function __construct($packageTranslationsDir, $packageName, $publishTrans
* @param boolean $publishTranslations Whether to also automatically make translations publishable.
* @return null
**/
public static function loadTranslations($packageTranslationsDir, $packageName, $publishTranslations = true)
protected function loadTranslations($packageTranslationsDir, $packageName, $publishTranslations = true)
{
$packageTranslationsDir = $packageTranslationsDir ?? __DIR__ . '/../resources/lang';
$packageTranslationsDir = rtrim($packageTranslationsDir, '/');
$packageName = trim($packageName);

$translationsLoader = new NovaTranslationsLoader($packageTranslationsDir, $packageName, $publishTranslations);
return $translationsLoader->translations();
$this->translations($packageTranslationsDir, $packageName, $publishTranslations);
}

protected function translations()
private function translations($pckgTransDir, $pckgName, $publish)
{
if (app()->runningInConsole() && $this->publishTranslations) {
$this->publishes([$this->packageTranslationsDir => resource_path("lang/vendor/{$this->packageName}")], 'translations');
if (app()->runningInConsole() && $publish) {
$this->publishes([$pckgTransDir => resource_path("lang/vendor/{$pckgName}")], 'translations');
return;
}

if (method_exists('Nova', 'translations')) {
Nova::serving(function (ServingNova $event) {
Nova::serving(function (ServingNova $event) use ($pckgTransDir, $pckgName) {
$locale = app()->getLocale();
$fallbackLocale = config('app.fallback_locale');

if ($this->attemptToLoadTranslations($locale, 'project')) return;
if ($this->attemptToLoadTranslations($locale, 'local')) return;
if ($this->attemptToLoadTranslations($fallbackLocale, 'project')) return;
if ($this->attemptToLoadTranslations($fallbackLocale, 'local')) return;
$this->attemptToLoadTranslations('en', 'local');
if ($this->attemptToLoadTranslations($locale, 'project', $pckgTransDir, $pckgName)) return;
if ($this->attemptToLoadTranslations($locale, 'local', $pckgTransDir, $pckgName)) return;
if ($this->attemptToLoadTranslations($fallbackLocale, 'project', $pckgTransDir, $pckgName)) return;
if ($this->attemptToLoadTranslations($fallbackLocale, 'local', $pckgTransDir, $pckgName)) return;
$this->attemptToLoadTranslations('en', 'local', $pckgTransDir, $pckgName);
});
}
}

protected function attemptToLoadTranslations($locale, $from)
private function attemptToLoadTranslations($locale, $from, $packageTranslationsDir, $packageName)
{
$fileDir = $from === 'local'
? "{$this->packageTranslationsDir}"
: resource_path("lang/vendor/{$this->packageName}");
? $packageTranslationsDir
: resource_path("lang/vendor/{$packageName}");

$filePath = "$fileDir/{$locale}.json";

Expand Down

0 comments on commit 2c0b991

Please sign in to comment.