This question already has answers here:
Why do I get, "Culture is not supported" and What, if Anything, Should I Do about it?
(2 answers)
Closed 5 years ago.
I have a windows form application developed in VB.net with Target Framework 3.5. Application is running good in windows 7,8,8.1,10. But in windows XP, its showing an error :
Culture name 'en-in' is not supported
I have checked the code and find the line causing error is :
My.Application.ChangeCulture("en-IN")
when i tried to remove this line, many forms and reports are showing error or wrong information. So i can not remove this line.
Is there any way to install or load this Culture in windows ?
en-IN is not a known culture code.
You can check all existing codes in this list.
You can create a custom culture, using CultureAndRegionInfoBuilder, but it's highly unrecommended.
// Create a new Culture, with the name you desire
CultureAndRegionInfoBuilder cib = new CultureAndRegionInfoBuilder("en-IN", CultureAndRegionModifiers.None);
// Load all defaults from en-US
CultureInfo ci = new CultureInfo("en-US");
cib.LoadDataFromCultureInfo(ci);
// Populate the new CultureAndRegionInfoBuilder object with region information.
RegionInfo ri = new RegionInfo("US");
cib.LoadDataFromRegionInfo(ri);
// Now you can make changes, or finish.
// Changes can be currency, RegionName, etc.
// Finish
cib.Register();
this article explains how to do it.
Or you can set custom culture as :
Dim customCulture As Globalization.CultureInfo = New Globalization.CultureInfo("en-US")
customCulture.DateTimeFormat.ShortDatePattern = "dd-MMM-yyyy"
customCulture.DateTimeFormat.LongDatePattern = "dd-MMM-yyyy HH:mm:ss"
customCulture.DateTimeFormat.ShortTimePattern = "HH:mm:ss"
customCulture.DateTimeFormat.LongTimePattern = "HH:mm:ss"
System.Threading.Thread.CurrentThread.CurrentCulture = customCulture
System.Threading.Thread.CurrentThread.CurrentUICulture = customCulture
Related
We are experiencing weird behaviour between a web application and windows service when trying to perform a ToString() on a DateTime value.
See the example below.
DateTime parsedReportDate;
reportDate = DateTime.Now.ToString("yyyyMMdd");
reportDateWithSlash = DateTime.Now.ToString("dd/MM/yyyy");
if (DateTime.TryParse(MyDateValue, out parsedReportDate))
{
reportDate = parsedReportDate.ToString("yyyyMMdd");
reportDateWithSlash = parsedReportDate.ToString("dd/MM/yyyy");
}
--reportDateWithSlash on Web Application: 28/03/2017
--reportDateWithSlash on Windows Service: 28-03-2017
The Windows Service calls the same function as the Web Application does, so why is the formatting different then?
The formatting of dates to strings uses a CultureInfo object to know what format to use.
Each Thread has a Thread.CurrentCulture property.
You can find out what CultureInfo the current Thread is set by getting the current Thread using Thread.CurrentThread and then inspecting it's Thread.CurrentCulture property.
public class Program
{
public static void Main()
{
Console.WriteLine(Thread.CurrentThread.CurrentCulture.Name);
}
}
https://dotnetfiddle.net/dsA3VT
Output: en-US
You can set the CultureInfo for the the Thread, or pass it with each call to ToString.
Setting Thread.CultureInfo
You can set the Thread.CultureInfo using the same property as you use to read it.
Thread.CurrentCulture = new CultureInfo("en-gb");
Unfortunately .Net Fiddle doesn't support changing thread properties.
I didn't know this, but bradbury9 pointed out that since .net 4.6 you can set the CultureInfo.CurrentCulture property as well.
CultureInfo.CurrentCulture = CultureInfo.CreateSpecificCulture("nl-NL");
Unfortunately .Net Fiddle doesn't support changing the culture this way either.
Passing CultureInfo to ToString
'DateTime.ToString' has overloads which can take an IFormatProvider, and CultureInfo impliments IFormatProvider.
DateTime.Now.ToString(new CultureInfo("en-gb"));
https://dotnetfiddle.net/qkS5HF
public class Program
{
public static void Main()
{
var dateTime = DateTime.Now;
Console.WriteLine(Thread.CurrentThread.CurrentCulture.Name);
Console.WriteLine(dateTime.ToString(CultureInfo.InvariantCulture));
Console.WriteLine(dateTime.ToString(new CultureInfo("en-us")));
}
}
Output:
en-US
03/28/2017 09:43:49
3/28/2017 9:43:49 AM
The problem must come from having different cultures. Using the DateTime.ToString (String, IFormatProvider) overload with the CultureInfo.InvariantCulture property should solve the problem:
DateTime.Now.ToString("dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
it may be what is calling the Windows service is formatting the date. the code certainly is clear enough. Try debugging the windows service by attaching to the running process and see what it generates. If your service consumer is a web app, look at F12 developer tools and see what is getting sent back int he response stream.
I'm working on a localized Windows (Phone) 10 UWP app.
I have implemented support for 2 language: en-US and nl-NL (Dutch-Netherlands). This works fine: When the user has selected English in the phone settings the app starts in English and when the user has selected Dutch in the phone settings the app starts in Dutch.
To get this working I had to make some changes in the package.appxmanifest because my language resources are in a different DLL:
<Resources>
<Resource Language="nl-NL"/>
<Resource Language="en-US"/>
</Resources>
But I cannot find any way to get the regional format from the user for date, time and number formatting.
When the user has selected English as language but Dutch (Netherlands) for regional format, my app starts with both
System.Globalization.CultureInfo.CurrentUICulture and System.Globalization.CultureInfo.CurrentCulture set to "en-US", where System.Globalization.CultureInfo.CurrentCulture should be "nl-NL".
I have been searching all of the documentation but I cannot find a way to retrieve the phone regional format (except Windows.System.UserProfile.GlobalizationPreferences.HomeGeographicRegion which is something different).
Is there a way to retrieve the phone regional format?
I only know the following hack mentioned here
var systemLanguage = GlobalizationPreferences.Languages.First();
var regionInfo = new RegionInfo(systemLanguage);
var dtf = new DateTimeFormatter("longdate", new[] { regionInfo.TwoLetterISORegionName });
var regionInfoName = dtf.ResolvedLanguage;
var regionFormatCultureInfo = new CultureInfo(regionInfoName);
Peter Meinl's answer works, but is a little confusing because you do not need the RegionInfo.
Pedro Lamas describes the hack using the ResolvedLanguage of DateTimeFormatter just using "US".
DateTimeFormatter dtf = new DateTimeFormatter("longdate", new[] { "US" });
string regionInfoName = dtf.ResolvedLanguage;
CultureInfo regionFormatCultureInfo = new CultureInfo(regionInfoName);
The ResolvedLanguage property of the DateTimeFormatter will contain the regional format id of the phone, in this case "nl-NL".
Mind that you DO need a language in the constructor of the DateTimeFormatter, just new DateTimeFormatter("longdate") or DateTimeFormatter.LongDate won't work.
I have the following snippet of Code for Convert Gregorian Date to Hijri Date.
public static string GregoriantoHijri(DateTime gregorianDate)
{
CultureInfo arCI = new CultureInfo("ar-SA");
var hijriCalendar = new HijriCalendar();
hijriCalendar.HijriAdjustment = App_Code.StoreRetrieveSettingsAssist.getHA();
arCI.DateTimeFormat.Calendar = hijriCalendar; //CODE FAILS HERE
string hijriDate = gregorianDate.ToString("dd-MM-yyyy", arCI);
return hijriDate;
}
This code runs perfectly for my Windows Mobile App which is also posted on Store.
However the same code is giving me issues in Xamarin.Android
The Error:
System.ArgumentOutOfRangeException:
Not a valid calendar for the given culture.
Parameter name: value
I don't understand why codes using same .NET base class have issues on different platforms. Can you suggest a workaround cause this doesn't seem to work.
You might want to consider NodaTime. It is supposedly more robust than the native .NET datetime handling, and is supposed to support Hijri.
I am currently working on a windows phone application that takes some information from the user and returns some other information based on the user input.
The application works great if the specific device has its region settings set to US. If the region settings of the device are set to Greek or German, some problems occur. For example, the US decimal point character "." is considered as "," and vice versa. As a result, all the calculations are false.
What I want to do is internationalize the application so that it works exactly the same no matter what the regional settings are. Is this possible?
If you want your app allways to show number and dates in one specify format you can force the app to allways run in one specify culture like this.
You just have to set the current thread of your app to one specify culture (add to the App.cs file)!
public App()
{
// Standard Silverlight initialization
InitializeComponent();
// Phone-specific initialization
InitializePhoneApplication();
// Set the current thread to US!
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
I have screen in c# asp.net webapplication, where i add news on particular dates.And can edit those dates also.It workes in my local sytem.But shows datetime error when it was running in iis 7 server(Used sql database).And i knew that the short date and long date format in server was different from local system.So i changed date format in local system same as in iis.But still it is working properly.
Instead of guessing culture settings write code that sets one you need before reading from database/restore after unsing Thread.CurrentCulture property. Simialr to code below (need to also use CurrentUICulture, chose cuture you need and wrap code around setting/restoring into try/finally for real code)
var oldCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
// read from DB
...
Thread.CurrentThread.CurrentCulture = oldCulture;