I'm working on a silverlight application where I'm checking the url to see if the user is trying to access the "Spanish" version. My methods work fine on Windows 7 and Windows Vista but I'm getting a crash throwing an Argument_InvalidCultureName exception when a user of Windows XP tries to load the application. Can someone look at the code below a give me perhaps an idea of what's going wrong and how to resolve it? Thanks in advance.
if (HtmlPage.Document.DocumentUri.Query.Contains("Spanish") || HtmlPage.Document.DocumentUri.Query.Contains("spanish") || HtmlPage.Document.DocumentUri.Query.Contains("Espanol"))
{
try
{
var culture = new CultureInfo("es-ES");
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
}
catch (Exception)
{
// Do Something
}
}
I changed the logic over to "es" instead of "es-ES" and this worked fine on the XP machines.
Related
I add an application to the Windows store. When I download and run application, everything goes alright, but when some people download and run application, so throw the exception.
Třída není zaregistrována == Class not registered
There is code:
try
{
StoreContext storeContext = Windows.Services.Store.StoreContext.GetDefault();
string[] productKinds = {"Durable", "Consumable", "UnmanagedConsumable" };
List<String> filterList = new List<string>(productKinds);
queryResult = await storeContext.GetAssociatedStoreProductsAsync(filterList);
}
catch (Exception ex)
{
MessageDialog(ex, "UpdateDatabaze");
}
Anyone who know how I might resolve this exception?
I am programming the application with target version Windows anniversary (build version 14393), which supports StoreContext class. When users download application and their Windows was not anniversary but only windows 10 with build 10586 or lowest build version, then throw exception which is described in the part called question.
I've localized my App in two languages (English and German) with the MulitlingualAppToolkit 4.0. English is the base language, while german is a translation based on the english one.
The translations are stored as resw-file inside folder "strings.en" and "strings.de".
In App.xaml.cs App() I set the culture like this:
Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = GetCurrentCulture();
CultureInfo.CurrentUICulture = GetCurrentCulture();
private CultureInfo GetCurrentCulture()
{
var cultureName = new DateTimeFormatter("longdate", new[] {"US"}).ResolvedLanguage;
return new CultureInfo(cultureName);
}
(I got this quiet weird way to the regional-culture in Windows 10 from this article https://www.pedrolamas.com/2015/11/02/cultureinfo-changes-in-uwp/ since I recognized that CultureInfo.CurrentCulture and CultureInfo.CurrentUICulture are always "en-EN" no matter what i configurate in my machines regional- and language-settings)
To check if PrimaryLanguageOverride works as expected, I added a TextBox by the name of tbTest on my first Page and a button linkt to this event:
private void Button_Click(object sender, RoutedEventArgs e)
{
Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = tbTest.Text;
Frame.Navigate(this.GetType(), 0);
System.IO.File.AppendAllText(System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "PrimaryLanguageOverride.txt"),
DateTime.Now + " - Actual PrimaryLanguageOverride:\n " + WIndows.Globalization.ApplicationLanguages.PrimaryLanguageOverride);
}
catch(Exception ex) { Helper.Log.LogUnhandledError(ex); }
}
Now comes the weird stuff:
When Debugging, or executing the App from my Development-Machine, everything works as expected, but when I make an appx-bundle and install it on another (Windows10-Desktop) device, the App does not recognize the its Language.
In my situation, the device is set to german, regional as well as its language. Also when using the test-procedure, it defenetively gets the string I set , as long as it's a valid culture-name, e.g.: "en-EN", "en-US", "de-DE", "de", "en" (all of these are working fine on my development machine) if it's an invalid string, I get an exception, with a log-entry in my unhandled-error-log. It refresh the Page, and even writes the new-set language in my PrimaryLanguageOverride-Log, but it doesn't change any text I did translate.
So my question is, is this a common Issue (since I recognized in UWP/Win10 the culture-system is a little messed up) or do I use the wrong procedure to override the App-Culture?
This is an issue with AppXBundling. When Installing bundles, it checks with the current OS for the installed Language packs and copies the relevant language resources from the bundle and omits the other language files. The objective of a single bundle is to copy necessary resources and build the application and therefore the languages which are not in the system are considered irrelevant. As a fix you could stop generating single bundles and create package for each CPU architecture. Check this for more info
We have an application based on ASP.NET MVC 5.2 and we're using localization with Resource files (resx), Localization.resx, Localization.de-DE.resx, Localization.de-AT.resx, Localization.en-US.resx, Localization.en-GB.resx... etc etc which produces satellite assemblies.
We determine the language first by cookie and then as fallback by browser header information. Works very well.
protected internal void Application_BeginRequest(object sender, EventArgs e)
{
HttpCookie cultureCookie = httpRequest.Cookies[CookieName.Culture];
string cultureName;
if (cultureCookie != null)
cultureName = cultureCookie.Value;
else
{
if (httpRequest.UserLanguages != null && httpRequest.UserLanguages.Length > 0)
cultureName = httpRequest.UserLanguages[0];
else
cultureName = null;
}
cultureName = CultureHelper.GetImplementedCulture(cultureName); // returns default on null
Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureName);
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
}
We use the Localization.PropertyName directly in views like
<div class="text-right">#Localization.Header_TopMenu_Service</div>
This works fine so far but after around 60-80 minutes we have a problem on just one environment (productive): the localization does not work anymore.
It effects just these textes! The thread still has the correct culture!
Internally we still work with the Thread.CurrentThread.CurrentCulture values like for static texts, calculations of taxes... and this still works.
So some parts of the application are now in english (static stuff and logic stuff like calulcation of taxes) and all elements from the Localization resources are in german (default).
Why can this happen?
We thought there is something with race conditions but we've embedded some information to the output and everything is fine:
<!--
CurrentCulture == 'en-GB'
CurrentUICulture == 'en-GB'
ManagedThreadId == '25'
IsThreadPoolThread == 'True'
-->
Even if the language in this output is en-GB the localization outputs our default ,which is german.
When we restart or recycle the application, the localization works for again 60 minutes.
We also tried to set the language via Filter or in the Controller: same issue.
What can be the reason for this weird issue?
Wrong implementation? Something with satellite assemblies? IIS issue? system configuration?
Because everything works fine on the test stage and this effects just occur on the prod (no new build, just a copy) I think it is something with the system.
Problem is solved.
It was a race condition which set Localization.Culture (or in other implementations Resources.Culture) globally to a specific language.
The problem was, that this should be null in web applications. If this is null, ResourceManager.GetString() uses Thread.CurrentThread.CurrentUICulture for localization.
If Localization.Culture is not null the ResourceManager does not care about Thread.CurrentThread.CurrentUICulture.
I need a few tips on how to change application launguage. (Windows 8.1)
So here is my code that I put under OnLaunched method, It gets the current preffered launguage. I am using Multilingual app toolkit. I have all the resource files.
But I have no idea what to do next.
var rootFrame1 = new Frame();
rootFrame1.Language = Windows.System.UserProfile.GlobalizationPreferences.Languages[0];
You have to set current CultureInfo properties in this way:
var culture = new CultureInfo("en-US");
Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = culture.Name;
CultureInfo.DefaultThreadCurrentCulture = culture;
CultureInfo.DefaultThreadCurrentUICulture = culture;
Some resources point to placing this code in Page constructor to react to your default language being changed
ResourceContext.GetForCurrentView().QualifierValues.MapChanged +=
(IObservableMap<string, string> sender, IMapChangedEventArgs<string> e) =>
{
ResourceManager.Current.DefaultContext.Reset();
};
Also, worth mentionig is that preferable method for changing language is setting Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride property.
This works not so good for Windows Phone 8.1, but some claim that this solved their issue on Windows 8.1.
I have a C# application that needs to set the Windows language bar to english or at least back to the default setting. I know I can set the InputLanguage of my own application but I need to set the input language for Windows in general. This can be done manually using the language bar, but I need a way to do it programmatically. Is there a way to do this?
I ended up doing this:
Process[] apps=Process.GetProcesses();
foreach (Process p in apps)
{
if (p.MainWindowHandle.ToInt32()>0)
{
NativeWin32.SetForegroundWindow(p.MainWindowHandle.ToInt32());
//send control shift 2 to switch the language bar back to english.
System.Windows.Forms.SendKeys.SendWait("^+(2)");
p.Dispose();
}
}
I haven't done this since Windows XP was in it's childhood, so you may want to check if the language support is still based on the same principles. It is all Win32, so they need to be imported for C#.
First, read on MSDN the pages about keyboard input:
http://msdn.microsoft.com/en-us/library/ms645530%28VS.85%29.aspx
GetKeyboardLayoutList tells you what layout are installed
LoadKeyboardLayout loads a new input locale identifer.
ActivateKeyboardLayout sets the current language
A much better approach to this is this:
//change input language to English
InputLanguage currentLang = InputLanguage.CurrentInputLanguage;
InputLanguage newLang = InputLanguage.FromCulture(System.Globalization.CultureInfo.GetCultureInfo("en-US"));
if (newLang == null)
{
MessageBox.Show("The Upload Project function requires the En-US keyboard installed.", "Missing keyboard", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
else
{
InputLanguage.CurrentInputLanguage = newLang;
}
See full post on this: Language Bar change language in c# .NET