Skip to content

Commit 4361b2a

Browse files
committed
Fix Laravel 9+ language directory structure
- Move language files from /src/resources/lang/ to /src/lang/ - Update ServiceProvider to support both Laravel 9+ and legacy structures - Add backward compatibility for older Laravel versions - Update publish commands to support both directory structures - Resolves GitHub issue #151 This ensures compatibility with Laravel 9+ where the default language directory has moved from /resources/lang to /lang, while maintaining backward compatibility with older Laravel versions.
1 parent 90553f9 commit 4361b2a

File tree

7 files changed

+97
-4
lines changed

7 files changed

+97
-4
lines changed

LARAVEL9_LANG_FIX.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Laravel 9+ Language Directory Fix
2+
3+
## Problem
4+
In Laravel 9+, the default language directory has moved from `/resources/lang` to `/lang`. The Laravel Logger package was still using the old structure, which could cause confusion and conflicts.
5+
6+
## Solution
7+
Updated the package to support both Laravel 9+ and older versions:
8+
9+
### Changes Made:
10+
11+
1. **Moved language files** from `/src/resources/lang/` to `/src/lang/`
12+
2. **Updated ServiceProvider** to detect and use the appropriate structure
13+
3. **Added backward compatibility** for older Laravel versions
14+
4. **Updated publish commands** to support both structures
15+
16+
### ServiceProvider Updates:
17+
18+
```php
19+
// Load translations from new Laravel 9+ structure if available, fallback to old structure
20+
if (is_dir(__DIR__.'/lang/')) {
21+
$this->loadTranslationsFrom(__DIR__.'/lang/', 'LaravelLogger');
22+
} else {
23+
$this->loadTranslationsFrom(__DIR__.'/resources/lang/', 'LaravelLogger');
24+
}
25+
```
26+
27+
### Publishing Updates:
28+
29+
```php
30+
// Publish language files to Laravel 9+ structure if available, fallback to old structure
31+
if (is_dir(__DIR__.'/lang/')) {
32+
// Laravel 9+ structure
33+
$this->publishes([
34+
__DIR__.'/lang' => base_path('lang/vendor/'.$publishTag),
35+
], $publishTag);
36+
37+
// Also publish to old structure for backward compatibility
38+
$this->publishes([
39+
__DIR__.'/lang' => base_path('resources/lang/vendor/'.$publishTag),
40+
], $publishTag.'-legacy');
41+
} else {
42+
// Old structure fallback
43+
$this->publishes([
44+
__DIR__.'/resources/lang' => base_path('resources/lang/vendor/'.$publishTag),
45+
], $publishTag);
46+
}
47+
```
48+
49+
## Benefits:
50+
51+
-**Laravel 9+ Compatible** - Uses new `/lang` directory structure
52+
-**Backward Compatible** - Still works with older Laravel versions
53+
-**No Breaking Changes** - Existing installations continue to work
54+
-**Future Proof** - Ready for Laravel 10+ and beyond
55+
56+
## Usage:
57+
58+
### Laravel 9+ Projects:
59+
```bash
60+
php artisan vendor:publish --tag=LaravelLogger
61+
# Language files will be published to /lang/vendor/LaravelLogger/
62+
```
63+
64+
### Legacy Laravel Projects:
65+
```bash
66+
php artisan vendor:publish --tag=LaravelLogger-legacy
67+
# Language files will be published to /resources/lang/vendor/LaravelLogger/
68+
```
69+
70+
## References:
71+
- [Laravel 9 Language Directory Change](https://laravel.com/docs/9.x/upgrade#language-directory)
72+
- [GitHub Issue #151](https://github.com/jeremykenedy/laravel-logger/issues/151)
73+
- [Stack Overflow Discussion](https://stackoverflow.com/questions/71084830/laravel-9-app-upgraded-from-8-lang-directory-not-working-as-expected)

src/LaravelLoggerServiceProvider.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,13 @@ class LaravelLoggerServiceProvider extends ServiceProvider
5454
public function boot(): void
5555
{
5656
$this->app['router']->middlewareGroup('activity', [LogActivity::class]);
57-
$this->loadTranslationsFrom(__DIR__.'/resources/lang/', 'LaravelLogger');
57+
58+
// Load translations from new Laravel 9+ structure if available, fallback to old structure
59+
if (is_dir(__DIR__.'/lang/')) {
60+
$this->loadTranslationsFrom(__DIR__.'/lang/', 'LaravelLogger');
61+
} else {
62+
$this->loadTranslationsFrom(__DIR__.'/resources/lang/', 'LaravelLogger');
63+
}
5864
}
5965

6066
/**
@@ -117,8 +123,22 @@ private function publishFiles(): void
117123
__DIR__.'/resources/views' => base_path('resources/views/vendor/'.$publishTag),
118124
], $publishTag);
119125

120-
$this->publishes([
121-
__DIR__.'/resources/lang' => base_path('resources/lang/vendor/'.$publishTag),
122-
], $publishTag);
126+
// Publish language files to Laravel 9+ structure if available, fallback to old structure
127+
if (is_dir(__DIR__.'/lang/')) {
128+
// Laravel 9+ structure
129+
$this->publishes([
130+
__DIR__.'/lang' => base_path('lang/vendor/'.$publishTag),
131+
], $publishTag);
132+
133+
// Also publish to old structure for backward compatibility
134+
$this->publishes([
135+
__DIR__.'/lang' => base_path('resources/lang/vendor/'.$publishTag),
136+
], $publishTag.'-legacy');
137+
} else {
138+
// Old structure fallback
139+
$this->publishes([
140+
__DIR__.'/resources/lang' => base_path('resources/lang/vendor/'.$publishTag),
141+
], $publishTag);
142+
}
123143
}
124144
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)