springspring-boot

Spring boot 3 Internationalization with variants


I would like to have internationalization for a given language with different variants. My case is to display for the same language, different messages according to if the user is a teacher or a student. So I have tried messages files like :

messages_fr_FR_ST.properties
messages_fr_FR_TE.properties
messages_fr_FR_st.properties
messages_fr_FR_te.properties
messages_fr_FR_student.properties
messages_fr_FR_teacher.properties
messages_fr__st.properties
messages_fr__te.properties

but they seem not detected, while messages_fr.properties is.

I made the locale like that :

Locale(Locale.FRANCE.language, Locale.FRANCE.country, "st")
Locale(Locale.FRANCE.language, Locale.FRANCE.country, "ST")
Locale(Locale.FRANCE.language, Locale.FRANCE.country, "student")
...

I digged a bit inside the MessageSource of Spring. In that doc I found the function calculateFilenamesForLocale saying following the rules defined by Locale.toString()

So I don't understand where the problem comes from. Any idea ? Thank you.


Solution

  • Found it. Surprisingly, spring requires a base file from which we can add variants. So this kind of setup works :

    messages.properties <<<< required I guess for sprint to understand the basename and the fallback of any not found locale.
    messages_fr_FR_student.properties <<< can be called with messageSource.getMessage(code,args,Locale(Locale.FRANCE.language, Locale.FRANCE.country, "student") )
    messages_fr__te.properties <<< messageSource.getMessage(code,args,Locale(Locale.FRANCE.language, "", "te") )