Thursday, October 16, 2008

Using Resource files in Web Service (.NET 3.5) and Regular expression with LINQ

/*
Those who has tried to use resource file in ASP.NET web service know well there is issue in using resource files in web service. Following code snippet targets 3 points:
1. Using resource file in web service or HttpHandlers.
2. Regular expression for converting resource strings with respective localized strings
3. LINQ integration in Regular expression.
Suppose placeholder for resource string in text is like ({BR[MyResourceString]BR}) . Following function will return localized string.
*/


public static string LocalizedHTML(string inputHtml)
{
if (string.IsNullOrEmpty(inputHtml)) return "";

Regex reg = new Regex(@"\(\{IA\[(.*?)\]IA\}\)", RegexOptions.Multiline | RegexOptions.Singleline
| RegexOptions.IgnoreCase | RegexOptions.Compiled);


System.Resources.ResourceManager RM =
new System.Resources.ResourceManager("Resources.Language",
global::System.Reflection.Assembly.Load("App_GlobalResources"));


return reg.Replace(inputHtml, m => (!string.IsNullOrEmpty(RM.GetString(m.Groups[1].Value)) ? RM.GetString(m.Groups[1].Value) : m.Value));

}

1 comment:

Anonymous said...

Hi Ashish,
Thanks for the code, please can you post sample code as well.
Regards,
Raj