Changing Application Culture at Runtime Failure - c#

All, I have a localised WinForms application. I have created a facility where users are able to change the required culture/language of the application at runtime. To avoid doing anything fancy, when the use does want to change the culture they are prompted that the application must be restarted for the change in culture to take effect (reasonable as this is not going to happen often if at all). I then store the new language required as a string in one of my settings XML files ("de-DE", "en-US", "en-GB" et al.). Then when the application restarts, if required I switch to the required culture:
// Main constructor.
public SqlEditorForm(string[] args)
{
// Load settings.
username = Environment.UserName;
userSettings = UserSettings.Instance();
advUserSettings = AdvanceUserSettings.Instance();
CheckChangeCurrentCulture();
isInitialising = true;
InitializeComponent();
...
}
private void CheckChangeCurrentCulture()
{
//if (!Debugger.IsAttached)
// Debugger.Launch();
if (advUserSettings.DefaultCulture)
return;
else
{
CultureInfo culture = new CultureInfo(advUserSettings.CustomCultureString);
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
}
}
Now when running in debug/release mode from VS2012, this mechanism works fine. However, when I install the application on (running Windows 7 (x86/x64)) various machines this mechanism no longer works, that is to say that the culture does not switch from the machines default EVER.
I have attached the debugger to the installed application and the code to change the culture is being invoked and the logic seems to be working and no exceptions are thrown, but the culture/language does not change. I have been though many questions on SO but cannot find an applicable one that covers this particular issue.
Why is this mechanism working from VS2012, but not for the installed application? What do I need to do in order to achieve my desired behaviour?
Thanks for your time.
Note, I have also tried replacing the call to CheckChangeCurrentCulture(); with:
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("de-DE");
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("de-DE");
which also works within VS2012 (Debug and Release modes) but not with the installed application. I have also changed the machines culture Start -> Control Panel -> Region and Language to German ("de-DE") expecting the application and the .NET framework to detect I have that culture available and use it. This also failed for the installed application.

Maybe on your production machines the sattelite assembly of the new culture cannot be loaded for some reason and hence your application falls back to the neutral culture.
Check with Microsoft sysinternals Process Monitor tool for unsuccessfull satellite assembly file accesses.
You an also check with .NET fuslog tool http://msdn.microsoft.com/en-us/library/e74a18c4.aspx

Related

Where System.Threading.Thread.CurrentThread.CurrentCulture is set

