Carbon的formatLocalized在生产服务器上不起作用。

huangapple go评论47阅读模式
英文:

Carbon's formatLocalized not working on production server

问题

以下是您要翻译的内容:

The following function present in app/Helpers/helpers.php returns me the short month on my local machine. However, I deployed it to the server and it is always returning the month in English.

function dateFormatShortDateLang($date) {
    setlocale(LC_TIME, app()->getLocale());
    \Carbon\Carbon::setLocale(app()->getLocale());
    $shortMonth = ucfirst(str_replace('.', ',', \Carbon\Carbon::createFromFormat('d/m/Y', $date)->formatLocalized('%b %G')));

    return $shortMonth;
}

If I dd() the value of app()->getLocale() I get the correct current locale.
The whole repo has the same code, same PHP version and I run the following commands:

php artisan config:clear
composer dump autoload

My config/app.php:

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Application Name
    |--------------------------------------------------------------------------
    |
    | This value is the name of your application. This value is used when the
    | framework needs to place the application's name in a notification or
    | any other location as required by the application or its packages.
    |
    */

    'name' => env('APP_NAME', 'Laravel'),

    /*
    |--------------------------------------------------------------------------
    | Application Environment
    |--------------------------------------------------------------------------
    |
    | This value determines the "environment" your application is currently
    | running in. This may determine how you prefer to configure various
    | services your application utilizes. Set this in your ".env" file.
    |
    */

    'env' => env('APP_ENV', 'production'),

    /*
    |--------------------------------------------------------------------------
    | Application Debug Mode
    |--------------------------------------------------------------------------
    |
    | When your application is in debug mode, detailed error messages with
    | stack traces will be shown on every error that occurs within your
    | application. If disabled, a simple generic error page is shown.
    |
    */

    'debug' => env('APP_DEBUG', false),

    /*
    |--------------------------------------------------------------------------
    | Application URL
    |--------------------------------------------------------------------------
    |
    | This URL is used by the console to properly generate URLs when using
    | the Artisan command line tool. You should set this to the root of
    | your application so that it is used when running Artisan tasks.
    |
    */

    'url' => env('APP_URL', 'http://localhost'),

    /*
    |--------------------------------------------------------------------------
    | Application Timezone
    |--------------------------------------------------------------------------
    |
    | Here you may specify the default timezone for your application, which
    | will be used by the PHP date and date-time functions. We have gone
    | ahead and set this to a sensible default for you out of the box.
    |
    */

    'timezone' => 'UTC',

    /*
    |--------------------------------------------------------------------------
    | Application Locale Configuration
    |--------------------------------------------------------------------------
    |
    | The application locale determines the default locale that will be used
    | by the translation service provider. You are free to set this value
    | to any of the locales which will be supported by the application.
    |
    */

    'locale' => 'es',

    /*
    |--------------------------------------------------------------------------
    | Application Fallback Locale
    |--------------------------------------------------------------------------
    |
    | The fallback locale determines the locale to use when the current one
    | is not available. You may change the value to correspond to any of
    | the language folders that are provided through your application.
    |
    */

    'fallback_locale' => 'es',

    /*
    |--------------------------------------------------------------------------
    | Encryption Key
    |--------------------------------------------------------------------------
    |
    | This key is used by the Illuminate encrypter service and should be set
    | to a random, 32 character string, otherwise these encrypted strings
    | will not be safe. Please do this before deploying an application!
    |
    */

    'key' => env('APP_KEY'),

    'cipher' => 'AES-256-CBC',

    /*
    |--------------------------------------------------------------------------
    | Logging Configuration
    |--------------------------------------------------------------------------
    |
    | Here you may configure the log settings for your application. Out of
    | the box, Laravel uses the Monolog PHP logging library. This gives
    | you a variety of powerful log handlers / formatters to utilize.
    |
    | Available Settings: "single", "daily", "syslog", "errorlog"
    |
    */

    'log' => env('APP_LOG', 'single'),

    'log_level' => env('APP_LOG_LEVEL', 'debug'),

    /*
    |--------------------------------------------------------------------------
    | Autoloaded Service Providers
    |--------------------------------------------------------------------------
    |
    | The service providers listed here will be automatically loaded on the
    | request to your application. Feel free to add your own services to
    | this array to grant expanded functionality to your applications.
    |
    */

    'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        Illuminate\Auth\AuthServiceProvider::class,
        Illuminate\Broadcasting\BroadcastServiceProvider::class,
        Illuminate\Bus\BusServiceProvider::class,
        Illuminate\Cache\CacheServiceProvider::class,
        Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
        Illuminate\Cookie\CookieServiceProvider::class,
        Illuminate\Database\DatabaseServiceProvider::class,
        Illuminate\Encryption\EncryptionServiceProvider::class,
        Illuminate\Filesystem\FilesystemServiceProvider::class,
        Illuminate\Foundation\Providers\FoundationServiceProvider::class,
        Illuminate\Hashing\HashServiceProvider::class,
        Illuminate\Mail\MailServiceProvider::class,
        Illuminate\Notifications\NotificationServiceProvider::class,
        Illuminate\Pagination\PaginationServiceProvider::class,
        Illuminate\Pipeline\PipelineServiceProvider::class,
        Illuminate\Queue\QueueServiceProvider::class,
        Illuminate\Redis\RedisServiceProvider::class,
        Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
        Illuminate\Session\SessionServiceProvider::class,
        // Illuminate\Translation\TranslationServiceProvider::class,
        Spatie\TranslationLoader\TranslationServiceProvider::class,
        Illuminate\Validation\ValidationServiceProvider::class,
        Illuminate\View\ViewServiceProvider::class,

        /*
         * Package Service Providers...
         */

        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        // App\Providers\BroadcastServiceProvider::class,
        App\Providers\EventServiceProvider::class,

        // Multilingual
        App\Providers\RouteService-provider::class,
        App\Providers\ViewComposerServiceProvider::class,

    ],

    /*
    |--------------------------------------------------------------------------
    | Class Aliases
    |--------------------------------------------------------------------------
    |
    | This array of class aliases will be registered when this application
    | is started. However, feel free to register as many as you wish as
    | the aliases are "lazy" loaded so they don't hinder performance.
    |
    */

    'aliases' => [

        'App' => Illuminate\Support\Facades\App::class,
        'Artisan' => Illuminate\Support\Facades\Artisan::class,
        'Auth' => Illuminate\Support\Facades\Auth::class,
        'Blade' => Illuminate\Support\Facades\Blade::class,
        'Broadcast' => Illuminate\Support\Facades\Broadcast::class,
        'Bus' => Illuminate\Support\Facades\Bus::class,
        'Cache' => Illuminate\Support\Facades\Cache::class,
        'Config' => Illuminate\Support\Facades\Config::class,
        'Cookie' => Illuminate\Support\Facades\

<details>
<summary>英文:</summary>

The following function present in app/Helpers/helpers.php returns me the short month on my local machine. However, I deployed it to the server and it is always returning the month in English.

function dateFormatShortDateLang($date) {
setlocale(LC_TIME, app()->getLocale());
\Carbon\Carbon::setLocale(app()->getLocale());
$shortMonth = ucfirst(str_replace('.', ',', \Carbon\Carbon::createFromFormat('d/m/Y', $date)->formatLocalized('%b %G')));

return $shortMonth;

}


If I dd() the value of app()-&gt;getLocale() I get the correct current locale. 
The whole repo has the same code, same PHP version and I run the following commands:

php artisan cache:clear
php artisan config:clear
composer dump autoload


My config/app.php:

<?php

return [

/*
|--------------------------------------------------------------------------
| Application Name
|--------------------------------------------------------------------------
|
| This value is the name of your application. This value is used when the
| framework needs to place the application&#39;s name in a notification or
| any other location as required by the application or its packages.
|
*/
&#39;name&#39; =&gt; env(&#39;APP_NAME&#39;, &#39;Laravel&#39;),
/*
|--------------------------------------------------------------------------
| Application Environment
|--------------------------------------------------------------------------
|
| This value determines the &quot;environment&quot; your application is currently
| running in. This may determine how you prefer to configure various
| services your application utilizes. Set this in your &quot;.env&quot; file.
|
*/
&#39;env&#39; =&gt; env(&#39;APP_ENV&#39;, &#39;production&#39;),
/*
|--------------------------------------------------------------------------
| Application Debug Mode
|--------------------------------------------------------------------------
|
| When your application is in debug mode, detailed error messages with
| stack traces will be shown on every error that occurs within your
| application. If disabled, a simple generic error page is shown.
|
*/
&#39;debug&#39; =&gt; env(&#39;APP_DEBUG&#39;, false),
/*
|--------------------------------------------------------------------------
| Application URL
|--------------------------------------------------------------------------
|
| This URL is used by the console to properly generate URLs when using
| the Artisan command line tool. You should set this to the root of
| your application so that it is used when running Artisan tasks.
|
*/
&#39;url&#39; =&gt; env(&#39;APP_URL&#39;, &#39;http://localhost&#39;),
/*
|--------------------------------------------------------------------------
| Application Timezone
|--------------------------------------------------------------------------
|
| Here you may specify the default timezone for your application, which
| will be used by the PHP date and date-time functions. We have gone
| ahead and set this to a sensible default for you out of the box.
|
*/
&#39;timezone&#39; =&gt; &#39;UTC&#39;,
/*
|--------------------------------------------------------------------------
| Application Locale Configuration
|--------------------------------------------------------------------------
|
| The application locale determines the default locale that will be used
| by the translation service provider. You are free to set this value
| to any of the locales which will be supported by the application.
|
*/
&#39;locale&#39; =&gt; &#39;es&#39;,
/*
|--------------------------------------------------------------------------
| Application Fallback Locale
|--------------------------------------------------------------------------
|
| The fallback locale determines the locale to use when the current one
| is not available. You may change the value to correspond to any of
| the language folders that are provided through your application.
|
*/
&#39;fallback_locale&#39; =&gt; &#39;es&#39;,
/*
|--------------------------------------------------------------------------
| Encryption Key
|--------------------------------------------------------------------------
|
| This key is used by the Illuminate encrypter service and should be set
| to a random, 32 character string, otherwise these encrypted strings
| will not be safe. Please do this before deploying an application!
|
*/
&#39;key&#39; =&gt; env(&#39;APP_KEY&#39;),
&#39;cipher&#39; =&gt; &#39;AES-256-CBC&#39;,
/*
|--------------------------------------------------------------------------
| Logging Configuration
|--------------------------------------------------------------------------
|
| Here you may configure the log settings for your application. Out of
| the box, Laravel uses the Monolog PHP logging library. This gives
| you a variety of powerful log handlers / formatters to utilize.
|
| Available Settings: &quot;single&quot;, &quot;daily&quot;, &quot;syslog&quot;, &quot;errorlog&quot;
|
*/
&#39;log&#39; =&gt; env(&#39;APP_LOG&#39;, &#39;single&#39;),
&#39;log_level&#39; =&gt; env(&#39;APP_LOG_LEVEL&#39;, &#39;debug&#39;),
/*
|--------------------------------------------------------------------------
| Autoloaded Service Providers
|--------------------------------------------------------------------------
|
| The service providers listed here will be automatically loaded on the
| request to your application. Feel free to add your own services to
| this array to grant expanded functionality to your applications.
|
*/
&#39;providers&#39; =&gt; [
/*
* Laravel Framework Service Providers...
*/
Illuminate\Auth\AuthServiceProvider::class,
Illuminate\Broadcasting\BroadcastServiceProvider::class,
Illuminate\Bus\BusServiceProvider::class,
Illuminate\Cache\CacheServiceProvider::class,
Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
Illuminate\Cookie\CookieServiceProvider::class,
Illuminate\Database\DatabaseServiceProvider::class,
Illuminate\Encryption\EncryptionServiceProvider::class,
Illuminate\Filesystem\FilesystemServiceProvider::class,
Illuminate\Foundation\Providers\FoundationServiceProvider::class,
Illuminate\Hashing\HashServiceProvider::class,
Illuminate\Mail\MailServiceProvider::class,
Illuminate\Notifications\NotificationServiceProvider::class,
Illuminate\Pagination\PaginationServiceProvider::class,
Illuminate\Pipeline\PipelineServiceProvider::class,
Illuminate\Queue\QueueServiceProvider::class,
Illuminate\Redis\RedisServiceProvider::class,
Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
Illuminate\Session\SessionServiceProvider::class,
// Illuminate\Translation\TranslationServiceProvider::class,
Spatie\TranslationLoader\TranslationServiceProvider::class,
Illuminate\Validation\ValidationServiceProvider::class,
Illuminate\View\ViewServiceProvider::class,
/*
* Package Service Providers...
*/
/*
* Application Service Providers...
*/
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
// Multilingual
App\Providers\RouteServiceProvider::class,
App\Providers\ViewComposerServiceProvider::class,
],
/*
|--------------------------------------------------------------------------
| Class Aliases
|--------------------------------------------------------------------------
|
| This array of class aliases will be registered when this application
| is started. However, feel free to register as many as you wish as
| the aliases are &quot;lazy&quot; loaded so they don&#39;t hinder performance.
|
*/
&#39;aliases&#39; =&gt; [
&#39;App&#39; =&gt; Illuminate\Support\Facades\App::class,
&#39;Artisan&#39; =&gt; Illuminate\Support\Facades\Artisan::class,
&#39;Auth&#39; =&gt; Illuminate\Support\Facades\Auth::class,
&#39;Blade&#39; =&gt; Illuminate\Support\Facades\Blade::class,
&#39;Broadcast&#39; =&gt; Illuminate\Support\Facades\Broadcast::class,
&#39;Bus&#39; =&gt; Illuminate\Support\Facades\Bus::class,
&#39;Cache&#39; =&gt; Illuminate\Support\Facades\Cache::class,
&#39;Config&#39; =&gt; Illuminate\Support\Facades\Config::class,
&#39;Cookie&#39; =&gt; Illuminate\Support\Facades\Cookie::class,
&#39;Crypt&#39; =&gt; Illuminate\Support\Facades\Crypt::class,
&#39;DB&#39; =&gt; Illuminate\Support\Facades\DB::class,
&#39;Eloquent&#39; =&gt; Illuminate\Database\Eloquent\Model::class,
&#39;Event&#39; =&gt; Illuminate\Support\Facades\Event::class,
&#39;File&#39; =&gt; Illuminate\Support\Facades\File::class,
&#39;Gate&#39; =&gt; Illuminate\Support\Facades\Gate::class,
&#39;Hash&#39; =&gt; Illuminate\Support\Facades\Hash::class,
&#39;Lang&#39; =&gt; Illuminate\Support\Facades\Lang::class,
&#39;Log&#39; =&gt; Illuminate\Support\Facades\Log::class,
&#39;Mail&#39; =&gt; Illuminate\Support\Facades\Mail::class,
&#39;Notification&#39; =&gt; Illuminate\Support\Facades\Notification::class,
&#39;Password&#39; =&gt; Illuminate\Support\Facades\Password::class,
&#39;Queue&#39; =&gt; Illuminate\Support\Facades\Queue::class,
&#39;Redirect&#39; =&gt; Illuminate\Support\Facades\Redirect::class,
&#39;Redis&#39; =&gt; Illuminate\Support\Facades\Redis::class,
&#39;Request&#39; =&gt; Illuminate\Support\Facades\Request::class,
&#39;Response&#39; =&gt; Illuminate\Support\Facades\Response::class,
&#39;Route&#39; =&gt; Illuminate\Support\Facades\Route::class,
&#39;Schema&#39; =&gt; Illuminate\Support\Facades\Schema::class,
&#39;Session&#39; =&gt; Illuminate\Support\Facades\Session::class,
&#39;Storage&#39; =&gt; Illuminate\Support\Facades\Storage::class,
&#39;URL&#39; =&gt; Illuminate\Support\Facades\URL::class,
&#39;Validator&#39; =&gt; Illuminate\Support\Facades\Validator::class,
&#39;View&#39; =&gt; Illuminate\Support\Facades\View::class,
],

];


</details>
# 答案1
**得分**: 2
`formatLocalized`由于以下原因已被弃用:它使用了依赖于计算机上已安装的语言环境的`strftime`,并且将在未来的PHP版本中删除。请参阅`isoFormat`和[本地化文档](https://carbon.nesbot.com/docs/#api-localization)
```php
function dateFormatShortDateLang($date) {
Carbon::setLocale(app()->getLocale());
return Carbon::createFromFormat('d/m/Y', $date)->isoFormat('MMM YYYY');
}
也,如果您启用了Laravel提供程序自动发现或加载了`Carbon\Laravel\ServiceProvider`,`Carbon::setLocale(app()->getLocale());` 不应该是必需的,这正是为了在您的Laravel应用程序区域设置上同步Carbon区域设置的。
英文:

formatLocalized is deprecated for this reason: it uses strftime which use locales installed on the machine and will also be removed in future PHP version. See isoFormat and Localization documentation

function dateFormatShortDateLang($date) {
Carbon::setLocale(app()-&gt;getLocale());
return Carbon::createFromFormat(&#39;d/m/Y&#39;, $date)-&gt;isoFormat(&#39;MMM YYYY&#39;);
}

Also Carbon::setLocale(app()-&gt;getLocale()); shouldn't be necessary if you enabled laravel provider auto-discovery or load Carbon\Laravel\ServiceProvider which is exactly meant to synchronize Carbon locale on your Laravel app locale.

答案2

得分: 0

这对我有用

{{ Carbon\Carbon::parse($datetime)->locale(app()->getLocale())->isoFormat('D MMMM Y H:mm') }}

英文:

This worked for me

{{ Carbon\Carbon::parse($datetime)-&gt;locale(app()-&gt;getLocale())-&gt;isoFormat(&#39;D MMMM Y H:mm&#39;) }}

huangapple
  • 本文由 发表于 2023年2月6日 19:37:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360813.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定