In the Shared project of my universal app I have two folders inside of the Strings folder, en-US and sv-SE. Inside of both folders I have Resources.resw files. These contain the strings for my app.
When I run the app I'm able to see the strings, mapped using x:Uid, but I'm not able to see the string when using the designer designer.
Moving the English Resources.resw file to the root of Strings generates an error, telling me there is no Resources.resw file for the default language (en-US). Also, it does not make the strings appear in the editor.
Is it possible to make the resources from a *.resw appear in the designer?
I'd be surprised if your other project actually works as you think. Strings in *.resw files are not automatically understood for the designer. Given this:
<TextBlock x:Uid="MyWelcomeMessage" />
In the designer would show an empty TextBlock because the designer doesn't get the localized resource at design-time. The recommendation is to use some placeholder values for those items you are localizing with resources.
Related
I have an application that I am developing that is made with Window Forms. For localizing all my Labels, ToolStripMenuItems, Buttons, etc I use resx resource files. Specifically to localize my application for German, I open my Main.en-CA.resx file in winres. I then go through all the terms found in the form and change them to their German translation. I then save the file to Main.de-DE.resx. I now have a Main.en-CA.resx file and a Main.de-DE.resx file. In my code I then only have to change the current culture to whatever language I want and apply the change to all my Labels, Controls, Buttons, etc. For example something like this:
System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(language);
// Must re-apply resources after changing the culture
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Main));
resources.ApplyResources(this, "$this");
foreach (Control c in this.Controls)
{
resources.ApplyResources(c, c.Name);
}
This seems to work great for all Labels etc that do not change. I do however have entries that are changed. For example I might have a dropdown ComboBox that is filled with the entries: "Apple", "Banana", "Orange". Or I might have some error messages: "Missing Input", "Cannot find xml file" that are only sometimes displayed. Now I suppose maybe for the error messages I could just have Labels and selectively change their visibility depending on whether they need to be shown, however for the dropdown ComboBox these entries might change depending on say which file the user loads.
I am wondering then, is there a way to store these entries in the resx files and then access them from my code. I tried opening the resx files and adding them manually (i.e. without using winres) but attempting to do this resulted in the warning:
You are trying to edit a resource file that is a part of another project item (such as a form or control). Editing this item could corrupt the project item, and you will have to recover it by hand. In addition, changes made to this resource file may be lost if further changes are made to the project item.
Do you really want to edit this file?
This sounded like a bad idea so I didn't try that any further. Additionally I am not sure on how I would access the terms in the file manually. I am very new to windows forms and resource files (this is my first time using them) so I realize this might be a simple question but I have had trouble finding information on how exactly to do this.
Ok as it turns out I have uncovered how I can achieve what I am looking for. Ok from the SO post I can access any strings stored in the files Resource.resx by the code:
myLabel.Text = Properties.Resources.MissingController;
where MissingController is a key (i.e. Name) in the file Resources.resx.
Therefore all I need to do is add additional resource files such as Resource.de-DE.resx in the case of German and fill in the translations (i.e. the values in the resource file) corresponding to the same keys (i.e. the names in the resource file).
The Resources.resx file looks like:
and the Resources.de-DE.resx looks like:
As mentioned in the question I had already created some resource files for translating my forms but I had used winres. Whereas they had been located under my Main.cs [Design] file, the Resources.resx and Resources.de-de.resx are located under Properties. Because I had used winres to make my resx files I think that meant I was not supposed to manually edit them hence the warning it gave?? I'm still not 100% sure about this.
Regardless I can now just manually add terms to my Resource.resx file as well as create different versions of this file for different languages and the localization will work. When right clicking on Properties and going Add->New Item and then selecting Resource, if you do not see the Resource file type as an option (as happened to me) then that might mean you need to add the development tools that did not get installed with your version of visual studio. You can achieve this by just running the visual studio installer again and clicking modify and adding the .NET development tools.
I am developing a WinForm application which require localization.
If I try to set the Localizable property of the form to True and set the text for all the languages then every thing works fine.
What I want is to maintain all languages resource files in a separate folder (one file for each form).
-Project
-Resources
-Language
frmFirstForm.en-US.resx
frmFirstForm.en-GB.resx
frmSecondForm.en-US.resx
frmSecondForm.en-GB.resx
frmFirst.cs
frmSecond.cs
In my resource file I have defined all strings as follows:
**Key Value**
lblName Name
lblAddress Address
.....
The key is my control names, I will also keep form specific strings in the resource files. Now the issue is when I compile the solution, it do generate the language files but while running the application it just displays the default values. I don't even know whether the localized resource file is loaded or not. Also, though I have specified two separate form files but while compiling the system is generating only one single resource file per language for a project (means no separate resource file for FirstForm and SecondForm).
Is there any way where the form controls are changed as per the specified localized thread?
I have already added the following line in my main application Program.cs file:
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-GB");
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
Please suggest me how to proceed with this..
#Ashish : this is what you want exactly!!!!
This problem has left me scratching my head! I'll try to be as concise as possible.
On a high level:
The problem is that although the project works fine and the code looks good to me. Whenever I edit and build certain forms, Visual Studio re-writes the *.Designer.cs files in a way that is very undesirable.
I quite confident that these *.Designer.cs files have not been edited (especially the auto-generated portion) in the past.
In more detail:
Our project uses custom controls, some which inherit from PictureBox. On the forms where these controls are present, if I view the *.Designer.cs file, I either see that the Image property is not set, or the Image property refers to an image stored in the project's resx file like below, which is all well and good.
this.customButton.Image = global::MyProject.Properties.Resources.buttonImage;
However, if I simply modify this form by adding another control (drag another button onto the form) and build the project, Visual Studio extensively edits the MyForm.Designer.cs and MyForm.resx files, even for the existing controls on the form that were not touched. It seems that it embeds all the images needed by the controls in the MyForm.resx file and then refers to them in the MyForm.Designer.cs as follows:
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MyForm));
this.customButton.Image = ((System.Drawing.Image)(resources.GetObject("customButton.Image")));
This is obviously not what I want. Why does Visual Studio want the form to use a local resource now, instead of the one embedded in the project's Resources.resx file as it was happy to do before the form was modified? If I go to the designer, view the properties of the customButton, and try to set the Image property to the image in the project resource file, it allows it, but on the next click, it will immediately revert back to the local reference embedded in MyForm.resx.
Any ideas why this is happening?
I figured this out with the help of another question
Basically, even though I've seen several recommendations to centralize your application's resources in a single assembly, this appears to be a bad choice. The VS Designer just doesn't like having to access resources external to the current assembly and while it can do so, it will also change your code to bring those resources in the current assembly by embedding them, thwarting your efforts to keep the resources in a single assembly.
I basically had to go back to keeping resources in the assembly the uses them.
I'm using resource files with strings for internationalization support in a C# WPF application.
I have the files under the folder Strings and I access it in code with something like Strings.MainWindow.SomeStringId.
Problem is that as the translation files start to grow, that folder beings to contain a huge number of files and it gets easier to screw the pooch by making changes to the wrong file, ending up with German sentences on Russian files.
I'd like to set up the Strings folder to have a sub folder for each locale. I tried changing the "Custom Tool Namespace" option under Properties but to no avail.
Basically this is how it's laid out: http://d.pr/ViwI
And this is how I'd like it to be laid out: http://d.pr/7wsu
Try another approach.
Instead of Resources/Locale/Bunch_Of_Resources use Resources/Bunch_Of_Folders_One_for_Each_Context/Resources(pt-Pt, en-EN)
It is not perfect. But it will help you organize.
You need to create the folders manually, then drag/drop your files into the correct folders.
You can then go through each *.Designer.cs file and change the namespace from "Strings" to "Strings.[locale]".
I have a small application, which includes a resource (.resx) file.
This file contains an icon, and a string, which is used by another application. The icon will be displayed on a button, and the string is the mouseover text.
What I want is in some way, to add strings in multiple languages, without having to crate additional .resx files.
Is there any way to do this?
I am writing my application in C#, and I would like to keep it in the .Net environment, and keep the resource managed.
Any help is appreciated - thanks in advance.
This would be an abuse of the way localization works in .net. It is meant to work like that - you create a resource file AND localized resource files containing .languageid. in the middle.
Of course, you can code your own "reader" method which appends some prefix to the resource name and then save several values in your file (for instance lv-formtitle, en-formtitle, ru-formtitle etc) , but - why?
If the problem is you do not want to copy the icon to several resource files - make 2 resources, one for icons, other for strings. Localize only the one containing strings.
Of course you can add more strings to your resource file, but they will have to have a different name and managed in your application that way (for example - "title" and "title-he") .
Doing this however, defeats the point of resource files.
You should use additional resource files, one per locale, even though you don't want to...