Java framework --- Struts2 localization/internationalization (i18n), --- struts2i18n
Internationalization (i18n)Is planning and implementing products and services that enable them to easily adapt to specific local languages and cultures. This process is called localization. The process of internationalization is sometimes referred to as enabling translation or localization. I18n is short for internationalization, Because I and the two ends use n to start with, and there are 18 characters between I and the last n.
Access Method:
There are several methods to access information resources, including gettext, text tags, key attributes of UI tags, and International tags. Let's take a look at their simplicity:
To display the i18n text, use the call property tag gettext or any other tag. For example, the UI tag is as follows:
<s:property value="getText('some.key')" />
Text tag retrieval from the default resource package, that is, a message struts. properties
<s:text name="some.key" />
Any resource bundle on the i18n tag push stack. Other labels in the i18n label range can display messages of the resource package:
<s:i18n name="some.package.bundle"> <s:text name="some.key" /></s:i18n>
Key attributes of most UI tags can be used to retrieve messages from a resource package:
<s:textfield key="some.key" name="textfieldName"/>
The following describes how to implement international processing.
Structure directory:
Configuration File Struts. xml
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.0 // EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name = "struts. devMode "value =" true "/> <! -- Scan files starting with global in the Resource Directory --> <constant name = "struts. custom. i18n. resources "value =" global "/> <package name =" helloworld "extends =" struts-default "namespace ="/"> <action name =" empinfo "class =" com. oumyye. action. employee "method =" execute "> <result name =" input ">/index. jsp </result> <result name = "success">/success. jsp </result> </action> <action name = "locale" class = "com. oumyye. action. locale "method =" execute "> <result name =" success ">/index. jsp </result> </action> </package> </struts>
Processing class: Employee. java
package com.oumyye.action;import com.opensymphony.xwork2.ActionSupport;public class Employee extends ActionSupport{ private String name; private int age; public String execute() { return SUCCESS; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}
Processing class Locale. java
package com.oumyye.action;import com.opensymphony.xwork2.ActionSupport;public class Locale extends ActionSupport{ public String execute() { return SUCCESS; }}
View index. jsp
<% @ Page language = "java" contentType = "text/html; charset = UTF-8 "pageEncoding =" UTF-8 "%> <% @ taglib prefix =" s "uri ="/struts-tags "%> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
Jump to successful page Success. jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
Global_fr.properties
global.name = Nom d'utilisateur global.age = l'âgeglobal.submit = Soumettre desglobal.heading = Sé lectionnez Localglobal.success =Authentifi\t\u00E9 avec succ\u00E8s
Global_us.properties
global.name = Nameglobal.age = Ageglobal.submit = Submitglobal.heading = Select Localeglobal.success =Successfully authenticated
Global_zh.properties
global.name = \u59D3\u540Dglobal.age = \u5E74\u9F84global.submit = \u63D0\u4EA4global.heading = \u9009\u62E9\u4E00\u79CD\u8BED\u8A00global.success =\u6210\u529F
Effect Interface
Pay attention to the url address used for access ..
Reference: http://www.yiibai.com/struts_2/struts_localization.html