Resources Culture issue in Silverlight 5 - c#

Sorry, in advance for my English, its my first post here.
I've got strange problem with RESX, supported culture in Silverlight.
Its silverlight app with 1 web(asp.net) project which start whole silverlight app, 1 main project(silverlight, sl.Application) and others which are use as modules(silverlight) in main project (solution explorer screen: http://i.imgur.com/A4sHWRI.png?1 ). Sl.Application can invoke other projects and use theirs views as part of the main view placed in sl.application
I tried to add different cultures, so:
I add to my app Supported cultures EN and CS to each cs.proj.
<SupportedCultures>pl;en;cs;de;ru;cz;sk</SupportedCultures>
My Neutral culture is set to PL in assemby info in each project.
[assembly: NeutralResourcesLanguageAttribute("pl-PL")]
Create resx Files for different cultures, here fol PL, EN , CS: http://i.imgur.com/P6woiV2.png
At starting app you can choose which culture you want, by clicking on specific flag.
First lets start with PL - its neutral culture, so i dont have to change anything. Using
var culture = System.Threading.Thread.CurrentThread.CurrentUICulture;
var test_en = Resources.DrivesBook.ResourceManager.GetString("AdresArrival", culture);
I checked that culture is set to PL and resource (DriveBook.resx) return correct string in PL
Next:
Let's try EN, Start App, choose EN flag - change culture to en-GB.
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-GB");
Using same code as above, returns me EN string so its still good
The best begins when i tried to use CS.
Same as above, I choose CS flag - change culture to cs-CZ
Using in SL.Application
var culture = System.Threading.Thread.CurrentThread.CurrentUICulture;
its returns me cs-CZ culture so I thought that there will be ok, when I call
var test_cs = Resources.DrivesBook.ResourceManager.GetString("AdresArrival", culture);
it returns me string from CS culture, still all works fine, so where is the problem?
When i tried same in others projects (from Common or Modules folder), here comes magic :)
Same as above
Using in Modules.Auxiliary
var culture = System.Threading.Thread.CurrentThread.CurrentUICulture;
still OK, we got cs-CZ culture, seems to works fine, but
var test_cs = Resources.DrivesBook.ResourceManager.GetString("AdresArrival", culture);
it returns me string from PL culture, not from CS
Summation:
Changing culture works fine, for EN, whole app got EN string from resx files, but when I change to CS, only SL.Application can resolve *.cs.resx file, other projects looks like they have not seen *.cs.resx files. While adding En and CS I d exactly same work for both and EN work fine, but CS not. its there any posibily that there is some property in cs.proj that limit max supported cultures in current project, or do you have any idea what I did wrong?

ok, I solved it by myself
Problem was in my custom framework, which was responsible for differential loading resources. Resolving dll only via filename, not included filePath (here folder cs, en for different cultures) caused overwrite files depending on the lexical order, so CS.resx was loaded firstly, but later I load EN resource and overwrite cs.resx. This framework starts working when comes to loading main xap. Main sl project works, cuz it was loaded earlier, e.g. for loading screen so resource.cs.resx was loaded when my framework couldn't corrupt it

Related

Windows Forms Localize resx without changing Culture

I know it's easy to localize Windows Forms App: set Localizable=True, change Language and set text in Controls for every Language. This information saves in resx-files and application will automatically select the required file. Great!
I know about disadvantages of this solution (you need to rebuild the app if there a typo, it's impossible to change language in runtime, etc), but it's not a problem for me and "resources" is the simpliest, built-in solution.
But this mechanism uses the property Culture of app's thread.
My app is the part ("plugin") of the bigger application and works in the same Thread.
The main application is multilingual too, but it doesn't use Culture to change interface's language. I can change the thread's culture globally, but it crushes the main app's interface.
So my question:
is it possible to manually set the resx-localizable resurce file that will be used? Not based on Culture, but, for example, on some variable in my app:
if (this.Language == "fr")
this.Resources.Add("Form1.fr.resx");
else
this.Resources.Add("Form1.en.resx");
Or something else.
Thank you!
My sandbox:
https://github.com/Tereami/WindowsFormsTestLanguage
The built resources file has a property ResourceManager that's used to return the desired content. This has an overload with a CultureInfo parameter. You can use it to request resources in individual languages:
var desiredCulture = new CultureInfo("en-us");
var text = MyStrings.ResourceManager.GetString(nameof(Resources.ExitMessage), desiredCulture);
If you want to set the culture globally for your resource file, you could also set it through the corresponding property:
var desiredCulture = new CultureInfo("en-us");
MyStrings.Culture = desiredCulture;
var text = MyStrings.ExitMessage;

Why does my localization only work on my Development-Machine?

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

Localization using satellite assembly returns default language

I'm trying to localize my application with satellite assemblies.
I tried to follow this blog: http://msdn.microsoft.com/en-us/library/21a15yht(v=vs.110).aspx
I've created two files, one file has the name "Global.resx" with an english string, and the second file has the name "Global.nl-NL.resx" with a dutch string.
After this i created a .resources file from the dutch file with resgen. With al.exe I made a .dll called LocalizationLab.resources.dll and i stored it in the folder: /bin/debug/nl-NL.
in my application i set the CurrentUICulture to nl-NL. and i call the string with Global.test. See underneath
Thread.CurrentThread.CurrentUICulture = new CultureInfo("nl-NL");
Console.WriteLine(Global.Test);
This returns the english string instead of the dutch one. When i debug and take a look into Global and watch the resourcesets the resourcemanager is using, I see three resourcesets: "nl", "nl-NL" and "" all three of them have english string values in them.
Can anyone tell me what i'm doing wrong?
Thanks in advance.
I eventually found out what the problem was,
The problem was that the generated code for initializing the resourcemanager was looking like:
ResourceManager rm = new ResourceManager("LocalizationLab.Global", typeof(Global).Assembly);
After some puzzling i found out that it should be this:
ResourceManager rm = new ResourceManager("Global", typeof(Global).Assembly);
which worked!

French localization file doesn't seem to be working

I have a localization file that I created that works fine, I called it:
Labels.resx
Now in my global.asax.cs, if a particular querystring value is present, I change the language to french:
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-CA");
I created a new resource file:
Labels.fr-CA.resx
I just put a single entry in the new french local file to test it, now when the controller action executes, while debugging I can see in the immediate window that the culture has changed correctly, but my text label is not in french.
What could the issue be?
The properties for my Labels.fr-CA.resx file are exactly like my Labels.resx file:
embedded resource
PublicResXFileCodeGenerator
(same namespace)
Also, if the given key isn't found, does it automatically lookup the value in the Strings.resx key as a fallback or does it cause an exception?
Try add the folowing line to global.ajax.cs:
Thread.CurrentThread.CurrentUICulture = new CultureInfo("fr-CA");

ResourceLoader and Neutral Language

I want to localize my Windows Store app. I have string\cs-CZ\Resources.resw and I load the strings in C# using ResourceLoader. When I have my system set to cs-CZ locale, the string are returned ok. When I set to to another locale, the GetString method returns an empty string. I have my projects neutral solution set to cs-CZ so what is the problem? How do I make the app always take resources from string\cs-CZ\Resources.resw?
The default locale can be programatically set using
Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = "cs-CZ";
in OnLaunched

Categories

Resources