Skip to content

I18n::setLocale doesn't behave the same as in 3.4 and overwrites default locale #11199

@liviakuenzli

Description

@liviakuenzli

This is a (multiple allowed):

  • bug (?)

  • CakePHP Version: 3.5.2

  • Platform and Target: -

What I did - What happened - And why it happened

I wrote some kind of I18n middleware for one of our projects and the tests I wrote for that middleware failed after I updated from CakePHP 3.4.x to 3.5.x (3.5.2 to be precise, but I don't think it matters). The tests fail because the I18n::locale() function behaves slightly different now.

The relevant changes can be seen in this commit:
de23e33#diff-f93c69a473b7c4e871355ef04e668140L214

In the "old" version (3.4), when calling I18n::locale() we first get the default locale, before doing anything else, setting the variable $_defaultLocale to whatever the default was (en_US in my case because I didn't set any other default). Then we can do whatever we want (e.g. set a new locale) but $_defaultLocale will always be the very first locale (en_US in my case) and getDefaultLocale() will always return exactly that. That is also what the documentation of getDefaultLocale() says: "This returns the default locale before any modifications" https://github.com/cakephp/cakephp/blob/master/src/I18n/I18n.php#L332

But in 3.5, we don't call getDefaultLocale() (or defaultLocale()) anymore when setting a new locale (it doesn't matter if you call setLocale() directly or if you call locale()), meaning the 'default locale' gets lost. Since setLocale() overwrites the default locale on "php level" in intl https://github.com/cakephp/cakephp/blob/master/src/I18n/I18n.php#L292 and getDefaultLocale() reads the default locale from intl https://github.com/cakephp/cakephp/blob/master/src/I18n/I18n.php#L341, when we first use getDefaultLocale(), getLocale() or locale() without parameter, the value that, at this point, has last been set will be set as default locale and it will stay that value from then on.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions