0

We have a Web.API application where we have to supported a Dutch and English language. For that we have set the culture as “nl” or “en” based on the user request.

But, we have facing the issue in “NumberDecimalSeparator” because for “en” it’s “.” and for “nl” it’s “,”. Now When user want to post or get any data for “nl” language then value should be display/posted as “,” and for “en” language it should display/posted as “.”.

Does anyone have idea on that How we can handle this ?

I am using Linq query and I have to display retried data based on language.

Thank you in advance..

Linq Query as bellow

    var test = await (from x in db.instance.Where(x => x.ID == 36)
                   select new GeofenceAttributeModel()
                   {
                     Address = x.geofenceattribute != null ? x.geofenceattribute.Address : string.Empty,
                     Latitude = x.geofenceattribute != null && x.geofenceattribute.Latitude.HasValue ? x.geofenceattribute.Latitude : null,
                     Longitude = x.geofenceattribute != null && x.geofenceattribute.Longitude.HasValue ? x.geofenceattribute.Longitude : null,
                     RadiusInMtr = x.geofenceattribute != null ? x.geofenceattribute.RadiusInMtr.ToString() : string.Empty
                   }).FirstOrDefaultAsync();
Anish Patel
  • 84
  • 1
  • 4
  • Does https://stackoverflow.com/a/24721335/34092 or https://forums.asp.net/t/1962812.aspx?Localization+in+web+Api help? – mjwills Jul 20 '17 at 12:45
  • Thank you.. Yes, I have set the current culture and current UI culture dynamically as per the 2nd link. But problem with the decimal number e.g if in database I have a value as 4.5 then I have to display it as 4,5 when nl language is selected and I have to handle it into the linq query. – Anish Patel Jul 20 '17 at 12:52
  • Numbers don't have a format. They are merely formatted when you convert them to a string. If you set the culture correctly, formatting will be automatic - whether using LINQ or anything else. – mjwills Jul 20 '17 at 12:53
  • Right, I have a Nullable data type, when I tried to convert this number to string e.g mynumber.ToStting() in linq query then it return result as "4.5" instead of "4,5". Culture is set correctly because other labels in site are display in dutch. – Anish Patel Jul 20 '17 at 12:58
  • Update your post to include the LINQ query. – mjwills Jul 20 '17 at 12:59
  • Formatting needs to be left up to the clients of your service. When they send a number to you, it should be a `float` in some culture-neutral form. It needs to be your service client's job to convert between presentation (what your users see or enter) and internal representation (what your service sees). – Sergey Kalinichenko Jul 20 '17 at 13:33

1 Answers1

3

Welcome to globalization support.

A rule of thumb: use the current locale only when it is parsed or displayed in the UI (eg. web browser).

In every other case (storing data, communication between components in textual formats, including json or XML body of posts) use invariant culture. At server side it should be completely transparent whether the UI uses Dutch or English or any other culture.

Never serialize numbers and datetime values with the local culture; otherwise, when you have to deserialize your data you cannot tell anymore whether a comma is a decimal sign or thousand separator, for example.

In your code RadiusInMtr.ToString() uses the system's local culture (Thread.CurrentThread.CurrentCulture). It is OK only if the string is displayed in the UI. If you use this string to transfer some data to another component, use ToString(CultureInfo.InvariantCulture) instead. And specify the culture when you parse the string similarly.

György Kőszeg
  • 17,093
  • 6
  • 37
  • 65
  • Okay.. I think I have to handle number formatting at front end level.. Based on language I have to parse the number and display as "," or "." and when making a request to the server I have to first convert "," to "." and then post it to server. – Anish Patel Jul 20 '17 at 13:19