Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of SpringMVC internationalization support

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the example analysis of SpringMVC internationalization support, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.

How it works: register a localeResolver (zone parser) in DispatchServlet and add a Locale interceptor (LocaleChangeInterceptor) to detect changes in parameters and locale in the request.

Register ResourceBundleMessageSource in the application context and define the path and name of the internationalization file in the program.

1. Language parser

In SpringMVC, common language parsers are

Header resolver: get the internationalized language that the user needs by parsing the accept-language of the client request header information center. For more information, see = AcceptHeaderLocaleResolver

Cookie resolver: obtain the internationalization information that the user needs by parsing the locale specified by Cookie on the client. For more information, see = CookieLocaleResolver

Session resolver: the required internationalization information is obtained by parsing the loacle information in the client request domain and stored in the httpSession. For more information, see = SessionLocaleResolver

two。 Area interceptor

We need to register an interceptor in DispatchServlet to monitor regional changes, which can help us detect request parameters and change the locale according to the language corresponding to the request parameters.

3. International resource allocation

4. Page internationalization

Here, I am using the fmt tag of jstl to achieve internationalization. Students who are interested can also use spring's message tag.

1) introduce tag library

2) specify the name of the international file resource

3) read the file information by internationalizing the key of the resource file.

5. What we chose in the second step is the SessionResolver parser. So in the request, we need to concatenate the locale= specific language identity (for example: locale=zh_CN) after the parameter of url.

Note: it can be troublesome to concatenate url on every page, and users usually want to do what they want to do. Choose a language once, and then choose the language first. So interested students can consider extending LocaleChangeInterceptor to achieve more perfect functions.

Thank you for reading this article carefully. I hope the article "sample Analysis of SpringMVC internationalization support" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report