Giter Club home page Giter Club logo

Comments (11)

Amegatron avatar Amegatron commented on August 25, 2024

Thats strange, I just tested it again, and just %f works fine for me.

Route::get('/test', function() {
    $date = \Laravelrus\LocalizedCarbon\LocalizedCarbon::create(2014, 6, 12);
    return $date->formatLocalized('%f');
});

Output is июня.

from localized-carbon.

vladshcherbin avatar vladshcherbin commented on August 25, 2024

@Amegatron probably, you are using windows. check the Carbon format method, there is a preg_replace function there for win users.

After this method, not on windows, I just get the f letter, your example also shows the f letter.

Latest version of 4.2 laravel, latest version of this package, mac os.

from localized-carbon.

Amegatron avatar Amegatron commented on August 25, 2024

I'm using homestead - so this is not under Windows. I checked the method used to determine Windows in original formatLocalized:

strtoupper(substr(PHP_OS, 0, 3))

It returns LIN.

I'm also having latest versions of Laravel and Carbon.

I suppose the issue occurs only under Mac. I'll think about better solution of it, but I need someone else to approve it on Mac.

from localized-carbon.

vladshcherbin avatar vladshcherbin commented on August 25, 2024

@Amegatron yes, that's strange

the native Carbon formatLocalized() function returns

return strftime($format, $this->timestamp);

With %f it returns just the f letter. Idk why this differs.
I guess, that function works not the same on different php/os versions.

My php version is 5.5.10

from localized-carbon.

vladshcherbin avatar vladshcherbin commented on August 25, 2024

@Amegatron ok, I tested it. In fact, there is no %f key in strftime function.

I suggest using the standard %B key for that.

I also tested the solution:

public function formatLocalized($format = self::COOKIE)
{
    $result = parent::formatLocalized($format);

    if (strpos($format, '%B') !== false)
    {
        $month = parent::format("F");
        $localizedMonth = \Lang::get("localized-carbon::months." . strtolower($month));
        $result = str_replace($month, $localizedMonth, $result);
    }

    return $result;
}

or even a shorter version

public function formatLocalized($format = self::COOKIE)
{
    if (strpos($format, '%B') !== false)
    {
        $month = strtolower(parent::format("F"));
        $localizedMonth = \Lang::get("localized-carbon::months." . $month);
        $format = str_replace('%B', $localizedMonth, $format);
    }

    return parent::formatLocalized($format);
}

Also, consider changing lang files from январь to Январь - that is the default behavior, not the lowercase one.

Hope, this will help. Thanks for the package ;)

from localized-carbon.

Amegatron avatar Amegatron commented on August 25, 2024

That is the case, %f is specially the key that is not used by default strftime to not conflict with default behaviour. And also thats why I don't want to replace %B because what if someone wants the default behaviour? I think that would not be a good solution.
Considering strftime, I also now think that the reason is the difference between implementations of this function under different systems.

from localized-carbon.

Amegatron avatar Amegatron commented on August 25, 2024

@vladshcherbin Please check out my possible fix: e00eb79.
I first replace %f and only then pass the string to parent::formatLocalized.

from localized-carbon.

vladshcherbin avatar vladshcherbin commented on August 25, 2024

@Amegatron yes, that is definitely working.

you can shorten it by

public function formatLocalized($format = self::COOKIE)
{
    if (strpos($format, '%f') !== false) {
        $month = strtolower(parent::format("F"));
        $localizedMonth = \Lang::get("localized-carbon::months." . $month);
        $format = str_replace('%f', $localizedMonth, $format);
    }

    return parent::formatLocalized($format);
}

All in all, please consider changing to the %B key & first letter uppercase, this is the expected behavior, there is no point in reinventing the wheel.

And if someone would want the default behaviour, the standard Carbon class should be used instead.

Thanks ;)

from localized-carbon.

Amegatron avatar Amegatron commented on August 25, 2024

I'll think about switching to %B, but I'm not going to make first letter uppercase for Russian localization. First uppercase character is the standard in USA and England and maybe somewhere else, but not in Russia, we have lower-case characters, we do not write "16 Января 2014", but "16 января 2014". Even standart %B behaviour uses lower-cased months.

Anyway, thanks for pointing out the issue about %%f under Mac :)

from localized-carbon.

Amegatron avatar Amegatron commented on August 25, 2024

@vladshcherbin sorry, I meant %b - abbreviated month, wich is in genitive for russian, e.g. "мая".

from localized-carbon.

vladshcherbin avatar vladshcherbin commented on August 25, 2024

@Amegatron yes, i checked the default %B in russian, the result is lowercased.

So, if the %f will be switched to the default %B it would be super cool. Thanks.

from localized-carbon.

Related Issues (19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.