Apress / pivotal-certified-pro-spring-dev-exam-02

Source Code for 'Pivotal Certified Professional Core Spring 5 Developer Exam' by Iuliana Cosmina
Other
170 stars 240 forks source link

Chapter 06 and 07: failing to change locales #19

Closed urs-beeli closed 3 years ago

urs-beeli commented 3 years ago

I've been working through chapters 06 and 07. While the changing of themes works perfectly, the changing of languages/locales does not work. The only thing that changes is the "Englisch" and "Deutsch" indicators, but every text is shown in German only (my machine uses "de_CH" as default locale).

Sifting through the logs of chapter07/boot-sec I found the following log message: o.s.c.s.ReloadableResourceBundleMessageSource - No properties file found for [classpath:i18n/global_en] - neither plain properties nor XML

It seems that the "global.properties" file is not picked up for lang="en" and instead global_de.properties is always used.

Renaming the file to global_en.properties and the switching back and forth of languages works.

iuliana commented 3 years ago

Hello urs-beeli,

The Chapter06/boot-web uses a Cookie resolver(actually all of them probably are) that has set Locale.English as default. I've looked into the Firefox inspector and the cookie changes value and the labels change accordingly. The global.properties is the default locale file. If a locale that is not supported is requested using a request to http://0.0.0.0:8081/?lang=ch for example, the ReloadableResourceBundleMessageSource defaults to the system locale. My system locale is en_UK, there is no "global_en.properties", so takes the default file.

On your machine, the locale is "de_CH" and thus looks for a "global_de.properties". And that file is found. That is why you always get German, but never English - your default is German. English is not found, so you get the default.

I will rename all global.properties to global_en.properties to avoid such problems in the future.

I pushed all fixes to the master branch.

Good catch! Thank you for this!