Form resources usage for programmatic strings? - c#

Had a basic WinForm question: By default a resx file is created for every form or user control (along with the designer.cs). This resx works fine for all the controls and the text added to the controls via the UI.
I was wondering if I could use the same resx to add strings which have to be used programmatically and based on conditions, attached to the controls? Will the resx get overridden in any case and this custom strings be removed?
What is the best practice to follow in this case?

There's a strange problem with the string resources in the Resources.resx file. There's no obvious way that I ever discovered how to create a new resource table for another language with the IDE. It can be done by hand though. Follow these steps:
Project + Properties, Resource tab,
add the strings you want to use in
your program
Start Windows Explorer and navigate
to your project's Properties folder
Copy and paste the Resources.resx
file
Rename it to the culture you want to
use. For example:
Resources.fr-FR.resx
Back to VS, click the Show All Files
icon in the Solution Explorer window
Expand the Properties node, the new
resource file should be visible
Right-click it and select "Include
in project"
Select it, in the Properties window
set Custom Tool =
"ResXFileCodeGenerator"
Verify that Build Action is set to
"Embedded Resource"
Build your program. You should get a new folder in your project's bin\Debug directory with the satellite assembly, named projectname.resources.dll. This satellite assembly contains both the localized strings and any localized resource from the forms. Test that it works with code like this:
public Form1() {
System.Threading.Thread.CurrentThread.CurrentUICulture =
System.Globalization.CultureInfo.GetCultureInfo("fr-FR");
InitializeComponent();
textBox1.Text = Properties.Resources.String1;
}

The auto-generated ones get overwritten (I'm using 2005), so no I would not use the same file. I would suggest creating a separate area in your project for .resx files like this. Usually I create one .resx per form, matching the name, of course.
Edit: Here is a more detailed answer I gave recently to organize the file/folder structure when localizing your app with .resx files.

Or you can try this:
Add this code in main();
using System.Resources;
ResXResourceWriter rw = new ResXResourceWriter("Resources.de-DE.resx");
rw.AddResource("String1", "de");
rw.Close();
...
repeat for more files
Go to the bin directory and move the XML (resx) file(s) to perhaps the property folder.
Go to Step 5-9 above in nobugz post.

Related

How to localize WPF Application with RESX files in subdirectories

My team has some WPF projects written using XAML. We recently added a large number of local specific RESX files to each project. In order to keep things tidy, I was asked to store these files in [Project]>Localization>[locale]
Now, when we run the application on a non en-US locale, strings are pulled from the appropriate RESX file. We've tried to update Namespaces in the properties of the RESX files, as well as setting them to Public so that a resulting Designer is created.
The only way that we can get things to work is by moving the locale RESX files directly into the Project's Properties directory.
Is there anyway to update the XAML to search for locale RESX files in a subdirectory?
EDIT
XAML Codebase and Solution Explorer
I've attached an image of the problem for clarification.
Some code changes that we've tried:
1) We attempted to set the Namespace for xmlns:res="clr-namespace:[PROJECT].CoreUI.Localization"
2) Attempted to set the Namespace to xmlns:res="clr-namespace:[PROJECT].CoreUI"
3) We've also attempted to change the Custom Tool Namespace of the RESX file to match the current namespace My.Properties as well as CoreUI.Properties
We've also confirmed that the Access Modifiers for both Resources.resx and Resources.ru.resx (for example) are set to Public and that both Build Actions show "Embedded Resource"
Thanks for the help!
My colleague actually stumbled upon another post in here that we somehow missed during out countless searching:
Put translated resx files in a different folder in Visual Studio?
In short, we were thinking about making the project search down into subdirectories for the RESX files. In actuality, we need the RESX file to search for the already created designer further up in the chain.
<ItemGroup>
<EmbeddedResource Include="Localization\ar\Resources.ar.resx" >
<ManifestResourceName>$(TargetName).Properties.%(Filename)</ManifestResourceName>
</EmbeddedResource>
Entering the ManifestResourceName appears to tell the RESX file that we already have a Designer created and not to make a new one.
Thanks again for everyone's help!
You've to first include your folder in the namespace and mention project name like:
xmlns:resx="clr-namespace:Prism_Modules.MyResources;assembly=Prism_Modules"
Also you must include:
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Finally, you should call values in resources as:
<dxb:BarButtonItem x:Name="menuFile" Content="{x:Static resx:Resources.mnuFile}" }" />
When moving .resx file to another folder/project, the .Designer.cs file that exposes resource members will still point to the old namespace and also Visual Studio will place it outside the .resx file.
To fix this, I simply deleted the generated .Designer.cs file and then after opening and saving .resx file, the Designer.cs file got generated with proper namespace and I could normally refer to it even from other projects. To generate .Designer.cs file for resource languages/files that have none just open .resx file and use the Access Modifier combobox and VS will create it for you.
I hope I was of any help.