Where is System.Threading.Thread.CurrentThread.CurrentCulture set? I have a US customer (and I am in US); the debugging code shows the value equals "en-GB". And therefore dates are shown in British format ('DD/MM/YYYY'). Where is this set, in which file?
You can force current CultureInfos by CultureInfo.CurrentUICulture and CultureInfo.CurrentCulture.
The first one effects to text based things such like datetime etc. The second one effects to numbers such like floating points. Setting both to same CultureInfo is basic implement.
Note that, this will work fine if you don't consider multi-languages. Or you need to add language selecting menus.
Generally, CultureInfo comes from the OS (presuming it's not explicitly overridden in code); in case of a web application, CultureInfo typically comes from the client OS, via HTTP's Accept-Language header (you can likely monitor this with tools like Fiddler, etc).
In your case, if it's possible to ask the client to check their system setting - on Windows 10, the PowerShell command Get-Culture will return the current culture set in the OS, for instance - you might be able to determine the source of the problem.
Furthermore, it is possible to set a different culture for the current user (no Admin required, I believe), using the PowerShell Set-Culture -CultureInfo en-US command, where en-US is the desired culture (no pun intended). There is also a way to reset it for the entire machine, with the PowerShell Set-WinSystemLocale -SystemLocale en-US command (Admin rights and a reboot are required).

How to set UWP language on runtime

i'm currently developing a cross-platform App using Xamarin.Forms 3.4 and Visual Studio 2017 (latest version by now).
Since that App should support multiple languages which can be changed on runtime, i'm currently looking into ways to get this done. I already added several resources and translated all interface elements which works just fine. I read through this article to get started:
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/localization/
For now, when using the UWP app, the language is automatically set to my current system settings, which is the german language, even if the default app language is english. Thats fine.
Now, I got a method which configures my current language by setting several information, like the default CultureInfo object. That method looks like follows:
public void UpdateAppLanguage()
{
CultureInfo ci;
Language l;
// storage is a class containing several persistent information, like
// the language selected by the user to be used
// the Local attribute states if the language is actually present on
// the current user's system
if(Storage.GetCurrentLanguage().Local == true)
{
// language is present locally, so the user wants to use that one for
// the interface
ci = new CultureInfo(Storage.GetCurrentLanguage().Code);
AppResources.Culture = ci;
CultureInfo.CurrentCulture = ci;
CultureInfo.CurrentUICulture = ci;
CultureInfo.DefaultThreadCurrentCulture = ci;
CultureInfo.DefaultThreadCurrentUICulture = ci;
DependencyService.Get<ILocalize>().SetLocale(ci); // set the Thread for locale-aware methods
}
else
{
// no preferences available yet, use the system's current ci
if (Device.RuntimePlatform != Device.UWP)
ci = DependencyService.Get<ILocalize>().GetCurrentCultureInfo();
else
ci = CultureInfo.CurrentCulture;
l = new Language{
Name = ci.EnglishName,
Code = ci.TwoLetterISOLanguageName
};
Storage.SetCurrentLanguage(l);
CultureInfo.CurrentCulture = ci;
CultureInfo.CurrentUICulture = ci;
AppResources.Culture = ci; // set the RESX for resource localization
DependencyService.Get<ILocalize>().SetLocale(ci); // set the Thread for locale-aware methods
}
}
I'm currently testing with a language set, and that language is English.
And here is the dependency service implementation for the UWP sub-project:
public class Localize : stereopoly.ILocalize
{
public void SetLocale (CultureInfo ci)
{
//Windows.Globalization.ApplicationSettings.PrimaryLanguageOverride = ci.Name;
Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = "EN-US";
Windows.ApplicationModel.Resources.Core.ResourceContext.GetForCurrentView().Reset();
Windows.ApplicationModel.Resources.Core.ResourceContext.GetForViewIndependentUse().Reset();
}
public CultureInfo GetCurrentCultureInfo ()
{
return null;
}
}
As you can see, I also tried to set the language to EN-US by force, which doesn't work either. I'm calling the UpdateAppLanguage() method even before the initial Application.InitializeComponent() method runs, I also tried calling it within the OnStart() event method, neither works.
I'd expect the language change getting applied since its executed even before the actual Application starts, but I can switch the several pages of my app as often as I want, the language will always be german, no matter what I do.
I found several answers to this question for plain UWP apps, but not for Xamarin.Forms implementations of UWP projects and that seems to be the problem here, because no other hint regarding pure UWP apps seem to work.
Do you have any idea what might help me?
Thanks.
Make sure you are targeting at least UWP 16299 (FCU), because before that version UWP had issues properly switching languages in debug mode. If you run in Release mode however, it should work normally.
In addition the UWP app needs to know it supports a given language, because this is generated at compile time (using <Resource Language="x-generate"/> in Package.appxmanifest)
You can do this by adding a Strings folder in your UWP project and then adding multiple subfolders - one for each supported language. Finally in each of them add a Resources.resw file (right-click folder, Add > New item..., Resources file (RESW)). The result will look like this.
Then inside each RESW file add some resource. I usually put in AppName, as I often localize it as well:
After this, the app should pick up on the fact that you support the given language and display localized.
I finally found the answer to my problem, it was just as simple as it usually is when it comes to such errors.
It turns out that all bootstrap projects like the UWP, iOS and Android sub-projects also inherit the Assembly Neutral Language from the parent project, no matter what configuration is defined in the specific sub-project (at least thats how it works for UWP). I just noticed that I can set an Assembly Neutral Language within the settings of my .Net main project, and as soon as I did that, all the warnings disappeared and my UWP app ended up showing english text instead of the system default german one. I can also switch back to german now, even though I still have to restart the app to get all frames translated, but thats not that big of a problem, I can work on fixing that up later on. Here's the link that finally got me covered:
Xamarin.Forms: Localization of the UWP app
Thanks for your amazing help anyway :).

Localization not properly working

