![](/img/trans.png)
[英]how can I share proxy objects across multiple Axis2 web service clients?
[英]Internationalization in Axis2 clients
我的網絡服務中有以下代碼
package com.manumehrotra.hs.samples.internationalization;
import java.util.MissingResourceException;
import javax.servlet.http.HttpServletRequest;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.i18n.Messages;
import org.apache.axis2.transport.http.HTTPConstants;
public class SampleInternationalizationHS {
public String getInternationalizedMessage(){
System.out.println("in getInternationalizedMessage");
String retVal = null;
String language = null;
// Get the current MessageContext
MessageContext msgContext = MessageContext.getCurrentMessageContext();
// Get HttpServletRequest from Message Context
Object requestProperty = msgContext.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);
if (requestProperty != null && requestProperty instanceof HttpServletRequest) {
HttpServletRequest request = (HttpServletRequest) requestProperty;
language = request.getLocale().getLanguage();
// language codes are available at - http://www.loc.gov/standards/iso639-2/php/code_list.php
}
System.out.println(language);
String msg_key = "samplemsg_en";
try {
msg_key = "samplemsg_"+language;
} catch (Exception e) {
msg_key = "samplemsg_en";
}
System.out.println(msg_key);
try {
retVal = Messages.getMessage(msg_key, "Jack");
} catch (MissingResourceException mre) {
// do nothing...
System.out.println("resource not found");
}
System.out.println("returning "+retVal);
return retVal;
}
}
我想知道如何在Java客戶端中設置不同的語言環境值,以便上面提到的Web服務代碼可以識別它。
當前僅識別“ en”。
以下代碼可用於在客戶端的SOAP標頭中設置語言環境
ServiceClient client = stub._getServiceClient();
OMFactory omFactory = OMAbstractFactory.getOMFactory();
OMElement omElement = omFactory.createOMElement(new QName("http://www.w3.org/2005/09/ws-i18n","locale", "i18n"));
omElement.setText("en");
client.addHeader(omElement);
為了在服務器端檢索語言環境,使用了以下代碼
SOAPHeader header = MessageContext.getCurrentMessageContext().getEnvelope().getHeader();
OMElement firstChildWithName = header.getFirstChildWithName(new QName("http://www.w3.org/2005/09/ws-i18n","locale","i18n"));
if (firstChildWithName != null) {
String locale = firstChildWithName.getText();
System.out.println(locale);
if(locale!=null)
language = locale.trim();
else
language="en";
}
我已經嘗試過使用示例Web服務和Web服務客戶端進行相同的操作,並且可以正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.