Adding new strings to resource.resx not reflecting into Designer.cs

I am adding two new strings to our resource.resx but these newly added resources(strings) are not reflecting into the auto-generated Designer.cs file. I have rebuilt the project and also tried clean+build the project but no luck!
I have other resource files to support various international languages where also i need to these two new string resources
Please suggest.
Make sure the Custom tool property for your resx file is set to ResXFileCodeGenerator or PublicResXFileCodeGenerator. The .Designer.cs file should be regenerated every time you modify the resx file; if it's not, you can force it by selecting Run custom tool in the context menu for the resx file.
Right click the resx file in solution explorer, select "Run custom tool".
This generated/added the code in the .designer.cs file for the new strings in the resource file for me.
When editing the resource file in Visual Studio, check the Access Modifier drop down is not set to No code generation.
If it is then select the appropriate option from the drop down - Public or Internal.
You just need to :
& then do :
Tada! You got your new Designer.cs !
In JetBrains Rider, I had to right-click the resx file and select "Generate resources"
I had the same issue when I renamed my [YOUR_RESOURCE].resx file into [YOUR_RESOURCE].en-US.resx. It seems [YOUR_RESOURCE].Designer.cs no longer auto generates any code after you renamed the default [YOUR_RESOURCE].resx format to something like [YOUR_RESOURCE].en-US.resx
I came across this solution and it worked fine for me.
Close the solution
Edit the project file LastGenOutput tag for the resource file
Open the solution again and try
If you're looking to automate this process you can use resgen
resgen c:\development\test\properties\resources.resx /str:csharp

How to use shared resource file between projects in one solution?

