Introduction
I wanted to use resources in my WPF project written in vb.net and be able to translate the strings into other languages. I should be able to use the resource strings in XAML and in the code and switch the culture. The resources are called Satellite Assemblies because the strings for different cultures are held in satellite files and loaded as required. They can be amended outside the main assembly and do not add to the size.
VB
is different to c# as some of the classes have different names and most
documentation is for c#. And I could only find partial answers online. Eventually I managed to get it working. It really is simple when you know how.
I have used en-GB because I live in Great Britain.
How to use resx files
You can double-click the resx file to create resources. These can be copied from other resx files. The editing works like editing Access tables. You can select the whole list by clicking in the top left.
It’s also possible to edit resources inside the properties of the project. Right click the project (second row, under the solution in Solution Explorer) and select Properties. There is a Resources tab on the left, about 5 down. Alt-Enter takes you straight there when you are in Solution Explorer.
Start the project using plain Resources.resx rather than Resources.en-GB.resx or whatever. In Class View (View … Other Windows) you can see the Resources class. This has its properties below. Note that resource names are case sensitive.
Within XAML
Resource strings can be accessed as shown in the two lines below. The first line is inside <Window ... >
Note that as soon as you type xmlns:resx=
you have intellisense on the Namespace with the My.Resources
. It adds the
clr-namespace
part for you. The second line is a sample of how you can set the contents of any string be it Title, Caption or Tooltip. Replace my MainWindowCaption with the name of your resource string.
xmlns:myRes="clr-namespace:WpfApplication1.My.Resources"
Title = "{x:Static myRes:Resources.MainWindowCaption}"
Within VB.net Code
Resource strings are simply picked up using code like:
My.Resources.MainWindowCaption
Where MainWindowCaption is the string name of the resource. This works for any culture.
Also View…Other Windows…Class View (Ctrl Shift C) shows My.Resources.
Adding Other Cultures
Cultures and languages are indicated by a five-character code. The first two characters, for example en indicate the language. It is useful to have a file, Resources.en.resx which will be the default for any culture that you don't specify fully. The middle character is a dash, the minus sign. The last two characters, if used, give the country for the language. For example en-GB has colour and en-US has color. They will have files Resources.en-GB.resx and Resources.en-US.resx where just a few of the words are spelled differently. The following link has a table of all the language culture names and codes:
http://msdn.microsoft.com/en-us/library/ee825488(v=cs.20).aspx
To use resources in different languages, start by following
this Walkthrough: Loading Resources from
a Satellite Assembly. I have extracted the relevant points below the link.
http://msdn.microsoft.com/en-us/library/cc668131(v=vs.100).aspx
To enable the satellite assembly
Open AssemblyInfo.vb in the Code Editor. To see the file in Solution Explorer, you may have to click Show All Files.
Uncomment the following line.
'<Assembly: NeutralResourcesLanguage("en-GB", UltimateResourceFallbackLocation.Satellite)>
- Save the solution.
- In Solution Explorer, right-click your project and select Unload Project.
- Visual Studio unloads your project.
- In Solution Explorer, right-click your project and select to edit your vbproj file. The project file opens in the XML Editor.
- Insert the following XML at the same level as the other <PropertyGroup> elements.
<UICulture>en-GB</UICulture>
-
Save and close the file.
- In Solution Explorer, right-click your project and select Reload Project.
- It’s also necessary to set the Custom tool to PublicVbMyResourcesResXFileCodeGenerator. Right click the resx file and select Properties to change this, you just need to add ‘Public’ to the start of the default name. The Access Modifier at the top of the actual list of resources appears as (Custom). You need to do this for every resx file.
These extra steps are required for VB. In c# the resources appear in a folder and
you can just copy and paste them. If you
do this in VB the files don’t end up in the right place.
In Solution Explorer, inside My Project, rename the original Resources.resx to Resources.en-GB.resx, or whatever culture is required.
Right-click on Project, select Properties & then select the Resources tab on the LHS. This will point out you don’t have a default file & offer to create one for you. Accept the offer.
Carry on renaming and creating the default until you have all you want including a default.
For testing the resources inside code:
Edit Application.xaml.vb and add this subroutine:
Protected Overrides Sub OnStartup(e As StartupEventArgs)
MyBase.OnStartup(e)
Dim culture = New CultureInfo("en-AU")
Thread.CurrentThread.CurrentCulture = culture
Thread.CurrentThread.CurrentUICulture = culture
End Sub
Do remember to remove this before release!
Switching Off Resources
If you have switched on multicultural support before the
development is complete (yes, I wanted to prove that it all worked as well!) I would recommend switching it off again while you are
adding new resources. To do this you
need to re-comment out the line in AssemblyInfo.vb and also comment out the <UICulture>
line you added to the vbproj file.
Any
resource files that you have created can be excluded from project by right
clicking them. They remain in the ‘My
Project’ folder and as shadowy icons that can be included later. Leave Resources.resx in place.
There are free tools that export resx files to Excel. Or you can just copy the whole resx file and paste into a workbook
sheet and copy back to any resx file. Use the left-hand column above the * to select groups of rows or the whole file. You can sort by Name, Value or Comment by clicking on the column header to group rows conveniently.
Points of Interest
Using satellite resource strings within vb.net really is easy when you know how. It just doesn't seem to be very well documented.
History
First published 17th February 2014