none
Localization for a remote client

    Question

  • Hi,
    I have written WCF application which contains some static data in the server, this data needs to be localized when call is requested by a client. But I am unable to figure out how to identify the OS culture of the remote client....
    The operation contract i have defined is GetData(string localizedvalue);
    Should i need to extened this as follows GetData(string localizedvalue, string oscultureofclient)

    Or is any other approach is available to achive this..

    Tuesday, September 15, 2009 5:08 PM

Answers

  • You can also use a message header to convey the client information to the server. And an IEndpointBehavior/IClientMessageInspector can be used to simplify the process of adding the header, like in the example below.

        public class Post_a6460e19_42d7_4ae0_8fdb_ca7bafd4f460
        {
            const string CultureHeaderName = "Culture";
            const string CultureHeaderNamespace = "the.culture.namespace";
            [ServiceContract]
            public interface ITest
            {
                [OperationContract]
                string Echo(string text);
            }
            public class Service : ITest
            {
                public string Echo(string text)
                {
                    int cultureHeaderIndex = OperationContext.Current.IncomingMessageHeaders.FindHeader(CultureHeaderName, CultureHeaderNamespace);
                    CultureInfo clientCulture;
                    if (cultureHeaderIndex < 0)
                    {
                        clientCulture = CultureInfo.InvariantCulture;
                    }
                    else
                    {
                        clientCulture = new CultureInfo(OperationContext.Current.IncomingMessageHeaders.GetHeader<int>(cultureHeaderIndex));
                    }
    
                    return String.Format("Client at culture {0} sent: {1}", clientCulture.DisplayName, text);
                }
            }
            static Binding GetBinding()
            {
                BasicHttpBinding result = new BasicHttpBinding();
                return result;
            }
            public class MyInspector : IClientMessageInspector, IEndpointBehavior
            {
                #region IEndpointBehavior Members
                public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) { }
                public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
                {
                    clientRuntime.MessageInspectors.Add(this);
                }
                public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { }
                public void Validate(ServiceEndpoint endpoint) { }
                #endregion
    
                #region IClientMessageInspector Members
                public void AfterReceiveReply(ref Message reply, object correlationState) { }
                public object BeforeSendRequest(ref Message request, IClientChannel channel)
                {
                    request.Headers.Add(
                        MessageHeader.CreateHeader(
                            CultureHeaderName, CultureHeaderNamespace, Thread.CurrentThread.CurrentCulture.LCID));
                    return null;
                }
                #endregion
            }
            public static void Test()
            {
                string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
                ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
                host.AddServiceEndpoint(typeof(ITest), GetBinding(), "");
                host.Open();
                Console.WriteLine("Host opened");
    
                ChannelFactory<ITest> factory = new ChannelFactory<ITest>(GetBinding(), new EndpointAddress(baseAddress));
                factory.Endpoint.Behaviors.Add(new MyInspector());
                ITest proxy = factory.CreateChannel();
    
                CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
                Console.WriteLine(proxy.Echo("Default"));
    
                Thread.CurrentThread.CurrentCulture = new CultureInfo("de-de");
                Console.WriteLine(proxy.Echo("German"));
    
                Thread.CurrentThread.CurrentCulture = new CultureInfo("ja-jp");
                Console.WriteLine(proxy.Echo("Japanese"));
    
                Thread.CurrentThread.CurrentCulture = currentCulture;
    
                ((IClientChannel)proxy).Close();
                factory.Close();
    
                Console.Write("Press ENTER to close the host");
                Console.ReadLine();
                host.Close();
            }
        }
    
    Tuesday, September 15, 2009 9:24 PM