I have a problem with resource files.
I have a solution with two projects. The first project contains ImageResource.resx file with the images that I use. Every Form in this project can access this file from the designer. But I can see in the designer ImageResource.resx file to use it from second project (Reference to second project is present).
I have added the ImageResource.resx file as a link to my second project. And I saw it in the designer! But when I use an image from this resource in the second project Visual Studio modified my original file (It sets the namespaces, and other..) and my solution breaks. Also Visual Studio tells me that ImageResource.resx is present in two dll's first_project.dll and second_project.dll
Can anybody help me with How to correctly use shared resources between projects?
The correct way to share resources is to create a global shared project. Create a new Project with the name Resources:
Next we must add some resources (such as icons) to the project. Do this as usual. Go to the projects setting, select tab Resources and Add Existing File… to the project. We can see that the icon is added to the project and was copied to the local folder:
Next step consists of adding this icon to the other project(s). Note the important difference, you need to add this icon as a link!
Adding as a link avoids the resource duplication. Create a new Project within the same solution and name it e.g. Main. Create some folder in this new project, naming it Resources (the logical name for our purpose). Then right click on this folder, select Add Existing Item… and choose the image file from the shared project folder. Make sure to use Add As Link here! If done correctly the icon of the newly added file will look slightly different (see below):
Added resource's icon must look like this
Now we must set the Build Action for this file to None. For this select the file and go to the Properties Window. There choose None for Build Action. We need to do this to avoid embedding this icon into the assembly:
Finally we need to add the linked files to the Resources of the corresponding project. Open the project Properties for the project where we just added the files. Choose the Resource tab and drag the linked file there:
These are the five simple steps you must perform to share icons between projects. You might ask "What are the benefits of this?" The benefits are:
We store resources in one place, and
It is easy to replace an icon with a new one.
This didn't work for me and I found another (VS2015+) approach.
See https://stackoverflow.com/a/45471284/4151626
In short, the shared project is directly included into the peripheral project. Thus, even though the IDE does not support <Resource> elements in the shared project. <Resource> elements can be added to the shared project, via a text editor. They are then incorporated into the peripheral project during the build.
(Apologies for the hyper-link. I would just repost the answer for clarity, but the stackoverflow editors crack down on this, deleting duplicate answers to save you from ???.)
Can you use a symbolic link to share the file into multiple folders?
windows:
mklink linked_location\ImageResource.resx original_location\ImageResource.resx
C:\Users\preet>mklink
Creates a symbolic link.
MKLINK [[/D] | [/H] | [/J]] Link Target
/D Creates a directory symbolic link. Default is a file
symbolic link.
/H Creates a hard link instead of a symbolic link.
/J Creates a Directory Junction.
Link specifies the new symbolic link name.
Target specifies the path (relative or absolute) that the new link
refers to.
If a resource file is really shared between projects, you should put it in a shared project.
Solution 'Sample'
Project Sample.Data
Project Sample.Business
Project Sample.UI
Project Sample.Resource //shared resources are put in a shared project
You can't see the resource if it is not public, and it is default set to "Friend". You can set the "Access Modifier" in the Visual Designer (upper right-hand corner).

resources at class library project

I want to add some xml file as resource to my class library project .
Any idea how to do so , and call it later?
In windows application i would do it like ClassLibrary1.Properties.Resources.file.xml
But here it didn't worked any idea how i do it here ?
This article explain how to use embedded resources in C#.
It boils down to
At 'Design time': Add file to project and mark it as an embedded resource (properties)
At 'Run time': Get instance of Assembly that holds the resource you want, then get that resource out as a stream.
var assembly = Assembly.GetExecutingAssembly();
var stream = assembly.GetManifestResourceStream("fully.qualified.name.of.the.resource");
If you're struggling to work out the fq name of the resource, a lazy way is to use the reflector to open the assembly that holds it. Go to the Resources folder, right click on the particular resource and choose 'Copy'.
In windows application i would do it like ClassLibrary1.Properties.Resources.file.xml
By default, the class generated to access the resources is internal, so you can't access it from another assembly. If you need to make it public, select the .resx file, go to its properties, and change the Custom tool property from "ResXFileCodeGenerator" to "PublicResXFileCodeGenerator". This custom tool generates public classes, which should solve your problem.
Right Click on Class Library Project, select properties, on tabs on the left choose Resources section.
Since your .resx files ares not recognizable by Visual Studio, add a new one.
It will create a new .resx file.
Copy all your original content and paste into new one. Then you can delete old one.
That's it!
You would need to repeat same process for your all supported languages.

.NET Projects: Where do I put .resx files?

Where do I put .resx files? Sometimes I see these files under Properties folder. Is there any design guideline about it?
Thank you!
Depends on what you are doing.
For example a web Application they go in App_GlobalResources or App_LocalResources folder.
For other projects I would create a Resource folder and put them there.
In a WinForms app, the .resx file associated ot a class (form, user-control,...) is stored side-by-side with the source code (e.g. C#) file. In addition, a global .resx file is created in the properties to let you store global stuff such as messages, pictures,...
If you right click your project in VS solution explorer you should have an option "Create folder" this will then have a list of folder you can create. App_GlobalResources and App_LocalResources is another.
For project wide strings and resources add the contents to the App_Global forlder. for form specific resources add the content to the App_Local folder instead.
Have a look here for more information

Categories

Resources