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

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

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.

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

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:

  1. php artisan config:clear
  2. composer dump autoload

My config/app.php:

  1. <?php
  2. return [
  3. /*
  4. |--------------------------------------------------------------------------
  5. | Application Name
  6. |--------------------------------------------------------------------------
  7. |
  8. | This value is the name of your application. This value is used when the
  9. | framework needs to place the application's name in a notification or
  10. | any other location as required by the application or its packages.
  11. |
  12. */
  13. 'name' => env('APP_NAME', 'Laravel'),
  14. /*
  15. |--------------------------------------------------------------------------
  16. | Application Environment
  17. |--------------------------------------------------------------------------
  18. |
  19. | This value determines the "environment" your application is currently
  20. | running in. This may determine how you prefer to configure various
  21. | services your application utilizes. Set this in your ".env" file.
  22. |
  23. */
  24. 'env' => env('APP_ENV', 'production'),
  25. /*
  26. |--------------------------------------------------------------------------
  27. | Application Debug Mode
  28. |--------------------------------------------------------------------------
  29. |
  30. | When your application is in debug mode, detailed error messages with
  31. | stack traces will be shown on every error that occurs within your
  32. | application. If disabled, a simple generic error page is shown.
  33. |
  34. */
  35. 'debug' => env('APP_DEBUG', false),
  36. /*
  37. |--------------------------------------------------------------------------
  38. | Application URL
  39. |--------------------------------------------------------------------------
  40. |
  41. | This URL is used by the console to properly generate URLs when using
  42. | the Artisan command line tool. You should set this to the root of
  43. | your application so that it is used when running Artisan tasks.
  44. |
  45. */
  46. 'url' => env('APP_URL', 'http://localhost'),
  47. /*
  48. |--------------------------------------------------------------------------
  49. | Application Timezone
  50. |--------------------------------------------------------------------------
  51. |
  52. | Here you may specify the default timezone for your application, which
  53. | will be used by the PHP date and date-time functions. We have gone
  54. | ahead and set this to a sensible default for you out of the box.
  55. |
  56. */
  57. 'timezone' => 'UTC',
  58. /*
  59. |--------------------------------------------------------------------------
  60. | Application Locale Configuration
  61. |--------------------------------------------------------------------------
  62. |
  63. | The application locale determines the default locale that will be used
  64. | by the translation service provider. You are free to set this value
  65. | to any of the locales which will be supported by the application.
  66. |
  67. */
  68. 'locale' => 'es',
  69. /*
  70. |--------------------------------------------------------------------------
  71. | Application Fallback Locale
  72. |--------------------------------------------------------------------------
  73. |
  74. | The fallback locale determines the locale to use when the current one
  75. | is not available. You may change the value to correspond to any of
  76. | the language folders that are provided through your application.
  77. |
  78. */
  79. 'fallback_locale' => 'es',
  80. /*
  81. |--------------------------------------------------------------------------
  82. | Encryption Key
  83. |--------------------------------------------------------------------------
  84. |
  85. | This key is used by the Illuminate encrypter service and should be set
  86. | to a random, 32 character string, otherwise these encrypted strings
  87. | will not be safe. Please do this before deploying an application!
  88. |
  89. */
  90. 'key' => env('APP_KEY'),
  91. 'cipher' => 'AES-256-CBC',
  92. /*
  93. |--------------------------------------------------------------------------
  94. | Logging Configuration
  95. |--------------------------------------------------------------------------
  96. |
  97. | Here you may configure the log settings for your application. Out of
  98. | the box, Laravel uses the Monolog PHP logging library. This gives
  99. | you a variety of powerful log handlers / formatters to utilize.
  100. |
  101. | Available Settings: "single", "daily", "syslog", "errorlog"
  102. |
  103. */
  104. 'log' => env('APP_LOG', 'single'),
  105. 'log_level' => env('APP_LOG_LEVEL', 'debug'),
  106. /*
  107. |--------------------------------------------------------------------------
  108. | Autoloaded Service Providers
  109. |--------------------------------------------------------------------------
  110. |
  111. | The service providers listed here will be automatically loaded on the
  112. | request to your application. Feel free to add your own services to
  113. | this array to grant expanded functionality to your applications.
  114. |
  115. */
  116. 'providers' => [
  117. /*
  118. * Laravel Framework Service Providers...
  119. */
  120. Illuminate\Auth\AuthServiceProvider::class,
  121. Illuminate\Broadcasting\BroadcastServiceProvider::class,
  122. Illuminate\Bus\BusServiceProvider::class,
  123. Illuminate\Cache\CacheServiceProvider::class,
  124. Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
  125. Illuminate\Cookie\CookieServiceProvider::class,
  126. Illuminate\Database\DatabaseServiceProvider::class,
  127. Illuminate\Encryption\EncryptionServiceProvider::class,
  128. Illuminate\Filesystem\FilesystemServiceProvider::class,
  129. Illuminate\Foundation\Providers\FoundationServiceProvider::class,
  130. Illuminate\Hashing\HashServiceProvider::class,
  131. Illuminate\Mail\MailServiceProvider::class,
  132. Illuminate\Notifications\NotificationServiceProvider::class,
  133. Illuminate\Pagination\PaginationServiceProvider::class,
  134. Illuminate\Pipeline\PipelineServiceProvider::class,
  135. Illuminate\Queue\QueueServiceProvider::class,
  136. Illuminate\Redis\RedisServiceProvider::class,
  137. Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
  138. Illuminate\Session\SessionServiceProvider::class,
  139. // Illuminate\Translation\TranslationServiceProvider::class,
  140. Spatie\TranslationLoader\TranslationServiceProvider::class,
  141. Illuminate\Validation\ValidationServiceProvider::class,
  142. Illuminate\View\ViewServiceProvider::class,
  143. /*
  144. * Package Service Providers...
  145. */
  146. /*
  147. * Application Service Providers...
  148. */
  149. App\Providers\AppServiceProvider::class,
  150. App\Providers\AuthServiceProvider::class,
  151. // App\Providers\BroadcastServiceProvider::class,
  152. App\Providers\EventServiceProvider::class,
  153. // Multilingual
  154. App\Providers\RouteService-provider::class,
  155. App\Providers\ViewComposerServiceProvider::class,
  156. ],
  157. /*
  158. |--------------------------------------------------------------------------
  159. | Class Aliases
  160. |--------------------------------------------------------------------------
  161. |
  162. | This array of class aliases will be registered when this application
  163. | is started. However, feel free to register as many as you wish as
  164. | the aliases are "lazy" loaded so they don't hinder performance.
  165. |
  166. */
  167. 'aliases' => [
  168. 'App' => Illuminate\Support\Facades\App::class,
  169. 'Artisan' => Illuminate\Support\Facades\Artisan::class,
  170. 'Auth' => Illuminate\Support\Facades\Auth::class,
  171. 'Blade' => Illuminate\Support\Facades\Blade::class,
  172. 'Broadcast' => Illuminate\Support\Facades\Broadcast::class,
  173. 'Bus' => Illuminate\Support\Facades\Bus::class,
  174. 'Cache' => Illuminate\Support\Facades\Cache::class,
  175. 'Config' => Illuminate\Support\Facades\Config::class,
  176. 'Cookie' => Illuminate\Support\Facades\
  177. <details>
  178. <summary>英文:</summary>
  179. 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')));

  1. return $shortMonth;

}

  1. If I dd() the value of app()-&gt;getLocale() I get the correct current locale.
  2. 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

  1. My config/app.php:

<?php

return [

  1. /*
  2. |--------------------------------------------------------------------------
  3. | Application Name
  4. |--------------------------------------------------------------------------
  5. |
  6. | This value is the name of your application. This value is used when the
  7. | framework needs to place the application&#39;s name in a notification or
  8. | any other location as required by the application or its packages.
  9. |
  10. */
  11. &#39;name&#39; =&gt; env(&#39;APP_NAME&#39;, &#39;Laravel&#39;),
  12. /*
  13. |--------------------------------------------------------------------------
  14. | Application Environment
  15. |--------------------------------------------------------------------------
  16. |
  17. | This value determines the &quot;environment&quot; your application is currently
  18. | running in. This may determine how you prefer to configure various
  19. | services your application utilizes. Set this in your &quot;.env&quot; file.
  20. |
  21. */
  22. &#39;env&#39; =&gt; env(&#39;APP_ENV&#39;, &#39;production&#39;),
  23. /*
  24. |--------------------------------------------------------------------------
  25. | Application Debug Mode
  26. |--------------------------------------------------------------------------
  27. |
  28. | When your application is in debug mode, detailed error messages with
  29. | stack traces will be shown on every error that occurs within your
  30. | application. If disabled, a simple generic error page is shown.
  31. |
  32. */
  33. &#39;debug&#39; =&gt; env(&#39;APP_DEBUG&#39;, false),
  34. /*
  35. |--------------------------------------------------------------------------
  36. | Application URL
  37. |--------------------------------------------------------------------------
  38. |
  39. | This URL is used by the console to properly generate URLs when using
  40. | the Artisan command line tool. You should set this to the root of
  41. | your application so that it is used when running Artisan tasks.
  42. |
  43. */
  44. &#39;url&#39; =&gt; env(&#39;APP_URL&#39;, &#39;http://localhost&#39;),
  45. /*
  46. |--------------------------------------------------------------------------
  47. | Application Timezone
  48. |--------------------------------------------------------------------------
  49. |
  50. | Here you may specify the default timezone for your application, which
  51. | will be used by the PHP date and date-time functions. We have gone
  52. | ahead and set this to a sensible default for you out of the box.
  53. |
  54. */
  55. &#39;timezone&#39; =&gt; &#39;UTC&#39;,
  56. /*
  57. |--------------------------------------------------------------------------
  58. | Application Locale Configuration
  59. |--------------------------------------------------------------------------
  60. |
  61. | The application locale determines the default locale that will be used
  62. | by the translation service provider. You are free to set this value
  63. | to any of the locales which will be supported by the application.
  64. |
  65. */
  66. &#39;locale&#39; =&gt; &#39;es&#39;,
  67. /*
  68. |--------------------------------------------------------------------------
  69. | Application Fallback Locale
  70. |--------------------------------------------------------------------------
  71. |
  72. | The fallback locale determines the locale to use when the current one
  73. | is not available. You may change the value to correspond to any of
  74. | the language folders that are provided through your application.
  75. |
  76. */
  77. &#39;fallback_locale&#39; =&gt; &#39;es&#39;,
  78. /*
  79. |--------------------------------------------------------------------------
  80. | Encryption Key
  81. |--------------------------------------------------------------------------
  82. |
  83. | This key is used by the Illuminate encrypter service and should be set
  84. | to a random, 32 character string, otherwise these encrypted strings
  85. | will not be safe. Please do this before deploying an application!
  86. |
  87. */
  88. &#39;key&#39; =&gt; env(&#39;APP_KEY&#39;),
  89. &#39;cipher&#39; =&gt; &#39;AES-256-CBC&#39;,
  90. /*
  91. |--------------------------------------------------------------------------
  92. | Logging Configuration
  93. |--------------------------------------------------------------------------
  94. |
  95. | Here you may configure the log settings for your application. Out of
  96. | the box, Laravel uses the Monolog PHP logging library. This gives
  97. | you a variety of powerful log handlers / formatters to utilize.
  98. |
  99. | Available Settings: &quot;single&quot;, &quot;daily&quot;, &quot;syslog&quot;, &quot;errorlog&quot;
  100. |
  101. */
  102. &#39;log&#39; =&gt; env(&#39;APP_LOG&#39;, &#39;single&#39;),
  103. &#39;log_level&#39; =&gt; env(&#39;APP_LOG_LEVEL&#39;, &#39;debug&#39;),
  104. /*
  105. |--------------------------------------------------------------------------
  106. | Autoloaded Service Providers
  107. |--------------------------------------------------------------------------
  108. |
  109. | The service providers listed here will be automatically loaded on the
  110. | request to your application. Feel free to add your own services to
  111. | this array to grant expanded functionality to your applications.
  112. |
  113. */
  114. &#39;providers&#39; =&gt; [
  115. /*
  116. * Laravel Framework Service Providers...
  117. */
  118. Illuminate\Auth\AuthServiceProvider::class,
  119. Illuminate\Broadcasting\BroadcastServiceProvider::class,
  120. Illuminate\Bus\BusServiceProvider::class,
  121. Illuminate\Cache\CacheServiceProvider::class,
  122. Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
  123. Illuminate\Cookie\CookieServiceProvider::class,
  124. Illuminate\Database\DatabaseServiceProvider::class,
  125. Illuminate\Encryption\EncryptionServiceProvider::class,
  126. Illuminate\Filesystem\FilesystemServiceProvider::class,
  127. Illuminate\Foundation\Providers\FoundationServiceProvider::class,
  128. Illuminate\Hashing\HashServiceProvider::class,
  129. Illuminate\Mail\MailServiceProvider::class,
  130. Illuminate\Notifications\NotificationServiceProvider::class,
  131. Illuminate\Pagination\PaginationServiceProvider::class,
  132. Illuminate\Pipeline\PipelineServiceProvider::class,
  133. Illuminate\Queue\QueueServiceProvider::class,
  134. Illuminate\Redis\RedisServiceProvider::class,
  135. Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
  136. Illuminate\Session\SessionServiceProvider::class,
  137. // Illuminate\Translation\TranslationServiceProvider::class,
  138. Spatie\TranslationLoader\TranslationServiceProvider::class,
  139. Illuminate\Validation\ValidationServiceProvider::class,
  140. Illuminate\View\ViewServiceProvider::class,
  141. /*
  142. * Package Service Providers...
  143. */
  144. /*
  145. * Application Service Providers...
  146. */
  147. App\Providers\AppServiceProvider::class,
  148. App\Providers\AuthServiceProvider::class,
  149. // App\Providers\BroadcastServiceProvider::class,
  150. App\Providers\EventServiceProvider::class,
  151. // Multilingual
  152. App\Providers\RouteServiceProvider::class,
  153. App\Providers\ViewComposerServiceProvider::class,
  154. ],
  155. /*
  156. |--------------------------------------------------------------------------
  157. | Class Aliases
  158. |--------------------------------------------------------------------------
  159. |
  160. | This array of class aliases will be registered when this application
  161. | is started. However, feel free to register as many as you wish as
  162. | the aliases are &quot;lazy&quot; loaded so they don&#39;t hinder performance.
  163. |
  164. */
  165. &#39;aliases&#39; =&gt; [
  166. &#39;App&#39; =&gt; Illuminate\Support\Facades\App::class,
  167. &#39;Artisan&#39; =&gt; Illuminate\Support\Facades\Artisan::class,
  168. &#39;Auth&#39; =&gt; Illuminate\Support\Facades\Auth::class,
  169. &#39;Blade&#39; =&gt; Illuminate\Support\Facades\Blade::class,
  170. &#39;Broadcast&#39; =&gt; Illuminate\Support\Facades\Broadcast::class,
  171. &#39;Bus&#39; =&gt; Illuminate\Support\Facades\Bus::class,
  172. &#39;Cache&#39; =&gt; Illuminate\Support\Facades\Cache::class,
  173. &#39;Config&#39; =&gt; Illuminate\Support\Facades\Config::class,
  174. &#39;Cookie&#39; =&gt; Illuminate\Support\Facades\Cookie::class,
  175. &#39;Crypt&#39; =&gt; Illuminate\Support\Facades\Crypt::class,
  176. &#39;DB&#39; =&gt; Illuminate\Support\Facades\DB::class,
  177. &#39;Eloquent&#39; =&gt; Illuminate\Database\Eloquent\Model::class,
  178. &#39;Event&#39; =&gt; Illuminate\Support\Facades\Event::class,
  179. &#39;File&#39; =&gt; Illuminate\Support\Facades\File::class,
  180. &#39;Gate&#39; =&gt; Illuminate\Support\Facades\Gate::class,
  181. &#39;Hash&#39; =&gt; Illuminate\Support\Facades\Hash::class,
  182. &#39;Lang&#39; =&gt; Illuminate\Support\Facades\Lang::class,
  183. &#39;Log&#39; =&gt; Illuminate\Support\Facades\Log::class,
  184. &#39;Mail&#39; =&gt; Illuminate\Support\Facades\Mail::class,
  185. &#39;Notification&#39; =&gt; Illuminate\Support\Facades\Notification::class,
  186. &#39;Password&#39; =&gt; Illuminate\Support\Facades\Password::class,
  187. &#39;Queue&#39; =&gt; Illuminate\Support\Facades\Queue::class,
  188. &#39;Redirect&#39; =&gt; Illuminate\Support\Facades\Redirect::class,
  189. &#39;Redis&#39; =&gt; Illuminate\Support\Facades\Redis::class,
  190. &#39;Request&#39; =&gt; Illuminate\Support\Facades\Request::class,
  191. &#39;Response&#39; =&gt; Illuminate\Support\Facades\Response::class,
  192. &#39;Route&#39; =&gt; Illuminate\Support\Facades\Route::class,
  193. &#39;Schema&#39; =&gt; Illuminate\Support\Facades\Schema::class,
  194. &#39;Session&#39; =&gt; Illuminate\Support\Facades\Session::class,
  195. &#39;Storage&#39; =&gt; Illuminate\Support\Facades\Storage::class,
  196. &#39;URL&#39; =&gt; Illuminate\Support\Facades\URL::class,
  197. &#39;Validator&#39; =&gt; Illuminate\Support\Facades\Validator::class,
  198. &#39;View&#39; =&gt; Illuminate\Support\Facades\View::class,
  199. ],

];

  1. </details>
  2. # 答案1
  3. **得分**: 2
  4. `formatLocalized`由于以下原因已被弃用:它使用了依赖于计算机上已安装的语言环境的`strftime`,并且将在未来的PHP版本中删除。请参阅`isoFormat`和[本地化文档](https://carbon.nesbot.com/docs/#api-localization)
  5. ```php
  6. function dateFormatShortDateLang($date) {
  7. Carbon::setLocale(app()->getLocale());
  8. return Carbon::createFromFormat('d/m/Y', $date)->isoFormat('MMM YYYY');
  9. }
  10. 也,如果您启用了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

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

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

  1. {{ 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:

确定