问题
I'm using C# 3.5 and Windows Forms (and Telerik components if it matters). How can I get a button to use three different pictures (one for idle, one for mouse hover and one for while clicked) in three (or N..) different languages?
When I'm adding a resource image (for example, a JPEG file), I can use the resource manager to get it and everything is great. I can also add another image (while using a different name) and then change the language of the file and change the picture.
Now the question: How can I change the button image in each language to the correct image if I can't give them the same name (e.g. a.jpg in resource.resx, a.jpg in resources-foo.resx, etc).
I don't think it is realistic to check in the code the current culture and then by using the pictures resources names in the OnMouseEnter/Leaves/Down implement the switch between the three picture.
As an average application has 100+ buttons there must be a simple and easy way to do it, that I just can't think of (or find...)...
回答1:
The ResourceManager in .NET does not only do string localization. It can provide localized binary content of any kind, including images. This means that you will never need to write logic to pick the relevant localized version of an image, and won't even need to manipulate image file paths. You just ask the ResourceManager to give you the image you want, and it will give you the appropriate language version of this image (depending on the current UICulture
).
Visual Studio makes it easy to add images to a resource file. The Resource editor has a button in the top left corner (see below) that lets you view the different types of resources contained in your resx file, which can be strings (the most common choice that most of us are familiar with), images and more.
This MSDN link explain the whole thing. Here's one example I took from it which shows how to retrieve a localized image from your resources:
rm = new ResourceManager("Images", this.GetType().Assembly);
pictureBox1.Image = (System.Drawing.Image)rm.GetObject("flag");
回答2:
I would use resource files. They work something like this: "if using this language, load 'Button1A.jpg', if using other language, load 'Button1B.jpg'", etc. I'm not sure of the syntax or how to implement them, but I'm pretty sure you can specify a language/locale somewhere (or maybe it gets specified for you based on Windows settings), and you just provide which image to use when and it'll do the rest for you.
Definitely look up resource files.
I think the default use for resource files is for strings. So rather than loading different images, they are supposed to be used to load different translated (localized) strings. However, rather than this, just supply a different filepath (the filepath to the different images) in place of the strings.
Hopefully I've made sense, sorry for not providing an example.
来源:https://stackoverflow.com/questions/8387699/localization-of-image-resources-in-windows-forms