From 2c0b99114d778adb62b3f1279a5a651c2737bf8e Mon Sep 17 00:00:00 2001 From: Tarvo Reinpalu Date: Tue, 3 Nov 2020 12:41:57 +0200 Subject: [PATCH] Rework everything --- CHANGELOG.md | 7 ++++ ...nsLoader.php => LoadsNovaTranslations.php} | 41 +++++++------------ 2 files changed, 22 insertions(+), 26 deletions(-) rename src/{NovaTranslationsLoader.php => LoadsNovaTranslations.php} (55%) diff --git a/CHANGELOG.md b/CHANGELOG.md index a12ba16..ccfb144 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/NovaTranslationsLoader.php b/src/LoadsNovaTranslations.php similarity index 55% rename from src/NovaTranslationsLoader.php rename to src/LoadsNovaTranslations.php index 8bb87b6..a231736 100644 --- a/src/NovaTranslationsLoader.php +++ b/src/LoadsNovaTranslations.php @@ -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. * @@ -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";