logo       

Re: embedding resources for globalization (complete frustration): msg#00082

windows.devel.dotnet.web

Subject: Re: embedding resources for globalization (complete frustration)

I've used embedded resources w/ VS.NET 2003 quite successfully. Here are the
steps I've used...

1. Create the resource files for each culture i.e.
ResourceFile.resx
ResourceFile.FR.resx
ResourceFile.DE.resx
Etc...

And add your translated strings to the files.

2. Then create a helper class to retrieve the managed content, something like
this:

public static string GetResourceContent(string name)
{
ResourceManager res = new ResourceManager("Root Namespace Name Here",
Assembly.GetExecutingAssembly());
return res.GetString(name);
}

3. I generally put this method in a custom control, similar to a label that I
can put on a page and assign the string name to retrieve from the resource
file...

namespace MyNameSpace
{
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Resources;
using System.Reflection;

/// <summary>
/// Summary description for SiteTextControl.
/// </summary>
public abstract class SiteTextControl : System.Web.UI.UserControl
{
private string _Name;
private string _CssClass;

private void Page_Load(object sender, System.EventArgs e)
{}

public string Name
{
get {return _Name;}
set {_Name = value;}
}

public string CssClass
{
get
{
if (_CssClass == null || _CssClass.Length == 0)
_CssClass = "font-md";

return _CssClass;
}
set {_CssClass = value;}
}

protected sealed override void CreateChildControls()
{
ResourceManager res = new
ResourceManager("Root Namespace Name Here", Assembly.GetExecutingAssembly());
string _Text = "";
if (_CssClass != null && _CssClass.Length != 0)
_Text = "<span class=\"" + _CssClass + "\">" +
res.GetString(_Name) + "</span>";
else
_Text = res.GetString(_Name);

LiteralControl lit = new
LiteralControl(_Text);
Controls.Add(lit);
lit.Dispose();
}

public static string GetResourceContent(string name)
{
ResourceManager res = new
ResourceManager("Root Namespace Name Here", Assembly.GetExecutingAssembly());
return res.GetString(name);
}

#region Web Form Designer generated code
protected sealed override void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web
Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// Required method for Designer support - do not
modify
/// the contents of this method with the code
editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}


4. Then I add the control to a page (or other control) where I want to use
managed content
<%@Register tagprefix="MyTagPrefix" Tagname="SiteTextControl"
src="SiteTextControl.ascx" %>

<p> <MyTagPrefix:SiteTextControl id="Sitetextcontrol1" runat="Server"
name="Name of string to lookup" /></p>

As long as you have set the System.Globalization.CultureInfo.CurrentCulture to
the correct target culture, you can rely on the resource manager to read the
string from the correct resource file. It should fall back on the English
version if it can't find the string in the target culture's resource file.

I think the most tricky thing I encountered when I was setting this up, was
making sure the namespace that I passed in to the ResourceManager constructor
was correct, but you should be able to use Reflector, or ILDasm to make sure
your resource files are being included in the assembly, and to check their
names...

Good Luck
MK



Mark Kucera
mkucera@xxxxxxxxxxxxxx


-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
[mailto:DOTNET-WEB@xxxxxxxxxxxxxxxxxxx] On Behalf Of lizet peña
Sent: Monday, June 13, 2005 10:56 AM
To: DOTNET-WEB@xxxxxxxxxxxxxxxxxxx
Subject: [DOTNET-WEB] embedding resources for globalization (complete
frustration)

Well, our team decided to go back to VS 2003 instead of deploying the
project on 2005, the reasons are not part of this email.
Our current problem is with 1.1 Framework. We have a .resX file for
each web form on the project and would like to access each resource in
it. The ResourceManager class would only work with .resources files
and in order to access them, these files should be embedded into
satellite assemblies.
We followed the steps in
http://www.codeproject.com/dotnet/Localization.asp

Al.exe
/t:lib
/embed: MyApplication.en-GB.resources,
/culture:en-GB
/out:MyApplication.resources.dll

and created a MyApplication.resources.dll for each culture. We
previously converted the .resX files into .resources with resgen.exe.
Resgen MyApplication.en-GB.resX

In order to load the specific resource:

Dim a As [Assembly] = [Assembly].Load("MyApplication")
Dim rm As ResourceManager = New
ResourceManager("searchUI", a)
Dim str As String = rm.GetString("errorMessage",
Thread.CurrentThread.CurrentUICulture)

The assembly loads fine and the resource manager is instantiated
correctly, however when we try to get the string with the particular
resource, there's is an exception saying the resource cannot be
located, to make sure the .resource file is embedded in the satellite
assembly.
Any ideas of where to start troubleshooting the error are more than
welcome, I have generated the satellite assemblies three times now and
honestly don't know why the resources aren't loaded at all.
Thanks in advance,
Lizet

===================================
This list is hosted by DevelopMentor® http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentor® http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com



<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise