Struts2 study notes 18 (International) and struts2 Study Notes

Source: Internet
Author: User
Tags i18n

Struts2 study notes 18 (International) and struts2 Study Notes
Java Internationalization

Program internationalization:
The program can automatically translate content according to the country and language environment where the machine is located.
The essence of internationalization is: search, replace, and find the language and text corresponding to the keywords in the corresponding country from the resource package.
Internationalization steps:
(1) provide international resource files
If the country resource package contains non-Western European characters, usenative2asciiProcess files,
     native2ascii sourcefile destfile
Generate a file with Unicode characters
Resource File Syntax: File NameLanguage codeCountry Code
Xiyang_en_US.properties
Hi = "hello"
Xiyang_zh_CN.properties
Hi = "Hello"
Note: you cannot directly write "hello". To use Unicode encoding, convert it to "hi =" \ u4f60 \ u597d"
(2) output the key value of the international message in the program, so that the program will search for the key value from the resource package.

  • Provide international resource files

    Mess_zh_CN.properties

  • Java Internationalization

Public class LocaleTest {public static class main (String [] args) {// view supported languages and country code System. out. println ("view language, country code"); Locale [] locales = Locale. getAvailableLocales (); for (Locale locale: locales) {System. out. println (locale. getDisaplayLanguage () + "---->" + locale. getLanguage (); System. out. println (locale. getDispalayCountry () + "---->" + locale. getCountry ()} // two resource packages are provided: hi_en_US.properties, hi_zh_CN.properties // obtain the local environment Locale locale = Locale. getDefault (Locale. ctegory. FORMAT); // bind the resource file ResouceBundle bundle = ResourceBundle. getBundle ("hi", locale); System. out. println (bundle. getString ("hi "));}}
Struts2 internationalization instance JSP Internationalization
<! -- Internationalization of JSP pages --> <! -- Name indicates the baseName of the international message --> <! -- Note s: Scope of i18n --> <% @ page language = "java" import = "java. util. * "pageEncoding =" gb2312 "%> <% @ taglib prefix =" s "uri ="/struts-tags "%> <! -- It is best to set a resource folder under src and put it under the content file. --> <! -- Important --> <s: i18n name = "resource/I18nJSP"> 
Action Internationalization
Public String execute () throws Exception {// TODO Auto-generated method stub if (name. equals ("me") & pass. equals ("12345") {setTip (getText ("login", new String [] {name}); return SUCCESS;} return ERROR;}/* with the same name as action, the system automatically loads Login_zh_CN.propertieslogin = \ u5F88 \ u9AD8 \ u5174 \ u89C1 \ u5230 \ u4F60 \ uFF0C {0} \ Users = Nice to meet you, {0 }\! */
Global Internationalization

All international keys are concentrated in one file and then automatically called by the system.
The focus is on configuring the constant struts. custom. i18n. resoures in struts. xml.

Struts uses the browser settings by default to select the country and language
Provides a mechanism for users to select a national language environment
A. Determine Based on request_locale
For example, when you submit a request, add request_locale = en_US to set the language environment to English.
B. Modify the language environment directly using browser options

The focus is to add constants in struts. xml:<constant name="struts.custom.i18n.resources" value="/resource/globalI18n"></constant>

Summary:
Both jsp pages and global internationalization require the location of the resource file to be specified.
      <s:i18n name="">
      <constant name="struts.custom.i18n.resources" value="/resource/globalI18n"></constant>
The range of action and package is automatically loaded.
You only need to note that the action range must be the same as the action, and the package must be named package_xx_xx.properties.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.