Java EE实现多语言的方法有很多种,特别我采用的还是SSH框架,比如:struts的国际化,配置文件.properties,数据库;
项目已经完成了一大部分功能,里面jsp界面的标签还是使用html的标签并未使用status的标签(jsp界面,图片含有中文),并且在.java程序和.js文件里面也有很多的汉字。我这里采用的方法为配置文件.properties,每一种语言新增一个对应的.properties,目前有chinese.properties、english.properties,japanese.properties;
文件.properties的加载和获取类
/**
*@Title: SysTextConfig.java
*@Package: org.csun.ns.conf
*@Description: TODO
*@Author: chisj [email protected]
*@Date: 2016年2月15日下午1:52:10
*@Version V1.0
*/
package org.csun.ns.conf;
import java.io.File;
import java.io.InputStream;
import java.util.Properties;
/**
*@ClassName: SysTextConfig
*@Description: TODO
*@Author: chisj [email protected]
*@Date: 2016年2月15日下午1:52:10
*
*/
public class SysTextConfig {
privatestatic Properties properties = null; //读取properties的资源文件
private static String filePath = null; //保存properties文件的绝对路径
static { //初始化static变量的值
filePath ="chinese.properties";
loadSysConfigFile();
}
public class SysTextConfig {
privatestatic Properties properties = null; //读取properties的资源文件
private static String filePath = null; //保存properties文件的绝对路径
static { //初始化static变量的值
filePath ="chinese.properties";
loadSysConfigFile();
}
public static void loadSysConfigFile(String newFilePath) {
System.out.println("---loadSysConfigFile---" + newFilePath);
properties = new Properties();
InputStream inputStream =SysTextConfig.class.getClassLoader().getResourceAsStream(newFilePath);
try {
properties.load(inputStream);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String getConfigInfo(String key) {
String value = "";
try {
value =properties.getProperty(key);
} catch (Exception e) {
e.printStackTrace();
}
return value;
}
}
JSP界面:
<%@page import="org.csun.ns.conf.SysTextConfig"%>
<%=SysTextConfig.getConfigInfo("lang")%>
JS文件:
<input type="hidden"id="not" name="not"value="<%=SysTextConfig.getConfigInfo("not")%>">
<input type="hidden"id="points" name="points"value="<%=SysTextConfig.getConfigInfo("points")%>">
var points = document.getElementById("points").value;
var not =document.getElementById("not").value;
.
Java文件:
this.getSession().setAttribute("errorMsg",SysTextConfig.getConfigInfo("errorMsg_0"));
加载用户选择的语言:
languageFile = ntUserBiz.getLanguageFile(ntUser.getLanguage());
SysTextConfig.loadSysConfigFile(languageFile);
Jquery显示所有的语言选择:
function get_language() {
var userIdId = "userId";
var userId =document.getElementById(userIdId).value;
$.ajax({
type: "post",
url: "LanguageShow.action",
dataType: "json",
data: {"userId": userId},
success: function(data) {
var d = eval("(" + data + ")");
for (var i = 0; i < d.length; i++) {
var id = d[i].languageId;
var name = d[i].languageName;
var opt = "<option value='" + id + "'>" + name + "</option>";
$("#languageSelect").append(opt);
}
},
error: function() {
alert("系统异常,请稍后再试!");
}
});
}
$(document).ready(function() {
get_language();
});
Html界面
<div id="language"class="div_1">
<select id="languageSelect" name="lanageSelect" onchange="javascript:languageChoose()">
</select>
</div>
Action对应的方法:
public String languageShow() {
System.out.println("---languageShow---");
System.out.println("userId = "+ userId);
List<Language>languages= newArrayList<Language>();
try {
languages = ntLanguageImpl.languageShow(userId);
System.out.println("languages size = "+ languages.size());
JSONArrayjsonArray= JSONArray.fromObject(languages);
result = String.valueOf(jsonArray);
}catch(Exception e) {
e.printStackTrace();
}
return SUCCESS;
}
Biz对应的方法:
public List<Language> languageShow(String userId) {
// TODO Auto-generated method stub
ShortLANGUAGE= 1;
int flag = 1;
List<Language>languages= newArrayList<Language>();
List<NtLanguage>ntLanguages= newArrayList<NtLanguage>();
ntLanguages = ntLanguageDAO.findAll();
NtUserntUser= newNtUser();
try {
if (null == ntLanguages) {
return null;
}
System.out.println("userId = "+ userId);
ntUser = ntUserDAO.findById(Long.parseLong(userId));
//System.out.println("language = " +ntUser.getLanguage());
if (null == ntUser.getLanguage()) {
ntUser.setLanguage(LANGUAGE);
ntUserDAO.update(ntUser);
flag = 0;
}
for (NtLanguage nl : ntLanguages) {
Languagelanguage= newLanguage();
System.out.println("id = "+ nl.getLanguageId());
language.setLanguageId(nl.getLanguageId());
language.setLanguageName(SysTextConfig.getConfigInfo(nl.getLanguageName()));
//System.out.println("language = " +ntUser.getLanguage());
if (1 == flag && ntUser.getLanguage().equals(nl.getLanguageId())) {
System.out.println("equals...");
languages.add(0, language);
}else{
languages.add(language);
}
}
}catch(Exception e) {
e.printStackTrace();
}
return languages;
}
下拉框对应的方法
Action
public String languageChoose() {
System.out.println("---languageChoose---");
System.out.println("languageId = "+ languageId);
try {
langageFile = ntLanguageImpl.languageChoose(languageId, userId);
System.out.println("langageFile = "+ langageFile);
SysTextConfig.loadSysConfigFile(langageFile);
this.getRequest().setAttribute("change","true");
result = "ok";
}catch(Exception e) {
e.printStackTrace();
}
return result;
}
Biz:
public String languageChoose(String languageId, String userId){
// TODO Auto-generated method stub
NtUserntUser= newNtUser();
try {
ntUser = ntUserDAO.findById(Long.parseLong(userId));
ntUser.setLanguage(Short.parseShort(languageId));
ntUserDAO.update(ntUser);
}catch(Exception e) {
e.printStackTrace();
}
return ntLanguageDAO.findById(Short.parseShort(languageId)).getLanguageFile();
}
JS代码:
function languageChoose() {
var languageSelectId = "languageSelect";
var userIdId = "userId";
var languageId = document.getElementById(languageSelectId).value;
var userId =document.getElementById(userIdId).value;
//alert(languageId);
var url = "LanguageChoose.action";
post(url,{'languageId':languageId,'userId':userId});
}
function languageOnload(change) {
if (change == 'true') {
window.parent.left.location.reload();
window.parent.right.location.reload();
window.parent.bottom.location.reload();
//document.title = "123";
}
}