Example of internationalization implementation of struts 2

I've always seen I18N before. Now I know that I18N is internationalization, because it starts with I and ends with N, with a total of 18 letters, which means internationalization. There is no switch between Chinese and English on Baidu search home page. After all, baidu is designed for Chinese people in China. There is no need to make an English switch. There are some on the Google search home page. Because Google's servers in China are placed in Hong Kong, there is also traditional switching. How did this happen? In fact, the principle is very simple and easy to implement with struts 2. Take the implementation of Chinese English switching as an example:

Step 1: add two resource files in the SRC directory. The naming method is very important. Format: basename_ language_ country. properties。 There are two other naming methods, both of which need not be studied too much. For example, to switch between Chinese and English, we need to add resource_ zh_ CN. Properties and resources_ en_ US. properties。 Then add each key value pair to the two files, and the key field must be unified. For example:

Among them, placeholders can be used for the value value, such as hello {0}. As for how to pass parameters, we will explain later.

Step 2: configure struts Properties file, which is also placed in the SRC directory.

The first key value pair is to tell the program that the internationalization resource file is the resource file named at the beginning of loginresource.

In fact, the configuration is finished here, and the next step is to use it in various places.

(1) Use in JSP. Give two examples.

You can also output information with placeholders. For example, there is a login Welcome = Hello {0}, you can display it in the following way:

(2) Used in action. For example:

You can also use information with placeholders. For example, there is a login Welcome = Hello {0}, you can display it in the following way:

The third step is to switch between Chinese and English in the JSP page.

index. The main codes in JSP are as follows:

Mainly look at the first two lines of code. Lang.action is actually an action to realize page conversion. There is no substantive content. Just define a helloaction Java, just return a success in the execute () method. The parameter request in the URL_ The locale is read by the I18N interceptor and the locale is set according to this value. I18N interceptor is a built-in interceptor in default in struts.

struts. The XML configuration is as follows:

Oh, it's almost finished. The next step is to see the effect. Run the following:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>