【问题标题】:Laravel carbon date trasnlationLaravel 碳日期翻译
【发布时间】:2021-04-03 01:53:06
【问题描述】:

大家好,我需要一些关于翻译碳值日期、时间等方面的帮助......,

例如使用{{ date('M d, Y',strtotime($something->created at)) }} 那么它将显示为-> Apr 03, 2021

我的问题是我们如何将“Apr”翻译成其他语言,在过去 3 天里,我在这个案例上花了超过 3 天的时间寻找可能的方法,但没有运气:(,如果你能帮助我和其他需要这个解决方案的人

我也尝试过不止一种方法,比如在providers.php上写这个

$app = $this->app && method_exists($this->app, 'getLocale') ? $this->app : app('translator');
        $locale = $app->getLocale();
        Carbon::setLocale($locale);
        CarbonImmutable::setLocale($locale);
        CarbonPeriod::setLocale($locale);
        CarbonInterval::setLocale($locale);

【问题讨论】:

  • 你不能使用 date() 来翻译,你应该用 Carbon 对象格式化它,例如$something->created_at->locale('ab_cd')->format('dddd MMMM YYYY')
  • 试试now()->locale($locale)->translatedFormat('M d, Y');carbon.nesbot.com/docs/#api-localization

标签: laravel


【解决方案1】:

您是否尝试过使用 formatLocalized 方法?谷歌一下

【讨论】:

    【解决方案2】:

    在 Carbon 中设置本地化格式之前,您需要使用 setlocale 函数。您的代码中的问题是,您设置语言环境的方式是无状态的。它没有被保存。

    setlocale(LC_TIME, 'German');                     
    echo $date->formatLocalized('%A %d %B %Y');
    

    https://stackoverflow.com/a/20058254/8607640

    另一种解决方案是在会话中保存语言环境,从而为会话保存。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-04
      • 2017-08-25
      • 2018-03-16
      • 2016-08-09
      • 2017-11-28
      • 2017-09-06
      • 2016-12-13
      • 2016-02-07
      相关资源
      最近更新 更多