Localization for a remote client


  • 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


  • 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";
            public interface ITest
                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;
                        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)
                public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { }
                public void Validate(ServiceEndpoint endpoint) { }
                #region IClientMessageInspector Members
                public void AfterReceiveReply(ref Message reply, object correlationState) { }
                public object BeforeSendRequest(ref Message request, IClientChannel channel)
                            CultureHeaderName, CultureHeaderNamespace, Thread.CurrentThread.CurrentCulture.LCID));
                    return null;
            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(), "");
                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;
                Thread.CurrentThread.CurrentCulture = new CultureInfo("de-de");
                Thread.CurrentThread.CurrentCulture = new CultureInfo("ja-jp");
                Thread.CurrentThread.CurrentCulture = currentCulture;
                Console.Write("Press ENTER to close the host");
    Tuesday, September 15, 2009 9:24 PM