简体   繁体   English

Axis2客户的国际化

[英]Internationalization in Axis2 clients

I have the following code in my webservice 我的网络服务中有以下代码

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;
    }
}

I want to know how to set different locale values in my java client so that the webservice code mentioned above recognizes it. 我想知道如何在Java客户端中设置不同的语言环境值,以便上面提到的Web服务代码可以识别它。

Currently only "en" is being recognized. 当前仅识别“ en”。

Following code can be used to set locale in SOAP headers at the client 以下代码可用于在客户端的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);

and for retrieving locale on server side the following code is used 为了在服务器端检索语言环境,使用了以下代码

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";
}

I have tried the same with a sample webservice and webservice client and it works. 我已经尝试过使用示例Web服务和Web服务客户端进行相同的操作,并且可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM