Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Visual Basic 2005 Programmer's Reference
This is the forum to discuss the Wrox book Visual Basic 2005 Programmer's Reference by Rod Stephens; ISBN: 9780764571985
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Visual Basic 2005 Programmer's Reference section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old May 4th, 2006, 04:13 PM
Registered User
 
Join Date: Mar 2006
Location: Hampton, VA, USA.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Localization Resources

In Chapter 25, page 696, you discuss Localization Resources. On first reading this looks "cool", but only seems to handle the static labeling of form controls at "design time" -- and leaves "run time" control labeling to your imagination.

I did setup a form with both (default) English (En-US) and German (De-DE) labeling for their controls and menus. Separate "form" resources did appear and both could be appropriately edited if you change the "Language" property on the form.

I'll take your word for it that in Germany they will see the German labels and in the US they see the English labels -- automatically -- since I don't have an O.S. with German localization settings. I was able to force German form labels per your example on page 697, and it worked. All my German "design time" control and menu label (and menu tooltip) settings did show up on the form when I executed the program.

However, when I tried the method of adding German and English "language" select radio buttons, and tried both your methods shown (Examples on page 698-699 and also Example on page 700) they seemed to work OK for normal controls (buttons, labels, etc.) but the Menu labels and menu tooltips failed to switch when you click the radio buttons. Any suggestion on how the Menu can be included in the dynamic switching of language resources in these examples?

Also, is their any similarly "automatic" way of handling the "run time" changes to control and menu labels? Is there any way to handle the tooltips of normal controls, since these no longer have a "design time" property for each control (as in VB 6.0), but must be assigned at run time via a ToolTip object?

I must say this Chapter is confusing because Microsoft has so many types of resource files and the real purpose of each type is not clearly spelled out in your book or in the Microsoft documentation I have read. Can you suggest any other source of information on "resources"?


Truman Byron Thomas
__________________
Truman Byron Thomas
Reply With Quote
  #2 (permalink)  
Old May 5th, 2006, 01:17 PM
Rod Stephens's Avatar
Wrox Author
Points: 2,999, Level: 22
Points: 2,999, Level: 22 Points: 2,999, Level: 22 Points: 2,999, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 601
Thanks: 1
Thanked 84 Times in 83 Posts
Default

This is hard. Microsoft doesn't really intend you to manually switch locales, particularly at run time. Although I can see some potential uses in addition to testing.

The example in the book was mostly intended to let you see that the localization sort of works. The real switch would happen automatically when you run on a localized system.

There are a couple of reasons that example is too simple for *really* switching locales.

As you noted, it doesn't handle menus. That's because the menu items (including the top-level menus like File and Help) are not "children" of the MenuStrip they're "Items."

As you also noted, it doesn't handle tooltips. Those are not really properties of the controls because they're handled by an ExtenderProvider so the resource manager can't localize them automatically. You'll need to do it in code. (Same thing with the ErrorProvider and any other ExtenderProvider.)

It doesn't handle nested controls. For example, if you have a Label inside a GroupBox, it doesn't update the Label.

There are some specific kinds of controls that are hard to handle, too. For example, ComboBox and ListBox contain items that are Objects not Strings so the resource manager doesn't know how to localize them. For example, they might contain Employee objects. The resource manager doesn't stand a chance. You need to do this yourself.

Here are some additional examples:

    Localize control resources at run time in VB .NET
    http://www.vb-helper.com/howto_net_l...t_runtime.html
    This is a much better version of the example from the book. It handles nested controls and menu items, and gives examples of handling ComboBoxes, ListBoxes, and tooltips. I can't guarantee it will work with every other kind of control. (I can probably guarantee it won't work because there are so many kinds of controls these days.)


    Localize control resources at run time by reloading the form in VB .NET
    http://www.vb-helper.com/howto_net_l...reloading.html
    This example reloads the form's controls. Note that it removes any data the user has entered.

    Localize control resources at run time by reopening the form in VB .NET
    http://www.vb-helper.com/howto_net_l...reopening.html
    This example reloads the whole form. It also removes any data the user has entered.

Hopefully these will get you started.


Rod
RodStephens@vb-helper.com
Author of "Visual Basic 2005 Programmer's Reference"
http://www.vb-helper.com/vb_prog_ref.htm
Reply With Quote
  #3 (permalink)  
Old May 8th, 2006, 03:50 PM
Registered User
 
Join Date: Mar 2006
Location: Hampton, VA, USA.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the links to three additional examples on your book's website, which I will call Example1, Example2, and Example 3 below. However, the last code example code ("Display Help About) in each of these additional examples does not work for me, because I am still confused about the plethora of resource types in VB.

The Good News: I tried Example2 and it DID correctly switch all the "primary" control labels from English to French (instead of German in your examples) and back again when I click En/Fr radio buttons. Unlike the book example in Chapter 25, it even changes the Menu Strip and its tooltips. I did not try Example3 but assume it will work the same.

The Bad News: I get confused when I read your explanation line in either of the three examples, just above "Display Help About" that reads:

    "This example stores strings in resource files names RuntimeStrings.resx and RuntimeStrings.de-DE.resx so Visual Basic makes a My.Resources.RuntimeStrings namespace for them."

I get confused about *where* and *how* I would create both of these resource files you call "RunTimeStrings". The same confusion happens in Example1 where the ResourceManager is given a resource file called "Localized.Form1" before other run-time control labels are changed by other code.

I have 3 questions below(Q1, Q2, and Q3), but unfortunately its not easy to ask them without a lot of explanation of my confusion first.

My program (WindowsApplication1) has already created a few local "project" resources with added 'strings' (like "Greeting" and "Salutation"), and these show up in the Solution Explorer window under "MyProject" in a file titled "Resources.resx". With the same resource editor I also added some icons, but these did not show up in the first resource file, but instead showed up separately (as individual icon files) on the Solution Explorer window under a "folder icon" labeled simply "Resources". I can reference both then defined "strings" and the "icons" at run-time with "My.Resources.<name>" in my code. I later learned that these two resource types got separated because there are two types of "Project" resources (the two type names escape me), and strings can only be in the Resources.vb file, while the icons default as separate files in the other place (but can be also be added to the Resources.vb file if you know how). Incidentally, I found no reference to these two types of resources in your Chapter 25.

Continuing, My program (WindowsApplication1) has other "Form-related" resources that show up in Solution Explorer under the name of my Form (which was accidentally named "MyFirstProject" instead of "Form1" as in your examples). Thus, under my form's file "MyFirstProject.vb" I have three files (1) "MyFirstProject.Designer.vb", (2)"MyFirstProject.fr.resx", and (3)"MyFirstProject.resx". My alternate (non default) form resources are "French" instead of "German" as in your examples. These are the "Localization Resources" I created by changing the 'Language' property of my form.

Q1: The question (finally) is *what* file of resources do I create, and *where*, that can handle all the "run-time" strings I need to define -- that are handled by the ComponentManager object that you use in "Example1" -- and also in the 'Help|About' code in all three examples?

At first I assumed that these custom "run-time" strings should be added to either the "French" or "Default" Localization Resource files defined for my form. However, when I tried to add a string to either file, VS squawks loudly, warning me that I probably shouldn't try. I never went ahead and tried it though. Instead, I created a new "Resource File" named "Resource.fr.resx" using "Add Item" under the "Project" menu. And, it lets me add all the "french" string resources I want. The only problem is that it shows up at the highest level under "WindowsApplication1" on the Solution Explorer. I was expecting it to show up under "My Project" which is where the "default" (English) string resources are defined.
Q2: Because it doesn't show up there, how do I know that it will be automatically selected instead of "Resources.resx" in France or when I access any ResourceManager string in Example1, Example2, or Example 3?
Q3: Or, should I have gone ahead and edited the "Localization Resource" files that VS strongly suggested I not edit?


Truman Byron Thomas
Reply With Quote
  #4 (permalink)  
Old May 9th, 2006, 10:13 AM
Rod Stephens's Avatar
Wrox Author
Points: 2,999, Level: 22
Points: 2,999, Level: 22 Points: 2,999, Level: 22 Points: 2,999, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 601
Thanks: 1
Thanked 84 Times in 83 Posts
Default

I think you pretty much have it.

> Q1: The question (finally) is *what* file of resources
> do I create, and *where*, that can handle all the
> "run-time" strings I need to define

Just as you did. Open the Project menu, select Add New Item, pick the Resources File template, enter a name liks ErrorMessages.resx, and click OK. The file should appear in the Solution Explorer. If it's not open already, double-click it to open it and add resources.

Next create a localized version of the file. You can use Project\Add New Item again but you'll want to add exactly the same resources as in the other version so you might just want to copy that one. Select that file in Solution Explorer, press Ctrl-C and Ctrl-V. Right-click on the new file, select Rename, and give it the same name as the other file but with the localization ID as in ErrorMessages.fr-FR.resx. Double-click that file and change the resources to use the French values. Leave the resource names alone so they match the names in the default file ErrorMessages.resx.

Just because the files are part of hte solution, Visual Basic automatically generates code to manage them so you can do this following:

    MessageBox.Show(My.Resources.ErrorMessages.MsgNotF ound)

Here the name "WindowsApplication1.ErrorMessages" is the project's root namespace (by default, also the name of the project) followed by the resource file's name without any extension information. The program will automatically pick between ErrorMessages.resx and ErrorMessages.fr-FR.resx at run time.

