Bayoneda
Bayoneda

Reputation: 15

Spring Framework - How to change locale in controller

I am new to Spring, and currently confusing about localization. I'm using the following code to get text from messages_jp.properties file.

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="messages" />
</bean>

And now I want to switch to text from messages_en.properties file, is there any ways to change locale in controller within if...else... block, not with using url params like "?lang=en", something like:

if (user.getLang() == 1) {
    // set locale to en
} else {
    // set locale to jp
}

Thanks in advance!

Upvotes: 0

Views: 1023

Answers (1)

Krunal Patel
Krunal Patel

Reputation: 186

Try this one

<util:properties id="yourFileNameId" location="classpath:/yourFileName.properties"/>

In Controller

@Value("#{yourFileNameId['message_id']?:1}")
private int smalltext;

Upvotes: 1

Related Questions