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!
Related
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;
We support a few languages, for which we have different traductions stored in the Properties of our website. Here are some of them:
Resources.es.resx, Resources.fr.resx, Resources.resx (which contains english traductions).
Sometimes (like 3 times in the last year), our website becomes in french for ALL our english users.
We implemented some logging to try and understand what is happening. The last time the bug occured, what we observed is that when trying to get a sample string from our Resources.resx file, the result was from the Resources.fr.resx file. This was the case for all our users.
This is how we obtained that string:
string englishResource = Properties.Resources.ResourceManager.GetString("Add", new CultureInfo("en-ca", useUserOverride: false));
It's as if the Resources.resx was not found and the language defaulted to Resources.fr.resx.
When the website works properly, the returned string is always from Resources.resx, as expected.
Does anyone know why this is happening and how to fix it ?
EDIT:
here is what we get for an english user when the website is displayed in french:
{
SessionCulture: “en”,
UserLanguages: [
“en-US”,
“en;q=0.9”,
“fr;q=0.8",
“es;q=0.7”,
“ca;q=0.6",
“it;q=0.5”
],
LocalizationSystemLanguage: “en”,
CurrentThreadCulture: “en-CA”,
CurrentThreadUICulture: “en-CA”,
EnglishResource: “Ajouter”,
FrenchResource: “Ajouter”,
SpanishResource: “Agregar”
}
the "EnglishResource" string should read "Add" instead of "Ajouter"
I am developing a SharePoint Online add-in in ASP.NET MVC 5. I have three resource files with three languages that I want CultureInfo to read when the corresponding language code is returned from Shareoint. This is my code:
In the ActionResult Index:
var returnedLangCode= WorkPlanRepo.LangSettings();
if (returnedLangCode!= "")
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(returnedLangCode);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(returnedLangCode);
}
else
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en");
}
The method returning sharePoint Lang settings in my class workPlanRepo:
public string LangSettings()
{
CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
var twoLetterLangName = currentCulture.TwoLetterISOLanguageName;
return twoLetterLangName;
}
I hev three Resource files, Default is English, then I have Swedish and Danish. All Resources are set to Public. In the view I reach for the resource file like this:
#Html.Label(CMP.ArbetsplanWeb.App_LocalResources.GlobalRes.ResetCalender ,new { #class = "btn btn-default", id = "clearCal" })
This is the errormessage:
When settting the CultureInfo error is returned. Wanted to insert img, but the upload tool dosen´t seem to work properly.
Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "CMP.ArbetsplanWeb.App_LocalResources.GlobalRes.resources" was correctly embedded or linked into assembly "CMP.ArbetsplanWeb" at compile time, or that all the satellite assemblies required are loadable and fully signed.
Line 75: public static string ResetCalender {
Line 76: get {
Line 77: return ResourceManager.GetString("ResetCalender", resourceCulture);
Line 78: }
Line 79: }
MissingManifestResourceException: Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "CMP.ArbetsplanWeb.App_LocalResources.GlobalRes.resources" was correctly embedded or linked into assembly "CMP.ArbetsplanWeb" at compile time, or that all the satellite assemblies required are loadable and fully signed.]
System.Resources.ManifestBasedResourceGroveler.HandleResourceStreamMissing(String fileName)
I havent registerd anything in web.config or Global.asax. I now I must be doing something wrong. But cant figure out what. I found diffrent guides and docs on this but cant figure out how to useit properly.
Thanks for all the help!!
Solved it. I was using a ASP.NET folder for my resources. When I changed to a regular folder everything worked.
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
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");