The name "MsgNotFound" is the name of a string resource in the files.

You can also do it this longer way:

    Dim resource_manager As New _
        ResourceManager("WindowsApplication1.ErrorMessages ", _
        Me.GetType.Assembly)
    MessageBox.Show(resource_manager.GetString("MsgNot Found"))

> At first I assumed that these custom "run-time" strings should be
> added to either the "French" or "Default" Localization Resource files
> defined for my form. However, when I tried to add a string to either
> file, VS squawks loudly, warning me that I probably shouldn't try.

Don't do it. The Form Designer automatically generates those files so it will probably overwrite any changes that you make.

You *could* use the Form Designer to hide values in labels and picture boxes on the form and set their Visible properties to False. Then you could do stuff like:

    MessageBox.Show(lblNotFound.Text)

> Instead, I created a new "Resource File" named
> "Resource.fr.resx" using "Add Item" under the
> "Project" menu. And, it lets me add all the "french"
> string resources I want. The only problem
> is that it shows up at the highest level under
> "WindowsApplication1" on the Solution Explorer.
> I was expecting it to show up under "My Project"
> which is where the "default" (English) string resources are defined.

That's where Visual Studio puts it.

> Q2: Because it doesn't show up there, how do I know
> that it will be automatically selected instead of
> "Resources.resx" in France or when I access any
> ResourceManager string in Example1, Example2, or Example 3?

IntelliSense will see it in My.Resources. You can't really *know* that it will get the right localized version until you try it. Be sure to use an exactly matching file name (plus the locale identifier). Also be sure the resource strings have exactly the same names.

> Q3: Or, should I have gone ahead and edited the
> "Localization Resource" files that VS strongly
> suggested I not edit?

Nope.

Let me know if it's still giving you trouble. It is a fairly confusing topic.


Rod
RodStephens@vb-helper.com
Author of "Visual Basic 2005 Programmer's Reference"
http://www.vb-helper.com/vb_prog_ref.htm
Reply With Quote
  #5 (permalink)  
Old October 8th, 2007, 04:57 AM
Registered User
 
Join Date: Oct 2007
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to amit55650
Default

Hello Sir,

I have some problems with localizing a window application.

I am using my application this way.....

What i have....
1) My application has around 10-15 winforms and 15-20 C# classes.
2) I have localized all the forms of the application.
3) My application is working fine for all the localized resources.

What i need to implement....

1) I am going to use global strings for error messages and others errors in my whole application. I need to localize all these strings. So where should I save all these resources.
     In Global resource.resx file
     In my custom resource file
     OR In each forms own rresource file.

2) I also need to broke the whole project into separate DLL's, may be 4 to 5.

    How these all DLL's will handle all the resources?



Please guide me.......

1) When we should use resource.resx file.
2) When we should use our custom resx file.
3) When we should use each forms resx file.

Regards,
Amit



Reply With Quote
  #6 (permalink)  
Old October 8th, 2007, 08:32 AM
Rod Stephens's Avatar
Wrox Author
Points: 2,999, Level: 22
Points: 2,999, Level: 22 Points: 2,999, Level: 22 Points: 2,999, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 601
Thanks: 1
Thanked 84 Times in 83 Posts
Default

For displayed strings and error messages, you need to use some sort of code to get them. (In contrast to the text displayed in labels, buttons, and so forth, which is displayed automatically.)

I think the main issue here is how much these values are shared. If they are fairly specific to a particular form, I would put the messages in that form. If they are shared among forms, then you can reduce maintenance by putting them in the program's main resource file. A custom resource file would serve the same purpose.

In your case, when you split the program up, it would help if they can still share. You might consider making a separate resource-only DLL that the other DLLs can all share. That way they can still all use the same error messages. You only have one resource file to maintain and translate and the file will help keep the messages consistent.

You could even put a class inside the resource DLL to make looking up strings easier.

Let me know if that makse sense for your application.


Rod
RodStephens@vb-helper.com

Check out my book:
"Expert One-on-One Visual Basic 2005 Design and Development"
http://www.vb-helper.com/one_on_one.htm

Sign up for the free VB Helper Newsletters at http://www.vb-helper.com/newsletter.html
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
localization veda ASP.NET 2.0 Basics 2 August 4th, 2008 07:48 AM
Globalization and Localization madhuriguda .NET Framework 2.0 1 April 28th, 2008 05:20 AM
localization mohitkumarpaliwal ASP.NET 1.x and 2.0 Application Design 1 July 26th, 2007 03:09 PM
Localization Issues gcm_uk ASP.NET 2.0 Basics 1 June 9th, 2007 09:44 AM
Problems with localization and sitemaps cheets ASP.NET 2.0 Basics 1 March 1st, 2007 12:32 PM



All times are GMT -4. The time now is 01:57 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.