I've encountered several times the same problem in applications we develop:
We want to allow the user to edit/display it's data in his format (date, currency, ...), but we want to display the application in English only (for several reasons, it's a pro, international application, in a domain in which we communicate mostly in English).
There is no problem when we manage the whole application, but most of third-party pro frameworks that I used (Telerik, DevExpress) are using the CurrentCulture to display my data in the correct format AND in the corresponding language.
So, even if I have my computer in English, I have my regional settings set to fr-CH, I will have all third party user controls in French.
I cannot set the CurrentCulture to a specific culture and set the format of my user controls to something else (I would loose my default format) and I can't let the CurrentCulture to be the default one because I would have my third party components in another language.
I tried to build my own culture (CultureAndRegionInfoBuilder), with no success. When I change the language, I still have my application in the user-specific language.
Concrete problem
I'm using a date editor(basic, it has one text input and can popup a calendar). I want to have the date displayed in my OS locale(ch-FR, so 15 january 2013 would be "15.01.2013"), but I don't want that when I display the calendar month/day name appears in french.
What is the correct approach with this?
Store the original CultureInfo for your purposes and try editing CurrentCulture and CurrentUICulture properties of the CurrentThread property in System.Threading.Thread, maybe this will solve your problem.
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
I resolved my problem by having a custom culture info:
private static void UpdateCultureInfoWithoutLangage()
{
//We initialize a en-US cultureInfo and change all formats + number infor related
CultureInfo cultureInfoEn = new CultureInfo("en-US");
CultureInfo cultureInfoEnClone = (CultureInfo)cultureInfoEn.Clone();
//Setting DateTimeFormat(Without changing translations)
cultureInfoEnClone.DateTimeFormat.FirstDayOfWeek = CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
cultureInfoEnClone.DateTimeFormat.FullDateTimePattern = CultureInfo.CurrentCulture.DateTimeFormat.FullDateTimePattern;
cultureInfoEnClone.DateTimeFormat.LongDatePattern = CultureInfo.CurrentCulture.DateTimeFormat.LongDatePattern;
cultureInfoEnClone.DateTimeFormat.LongTimePattern = CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern;
cultureInfoEnClone.DateTimeFormat.MonthDayPattern = CultureInfo.CurrentCulture.DateTimeFormat.MonthDayPattern;
cultureInfoEnClone.DateTimeFormat.ShortDatePattern = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
cultureInfoEnClone.DateTimeFormat.ShortTimePattern = CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern;
cultureInfoEnClone.DateTimeFormat.TimeSeparator = CultureInfo.CurrentCulture.DateTimeFormat.TimeSeparator;
cultureInfoEnClone.DateTimeFormat.YearMonthPattern = CultureInfo.CurrentCulture.DateTimeFormat.YearMonthPattern;
cultureInfoEnClone.NumberFormat = CultureInfo.CurrentCulture.NumberFormat;
Thread.CurrentThread.CurrentCulture = cultureInfoEnClone;
Thread.CurrentThread.CurrentUICulture = cultureInfoEnClone;
Application.CurrentCulture = cultureInfoEnClone;
}
Related
I have a localized WinForms app and the default locale is set German (de-DE). I've added 2 new locales (en-GB and en).
To get a particular culture info I do:
var culture = CultureInfo.GetCultureInfo("en-GB");
When the app is installed and the user's current culture is for example, en-US, how should I handle a fallback to match the other locale I have available, ie. en ?
When doing:
var culture = CultureInfo.GetCultureInfo("en");
The culture is not found and it falls back to my default locale.
I'm running a .NET Core app which contains DateTime.Now.ToString(). When on my PC it displays European date format (dd/mm/yy) but when running on a Linux VM located in the US it displays American date format (mm/dd/yy) despite the timezone on the VM being GMT.
How can I make it display only European date format?
I know I can format it manually inside the program, but is there a way to do this globally on the Linux system?
To set a culture for all application threads you can use CultureInfo.DefaultThreadCurrentCulture property, like that
if (Thread.CurrentThread.CurrentCulture.Name == "en-US")
{
culture = CultureInfo.CreateSpecificCulture("...");
CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;
}
You are checking that current culture is en-US and update the default culture to any EU specific culture, like de-DE or fr-FR for example. You can also refer to MSDN for details
You can use below code :
DateTime.Now.ToString("dd/MM/yy")
It should work.
For more info: https://www.c-sharpcorner.com/blogs/date-and-time-format-in-c-sharp-programming1
I'm trying to prepare an application with more languages available.
I prepared the simplest example to learn it, I done a lot of tentative but I'm not able to do it.
CultureInfo cul = new CultureInfo("de-De");
Resources.Culture = new System.Globalization.CultureInfo("de-De");
label1.Text = TestLanguages.Properties.Resources.Saluto;
In my application I have two resources different resources , one for Italian language, one for German.
Italian Resource : Saluto -> Ciao
German Resource : Saluto -> Hallo
But I can't use the German one. How can I do it?
You have to change the UI culture of the currently executing thread.
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("de-De");
label1.Text = TestLanguages.Properties.Resources.Saluto;
See the documentation for Thread.CurrentUICulture
Gets or sets the current culture used by the Resource Manager to look up culture-specific resources at run time.
I am trying to get right string values from culture resource file but it's not working, always returning english resources,
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
but
Resources.Resource1.myResource;
still getting english resources, I have two files Resource1.resx and Resource1.fr-FR.resx
I think that you need
Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR")
Thread.CurrentThread.CurrentUICulture
Gets or sets the current culture used by the Resource Manager to look
up culture-specific resources at run time.
I am looking for code in .NET (C#)
to change the system date/time format in Regional Options of the Control Panel
for Windows XP.
To change date-time format take a look at this http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/263d73b2-8611-4398-9f09-9aa76bbf325e/
You basically need to use native Win API method SetLocaleInfo.
If I understand right, you want to programatically change regional settings permanently, not just for your current process.
The information you need is stored in the registry. For the current user under the key:
HKCU\Control Panel\International
And the default for new users or users without a profile:
HKEY_USERS\.Default\Control Panel\International
You could change the registry values programatically, then broadcast a WM_SETTINGCHANGE message as described in a response to this question.
If you want to change temporarily the regional settings you can use following code:
System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
...
YOUR CODE
...
System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;
TO find out which regional setting is best, check:
CultureInfo
More reading on System.Globalisation
public void changedatetimeformat()
{
RegistryKey regkey = Registry.CurrentUser.OpenSubKey(#"Control Panel\International", true);
regkey.SetValue("sShortDate", "dd/MM/yyyy");
regkey.SetValue("sLongDate", "dd/MM/yyyy");
}
Then
Restart Your System