I wrote a small application that shall automatically start in either English or German. English is default and German would be used on a German Windows.
The problem:
The German strings aren't loaded on a German Windows, although the program recognizes the client as German.
Explicitly setting the current culture (in code) to German however will result in a German UI.
What I did:
I followed this guide: http://www.codeproject.com/Articles/299436/WPF-Localization-for-Dummies
I created copies of the Resources.resx in the Properties section
Now there are: Resources.resx, Resources.en-US.resx, Resources.de-DE.resx
I put all Strings in the three Resources files. Resources.resx and *.en-US.resx contain the exact same strings
The name of the strings are consistent, Access Modifier on all files set to Public
File Properties of the Resources: Build Action: Embedded Resource; Do not copy; Custom Tool: PublicResXFileCodeGenerator
Code:
private void InitLocalization() // Executed on startup
{
if (CultureInfo.CurrentCulture.Name != "de-DE") // only use german on german systems. Otherwise use english
{
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.CreateSpecificCulture("en-US");
}
else
{
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.CreateSpecificCulture("de-DE");
}
rm = new ResourceManager("SaveEye.Properties.Resources", Assembly.GetExecutingAssembly());
// Manually assign the correct text to XAML-elements
_SettingsTextBlock.Text = rm.GetString("Settings");
_StartWithWindowsCheckBox.Content = rm.GetString("Startup");
_SaveButton.Content = rm.GetString("SaveAndClose");
}
Basically, I just want to make sure that either the Resources.en-US.resx or Resources.de-DE.resx is loaded from the ResourceManager.
In the rest of the code I grab the Strings I need with rm.GetString("").
This works just fine with English. I can even manually set the CurrentCulture to de-DE at the beginning of the InitLocalization-Method and then all German strings are loaded. But as I said, on other systems that are de-DE by default, the German strings are not loaded.
What am I doing wrong?
Regards
It is the CurrentUICulture that will affect which resources are loaded, and the CurrentCulture that will affect date and number formatting. A lot of people get these two confused.
You should probably be testing CurrentUICulture if you're trying to either set, or determine what string resources to retrieve. There is a big difference between a machine running with German date and number formats (CurrentCulture) and a machine running with a full German Language Pack installed and activated (CurrentUICulture).
Your sample code is using CultureInfo.DefaultThread(UI)CurrentCulture which will control the behaviour of all new threads created but this will default to whatever the main thread is using anyway so if it was already in German, you shouldn't need to explictly set it as such.
You can test the resource loading behaviour by updating your InitLocalization() method and setting Thread.CurrentThread.CurrentUICulture explicitly to "de-DE" to see if the German resources start loading. Then verify that the CurrentUICulture is really set to "de-DE" and not just the CurrentCulture. It's possible the German machine has German date/number formatting (CurrentCulture) but isn't using a German language pack (CurrentUICulture).
EDIT: In response to your comment, if you're already using CurrentUICulture correctly and the machine really has a full German UI but the German resources still aren't appearing, you should check that the German satellite resource file is actually being deployed.
The German resources are contained within a separate assembly named de-DE\MyApp.resources.dll and must reside in the same folder as the executable and stay within the de-DE folder (so you have \Folder\MyApp.exe and \Folder\de-DE\MyApp.resources.dll). The default resources (the ones in Resources.resx stay in the main executable and are the ones the ResourceManager falls back to if no more appropriate resource is found).
You didn't specify how you were deploying the application, but if you run you can use the 'Open in Explorer' (or Dateipfad Öffnen on the German machine) feature of Task Manager to take you to deployed folder of the running application so you can check that the de-DE folder is there (I only mention doing it this way because if you ClickOnce deploy the application it can be hard to find the installation folder).
If you are using ClickOnce and you publish with Visual Studio, you can control the files included during publishing by right-clicking the project and selecting properties then navigating to the 'Publishing' tab. The 'Application Files' button will show you a list of all the files included when publishing. Your de-DE satellite resource file should be set to 'Include'.

How to start a new unmanaged Process with different culture?

I need to run a new Process from .NET application with a culture of my choice.
This process will run an external unmanaged 3rd party exe.
How can I make it run with a specific culture?
Note that I don't want to change culture for thread which can be done like this:
System.Treading.Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR")
I dont want to setup a default culture for new threads which can be done by reflection or:
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("fr-FR")
If executable is .Net application you can create your own proxy executable, set culture the way you want and "start" the other application by loading its executable via reflection and launching corresponding Main method. You'll likely need to understand configuration and assembly loading APIs to get all working correctly if it does not magically start on a first try.
For native apps I'm afraid you'd need to change regional settings or for some applications their own configuration.

debugging compiled .net application

1- i designed application which has two interfaces (english, arabic), the user can choose the UI language at runtime, and the change will be seen after application restarted. i store the selected langauge in app.config.
2- from the form constructor i change the CurrentUICulture to the selected lang by this code:
Public.ArabicView = UmAlQuraCalender.Properties.Settings.Default.ArabicView;
if (Public.ArabicView == true)
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("ar-SA");
else
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");
3- the application work without problem in my development machine.
4- when i test the application in another machine, only one user interface is working (english), if i check the other language and restart the application nothing happen the interface remain english although the arabic local is installed.
i use two radio button: one for arabic and the other for english, and inside the click event i change CurrentUICulture to the selected language inside this code:
private void rbArabic_Click(object sender, EventArgs e)
{
Public.ArabicView = rbArabic.Checked;
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("ar-SA");
UmAlQuraCalender.Properties.Settings.Default.ArabicView = Public.ArabicView;
UmAlQuraCalender.Properties.Settings.Default.Save();
MessageBox.Show("UI Language will be changed after application resart");
}
private void rbEnglish_Click(object sender, EventArgs e)
{
Public.ArabicView = rbArabic.Checked;
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");
UmAlQuraCalender.Properties.Settings.Default.ArabicView = Public.ArabicView;
UmAlQuraCalender.Properties.Settings.Default.Save();
MessageBox.Show("UI Language will be changed after application resart");
}
5- also how i can debug (trace the source code) in the test machine to figure out the problem?
if any one can help me i will be thankful.
Why can't you use VS to debug on your test machine? Use Remote Debugger tools if test machine doesn't have VS installed.
Another alternative is to use Reflector. Reflector Pro will allow you to step through your code when you don't have source code access.
thanks my friend for your help
i installed VS in the test machine and i discovered that i forget to include the localized DLL which VS Created in the debug directory.
when i included this directory which has the localized DLL for arabic language the problem was solved.
thanks for the every one reply to my question

Categories