4

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...)...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nadav Ben-Gal
  • 549
  • 3
  • 13

2 Answers2

3

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.

enter image description here

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");
Clafou
  • 15,250
  • 7
  • 58
  • 89
  • The problem is, when i'm adding the first image lets say "flag.png" to Resource.resx everything is great. then i go to another resx file (e.g. Resource.foo-BAR.resx), and try to add "flag.png" file but i got an error message: "A file with the name '......\flag.png" alreay exists. Do you want to replace it?" same message, if i add it as an existing file or if i create a new image... – Nadav Ben-Gal Dec 07 '11 at 22:39
  • If you look in your Visual Studio solution you'll see that unless the file you add to your resource is already in the solution, it creates a copy of it. So you'll need to prevent a conflict, either by give each language version a unique name, such as "flag.en-US.png" and "flag.fr-FR.png", or by adding them to your solution first inside their own language subfolder (e.g. "en-US\flag.png" and "fr-FR\flag.png" -- make sure these are in your Visual Studio project otherwise Visual Studio will try to copy them and will give you the error you mentioned) – Clafou Dec 08 '11 at 09:36
0

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.

Andrew Rasmussen
  • 14,912
  • 10
  • 45
  • 81
  • i'm using resources, the problem is that i have 3 images for each button in each language. like i said, 1 image for each langauge, isn't a real problem. the problem is the 3*N (=lanuguages) images and how to make it work togther without creating a `if (culture= ...) {image= ... ; } else if ...` for every button... – Nadav Ben-Gal Dec 05 '11 at 17:28
  • You should be able to say something like "button_1_path = resource_button_1_path"? – Andrew Rasmussen Dec 05 '11 at 17:36
  • The point if resource files is that you don't do if (locale 1), else if (locale 2)... You give it a resource string name and say this is the locale, this is the resource string name, give me the file path. It works as a map between locale and resource strings. – Andrew Rasmussen Dec 05 '11 at 17:40
  • 1
    you can't have an image in english saying "hey" for example and an image in another language saying "hey" both entered into the resources as hey.jpg. one must be hey-en.jpg and the other hey-foo.jpg, and you can't set for a button an image for onmousehover/onclick. it msut be done by code. – Nadav Ben-Gal Dec 05 '11 at 18:31