Localization - Real examples using satellite DLLs and C#.

M

Martin Platt

Guys!

I've been playing about with some code trying to get the
satellite assembly resource files to work.

From what I understand you must have a fallback for a
particular language, as well as specific regional
variations of that language in the satellite dll. That,
I can get to work.

The code I was using could load the currently executing
assembly, and pick out resources embedded in that without
any troubles.

Also, if I load a different satellite assembly, and use
the resourcemanager object on that, I can also make that
work. What I don't understand is, how is this useful? I
mean, if you must load the satellite assembly before
accessing the resource, it has to be hard coded in the
project (or you write something to trawl through a
particular location, loading all assemblies there?)

Here's a little snippet of what I'm trying to make use
of, but presently is of little use at all....

System.Reflection.Assembly myAssembly = this.GetType
().Assembly;
System.Reflection.Assembly myOtherAssembly;
myOtherAssembly = System.Reflection.Assembly.Load
("SatelliteResource");
System.Resources.ResourceManager rm = new
System.Resources.ResourceManager
("SatelliteResource.resource", myOtherAssembly);

System.Globalization.CultureInfo thisCulture =
System.Threading.Thread.CurrentThread.CurrentCulture;
string retVal = rm.GetString
("System.NullReferenceException", thisCulture);

MessageBox.Show(retVal);

I'd like this to be useful in that I can also have other
satellite resources, and add them into the application at
a later date without the need to recode.

Is trawling through and loading all dlls the smartest way
to do it??

Any suggestions would be highly appreciated...

Cheers.
 
M

Marc Bernard

Martin Platt said:
From what I understand you must have a fallback for a
particular language, as well as specific regional
variations of that language in the satellite dll. That,
I can get to work.

The code I was using could load the currently executing
assembly, and pick out resources embedded in that without
any troubles.

Also, if I load a different satellite assembly, and use
the resourcemanager object on that, I can also make that
work. What I don't understand is, how is this useful? I
mean, if you must load the satellite assembly before
accessing the resource, it has to be hard coded in the
project (or you write something to trawl through a
particular location, loading all assemblies there?)

Here's how we do it. In our case, we don't build the fallback resources
into the main application, to save space. We don't have to load the
resource dll - it happens for us. This snippet is from a function that
takes a constant string (constantName) that identifies the resource string
we want to load.


ResourceManager resourceManager = null;

string resourceString = "";

try
{
resourceManager = new ResourceManager("LR",
Assembly.GetExecutingAssembly());
}
catch (Exception resourceEx)
{
Console.WriteLine("Could not create resource manager. Error: " +
resourceEx.Message);
}

try
{
resourceString = resourceManager.GetString(constantName);
}
catch (MissingManifestResourceException)
{
// we couldn't find the right resource. We're probably running in
// a locale that we do not have a language DLL for. We should try to
// fall back to US English

try
{
CultureInfo culture = new CultureInfo("en-US");
resourceString = resourceManager.GetString(constantName, culture);
}
catch (Exception cultureEx)
{
// last chance - no idea what went wrong
resourceString = "Could not get string from resource manager";
}
}
catch (Exception ex)
{
// no idea what went wrong
resourceString = "Could not get string from resource manager.";
}

return resourceString;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top