Sunday, August 26, 2007

I downloaded the very excellent Resource Refactoring Tool (http://www.codeplex.com/ResourceRefactoring) the other day.  It works great in code files, but I found that it didn't work at all for ASPX/HTML pages.  Seeing this as a challenge, I decided to hack something up in VBA to automatically extract the selected text, create a resx name for the text, add it to the resource file, and insert the ASP.NET literal control.

How to use it

This is split up into two different macro modules, one named Utilities, and the other named what ever you want.  Then add them to your Visual Studio tool bar so you have something convenient you can click on (Right click the tool bar, click Customize, click Commands, then Macros under categories, find what you named the macro and then drag it up to your tool bar.  You can either drag it to an existing tool strip, or create your own.  Finally right click on the item while still in customize mode and add an image).

 

Caller:

Imports System Imports EnvDTE Imports EnvDTE80 Imports System.Diagnostics Public Module MoreMacros Public Sub AspTextLiteral() Dim resx as ProjectItem = DTE.ActiveWindow.Object.GetItem("solution\project\folder\ResourceFile.resx") Utilities.AspTextLiteral(resx) End Sub Public Sub InQuoteLiteral() DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer).Activate() Dim resx as ProjectItem = DTE.ActiveWindow.Object.GetItem("solution\project\folder\ResourceFile.resx") Utilities.InQuoteLiteral(resx) End Sub End Module

Utilities:

Imports System Imports EnvDTE Imports EnvDTE80 Imports System.Diagnostics Imports System.Xml Public Module Utilities Public Sub AspTextLiteral(ByRef resourceFile As ProjectItem) Const literalFormat = "<asp:Literal runat=""server"" Text=""<%$ Resources:{0},{1}%>"" />" Const inQuotesFormat = "<%$ Resources:{0},{1}%>" LiteralReplacer(resourceFile, literalFormat, 4) End Sub Public Sub InQuoteLiteral(ByRef resourceFile As ProjectItem) Const inQuotesFormat = "<%$ Resources:{0},{1}%>" LiteralReplacer(resourceFile, inQuotesFormat, 3) End Sub Private Sub LiteralReplacer(ByRef resourceFile As ProjectItem, ByVal template As String, ByVal delete As Int16) Dim ts As TextSelection = DTE.ActiveDocument.Selection Dim name As String = StripNonAllowedChars(ts.Text).Trim.ToLower Dim value As String = ts.Text Dim path As String = resourceFile.Properties.Item("LocalPath").Value Dim resName As String = System.IO.Path.GetFileNameWithoutExtension(resourceFile.Name) name = AddResourceEntry(path, name, value) ts.Text = String.Format(template, resName, name) DTE.ActiveDocument.Selection.Delete(delete) End Sub 'Returns the value of the name used Public Function AddResourceEntry(ByRef resourceFilePath As String, ByVal name As String, ByVal value As String) As String Dim doc As XmlDocument = New XmlDocument doc.Load(resourceFilePath) 'See if it exists first Dim root As XmlElement = doc.DocumentElement Dim e As XmlElement = root.SelectSingleNode(String.Format("data[@name=""{0}""]", name)) If e Is Nothing Then name = CreateUniqueName(root, name) Dim elem As XmlElement = CreateResourceFileElement(doc, name, value) root.AppendChild(elem) Else If e.SelectSingleNode("value").InnerText.ToLower = value.ToLower Then Return name Else name = CreateUniqueName(root, name) Dim elem As XmlElement = CreateResourceFileElement(doc, name, value) root.AppendChild(elem) End If End If doc.Save(resourceFilePath) Return name End Function Private Function CreateUniqueName(ByRef root As XmlElement, ByVal name As String) As String If IsNumeric(name.Substring(0, 1)) Then name = "n" & name End If 'Name doesn't exist, use it If Not CheckNameExists(root, name) Then Return name End If Dim result As String 'Build a unique name For I As Int16 = 1 To 100 result = name & I If Not CheckNameExists(root, result) Then Return result End If Next Return name & Guid.NewGuid.ToString End Function Private Function CheckNameExists(ByRef root As XmlElement, ByVal name As String) As Boolean Dim e As XmlElement = root.SelectSingleNode(String.Format("data[@name=""{0}""]", name)) Return Not (e Is Nothing) End Function Private Function CreateResourceFileElement(ByRef doc As XmlDocument, ByVal name As String, ByVal value As String) As XmlElement Dim result As XmlElement = doc.CreateElement("data") SetAttribute(result, "name", name) SetAttribute(result, "xml:space", "preserve") Dim valueElem As XmlElement = doc.CreateElement("value") valueElem.InnerText = value result.AppendChild(valueElem) Return result End Function Private Function SetAttribute(ByRef element As XmlElement, ByVal attr As String, ByVal value As String) As XmlElement If element.Attributes.GetNamedItem(attr) Is Nothing Then Dim attrib As XmlAttribute = element.OwnerDocument.CreateAttribute(attr) attrib.Value = value element.Attributes.Append(attrib) Else element.Attributes.Item(attr).Value = value End If Return element End Function Private Function StripNonAllowedChars(ByVal text As String) As String Const non_allowed = """';:,<.>/?\|`~!@#$%^&*()-=+[{]} " For I As Integer = 0 To non_allowed.Length - 1 text = text.Replace(non_allowed(I).ToString, "") Next Return text End Function End Module

 

del.icio.us Tags: , , ,
posted on Sunday, August 26, 2007 7:56:04 PM (Alaskan Standard Time, UTC-09:00)  #    Comments [0]
Related posts:
LINQ Goodness
De-crapify your code base with AOP using PostSharp
Problems serializing class which inherits from a generic list
Using Visual Studio Macros to Increase Productivity
Using Custom Attributes to Enable Quick Searching of Your Domain Entities With NHibernate
NHibernate, DateTime and UTC

Referred by:
http://www.dotnetkicks.com/upcoming [Referral]
http://www.dotnetkicks.com/aspnet/ASP_NET_Resource_Refactori... [Referral]
http://www.dotnetkicks.com/ [Referral]
asp.net resources (www.google.co.uk) [Referral]
http://dotnetkicks.com/ [Referral]
resources files .net resx utilities (www.google.com.ar) [Referral]
extract strings .NET resource file (www.google.com) [Referral]
http://www.dotnetkicks.com/page/3 [Referral]
http://dotnetkicks.com/page/4 [Referral]
resource in literal asp.net (www.google.it) [Referral]
ASP.NET string.Format("{0} - {1}" (www.google.com.au) [Referral]
Activedocument asp.net (www.google.com) [Referral]
Refactor!™ for ASP.NET (www.google.it) [Referral]
extract string element vba macro (www.google.com.sg) [Referral]
asp .net resource file literal (www.google.com) [Referral]
ASP.NET Resources .resx (www.google.com) [Referral]
http://www.dotnetkicks.com/page/37 [Referral]
dim constant string .net (www.google.com) [Referral]
.net extract all string resources (www.google.com) [Referral]
nhibernate xmlelement (www.google.com) [Referral]
refactoring aspx resources (www.google.se) [Referral]
.net resource resx tool (www.google.co.uk) [Referral]
asp.net 3 how to return text (www.google.com) [Referral]
Refactor ASP.NET (www.google.com) [Referral]
insert formatting in .net resource file (www.google.ca) [Referral]
+extract +strings +resources +.net (www.google.com) [Referral]
click (search.live.com) [Referral]
Create resource file image In ASP.NET (www.google.com.br) [Referral]
visual studio resource refactor (www.google.com) [Referral]
asp.net resource attribute format (www.google.com) [Referral]
"asp.net" "resource file" (www.google.co.za) [Referral]
{0} resource strings asp.net (www.google.com) [Referral]
extract image from resource file resx (www.google.com.vn) [Referral]
asp.net replace xmlelement value (www.google.co.za) [Referral]
how to delete node from resx (www.google.com) [Referral]
icon extractor resx (www.google.com.ar) [Referral]
Site Referral function in asp.net (www.google.co.in) [Referral]
asp string extract codeplex (www.google.com) [Referral]
aspx XMLElement from string (www.google.com) [Referral]
ASP.Net Resource file images (www.google.gr) [Referral]
.net +"resource file" +exist (www.google.co.uk) [Referral]
extract icon from resx (www.google.co.uk) [Referral]
replace value resource resx (www.google.fr) [Referral]
replace value resource resx (www.google.fr) [Referral]
asp.net asp:Literal resource (www.google.com) [Referral]
asp dim as string (www.google.com) [Referral]
asp.net extract text to resources (www.google.com) [Referral]
how to delete two different image path in asp.net (www.google.com) [Referral]
extract text from an ActiveDocument + visual studio (www.google.com) [Referral]
img to Image refactor ASP.net (www.google.com) [Referral]
asp.net remove resources not use (www.google.es) [Referral]
resourcefile property .net (www.google.com.bo) [Referral]
refactoring resource items asp.net (www.google.ae) [Referral]
asp.net extract string resources (www.google.com.uy) [Referral]
XmlElement to a String (www.google.com) [Referral]
xmlelement innertext asp.net (www.google.com.pk) [Referral]
visual studio projectitem resx (www.google.ru) [Referral]
asp return to referral page (www.google.com) [Referral]
visual studio refactor string literal to constant (www.google.co.uk) [Referral]
http://www.createyouown.com/format-data.html [Referral]
<%$ Resources Format asp.net (www.google.ru) [Referral]
formatting string values resx (www.google.co.uk) [Referral]
aspx refactor resource (www.google.com.br) [Referral]
extract .net resource string (www.google.de) [Referral]
C# asp.net resx literal (www.google.lt) [Referral]
"ASP.NET" Resource string format (www.google.co.jp) [Referral]
asp net resource files character entities (www.google.se) [Referral]
net insert xml to resx (www.google.se) [Referral]
resx asp.net image (www.google.com) [Referral]
XmlAttribute( "space = preserve" (www.google.com) [Referral]
resource file resx + asp.net + img (www.google.nl) [Referral]
asp.net image from resource file (www.google.is) [Referral]
asp.net resource extractor (www.google.com) [Referral]
milk carton development net (www.google.co.uk) [Referral]
resx extract image (www.google.ca) [Referral]
asp.net resource extract (www.google.com) [Referral]
ASP.NET extract resource Name (www.google.dk) [Referral]
returning html formatted strings from a function + asp.net (www.google.co.in) [Referral]
extract integer value from a string in asp.net 2.0 (www.google.co.in) [Referral]
.NET refactor string resource (search.live.com) [Referral]
asp.net resource name (www.google.com) [Referral]
resources resx ASP.NEt (www.google.es) [Referral]
ASP.NET resource file images (www.google.com) [Referral]
asp.net .resx tools (www.google.com) [Referral]
root.SelectSingleNode in ASP .NET (www.google.com.np) [Referral]
How to creatae resource.resx file in asp.net (www.google.com) [Referral]
extract string to resource file (www.google.es) [Referral]
about Resource.resx in asp.net (www.google.co.in) [Referral]
resx aspx format string (www.google.ru) [Referral]
.Net resource files for strings (www.google.com) [Referral]
about Resource.resx in asp.net (www.google.co.in) [Referral]
how to add name with opening of google.co.in with replace of google name (www.google.co.in) [Referral]
envdte resource (www.google.sk) [Referral]
image asp.net resource.resx (www.google.es) [Referral]
REFACTOR STRING TO RESOURCE (www.google.com.ar) [Referral]
asp.net resource strings (www.google.se) [Referral]
resource.resx ASP.NET (www.google.ru) [Referral]
resource asp.net text (www.google.com) [Referral]
use of innertext asp .net 2 (www.google.co.in) [Referral]
resource.resx in asp.net (www.google.co.in) [Referral]
click (search.live.com) [Referral]
string to xmlelement (www.google.com.tr) [Referral]
refactoring in asp.net (www.google.co.in) [Referral]
asp.net resource.resx class (www.google.com) [Referral]
asp.net Resources resx asp.net 3 (www.google.com) [Referral]
asp.net resx string (www.google.com) [Referral]
extract image .net resources (www.google.com) [Referral]
EnvDTE Literal (www.google.com) [Referral]
image path in resource file asp.net (www.google.co.in) [Referral]
asp.net resoource files strings (www.google.com) [Referral]
refactor string to constant (www.google.com) [Referral]
string.format resources (www.google.com) [Referral]
visual studio refactor string (www.google.com) [Referral]
asp.net resource strings and literals (www.google.co.uk) [Referral]
asp.net Resource.resx (www.google.co.il) [Referral]
extract string to resources (www.google.pl) [Referral]
DTE.ActiveWindow.Object.GetItem (www.google.com) [Referral]
replace values in .doc through asp.net (www.google.co.in) [Referral]
Resource Refactoring (www.google.ru) [Referral]
resource file in asp net (www.google.com.vn) [Referral]
.net string ressource extractor (www.google.com) [Referral]
remove xml:space="preserve" in asp.net 2.0 (www.google.com) [Referral]
Root.SelectSingleNode("Domain").Attributes["Name"] (www.google.co.uk) [Referral]
asp resources resx (www.google.com.co) [Referral]
activedocument.selection resource view file (www.google.dk) [Referral]
asp.net solution template DTE c# (www.google.com) [Referral]
xmlelement innertext should be character code? (www.google.co.in) [Referral]
asp.net resources strings properties (www.google.com) [Referral]
resourcefile with asp.net (www.google.com.eg) [Referral]
.net extract strings to resources project (www.google.com) [Referral]
.net resource other resources integers resx (www.google.com) [Referral]
extract image resource file .net (www.google.com) [Referral]
DTE.ActiveWindow.Object.GetItem( (www.google.ca) [Referral]
asp.net resource (www.google.com) [Referral]
asp.net resource file image c# (www.google.com) [Referral]
asp image from ressource (www.google.de) [Referral]
resources files ASP.NET standards (www.google.ca) [Referral]
resx string.format {0} (www.google.com) [Referral]
xmlelement exists in .net (www.google.com) [Referral]
google.ru (www.google.ae) [Referral]
c# asp add resource to file resx (www.google.be) [Referral]
extract image from .resx (www.google.co.uk) [Referral]
ASP.NET resource string {0} (www.google.ca) [Referral]
milk carton net (www.google.co.uk) [Referral]
.net resource files (www.google.com) [Referral]
extract string from resource files (www.google.si) [Referral]
asp.net refactor resource (www.google.cz) [Referral]
"String to XMLElement" c# (www.google.es) [Referral]
.net format value in resx (www.google.com) [Referral]
http://guk.s.scandoo.com/7BB3AEA600532F2111B23633D64921EE.ca... [Referral]
asp string resource (www.google.com) [Referral]
ASP.net load string from resx (www.google.si) [Referral]
asp.net Resource Refactoring (www.google.cz) [Referral]
google.com.uy (referral) (209.85.135.104) [Referral]
asp.net resource string (www.google.com) [Referral]
asp.net write resource string to the page (www.google.nl) [Referral]
asp.net resource name (www.google.ca) [Referral]
asp .net extract resources (www.google.com) [Referral]
asp tolower (www.google.co.uk) [Referral]
remove not used resource string in .net (www.google.com) [Referral]
c# asp .net resource functions (www.google.com) [Referral]
resx asp (www.google.es) [Referral]
Refactor to Resource aspx (www.google.co.uk) [Referral]
resource formatted string asp.net (www.google.com.pk) [Referral]
asp.net string resource file (www.google.com) [Referral]
Visual Studio refactor customize (www.google.com) [Referral]
Asp.Net +Text + Resources (www.google.ca) [Referral]
Replacing values in resources aspx asp.net (www.google.ee) [Referral]
<%$ Resources: Strings, %> (www.google.com) [Referral]
asp.net String.Format("{0}/{1}" (www.google.com) [Referral]
int value in resx .net (www.google.com) [Referral]
.net getitem (www.google.com) [Referral]
extract string in asp (www.google.ca) [Referral]
refactor aspx extract resource (www.google.com) [Referral]
Insert Resources File ASp.NEt (www.google.com.br) [Referral]
.net resx (www.google.de) [Referral]
refactoring string resources (www.google.com) [Referral]
asp.net extract text to resources (www.google.de) [Referral]
asp.net resource string format (www.google.com) [Referral]
envDTE80 resx (www.google.co.jp) [Referral]
String.Format ressources asp.net 2.0 (www.google.fr) [Referral]
visual studio envdte addresource (www.google.fr) [Referral]
asp.net resource formatting (www.google.com) [Referral]
resource file formatting values C# (www.google.com) [Referral]
asp resources resx (www.google.com) [Referral]
remove resources that are not used in asp.net (www.google.co.uk) [Referral]
ASP resources .resx (www.google.com) [Referral]
How to extract icon from resx file (www.google.co.in) [Referral]
new milk carton net (www.google.com) [Referral]
asp.net resx (www.google.pl) [Referral]
extract image from resx (www.google.ro) [Referral]
resource strings asp.net "<%$" (www.google.com) [Referral]
c# replace constant strings with resources (www.google.com) [Referral]
asp.net resources text (www.google.nl) [Referral]
ASP.NET Resources Strings (www.google.ca) [Referral]
image resource from Resource file in ASP.NEt (www.google.co.in) [Referral]
milk carton.com (www.google.com) [Referral]
<asp:Literal remove time value (www.google.com) [Referral]
net asp resources resx (www.google.com) [Referral]
asp.net resource (www.google.com) [Referral]
http://g.s.scandoo.com/84E6129FF5D90EC411616DED8F052057.cach... [Referral]
String to XMLElement (www.google.com) [Referral]
load resx file asp.net (www.google.com) [Referral]
asp.net asp:image resx (www.google.co.uk) [Referral]
ASP.NET Attributes.Item().Value xml (www.google.co.uk) [Referral]
asp.net Literal resource (www.google.com) [Referral]
XmlElement is exist c# (www.google.co.il) [Referral]
innertext asp:literal (www.google.co.in) [Referral]
.net refactoring utilities (www.google.co.uk) [Referral]
asp .net resource file (www.google.com) [Referral]
.net 2.0 resources image path in resource file (www.google.com) [Referral]
asp .net resource file image (www.google.com) [Referral]
how to extract icons from .resx c# (www.google.com) [Referral]
asp.net .Attributes.Item nothing (www.google.com) [Referral]
how to replace " from a string in asp.net (www.google.co.in) [Referral]
visual studio 2008 extract strings to resource files (www.google.com.by) [Referral]
asp.net literal text from few resources (www.google.ru) [Referral]
how to extract image from resx (www.google.ba) [Referral]
how to use resources resx aspx asp.net (www.google.com.ar) [Referral]
asp.net "constant strings" (www.google.com) [Referral]
string resource constant asp..net (www.google.com) [Referral]
asp.net string resources (www.google.com) [Referral]
asp.net resource string (www.google.com) [Referral]
ressource refactoring (www.google.com) [Referral]
ASP.NET resources Extractor (www.google.com) [Referral]
asp.net resource + uk (www.google.com.vn) [Referral]
remove resource asp.net (www.google.com) [Referral]
resource name asp.net (www.google.com) [Referral]
xmlelement asp.net (www.google.co.in) [Referral]
asp.net : how to replace values in .vm files (www.google.co.in) [Referral]
template net for carton (www.google.com) [Referral]
refactor string to resx (www.google.com) [Referral]
refactor extract to resource aspx (www.google.com) [Referral]
asp refactoring (www.google.com) [Referral]
resource refactor macro (www.google.com) [Referral]
resx extract image (www.google.de) [Referral]
Resource extract from resource file .NET (www.google.co.uk) [Referral]
extract from visual studio .resx (www.google.com) [Referral]
save image from resx (www.google.com) [Referral]
c# xmlelement string (www.google.com) [Referral]
add image from resource asp.net (www.google.com) [Referral]
envdte "resource file" (www.google.co.uk) [Referral]
asp.net c# refactoring (www.google.co.uk) [Referral]
.NET literal resource <#? (www.google.com) [Referral]
extract icons from resx files (www.google.gr) [Referral]
extract string function in asp.net (www.google.com) [Referral]
resx extractor (www.google.com) [Referral]
asp:literal resx (searchwithpantera.prodege.com) [Referral]
resx icon extractor (www.google.com) [Referral]
.resources extractor .net (www.google.co.uk) [Referral]
resource file images asp.net (www.google.com) [Referral]
refactor image resource file (www.google.co.in) [Referral]
asp.net const .resx (www.google.co.uk) [Referral]
visual studio refactor literal to resource (www.google.com) [Referral]
xml attribute isexist c# (www.google.com) [Referral]
asp.net resx (www.google.ee) [Referral]
extract item forma string c# asp.net (www.google.co.za) [Referral]
string to xmlelement (www.google.com) [Referral]
resource refactoring ASPX (www.google.co.in) [Referral]
refactoring asp codes to asp.net (www.google.com.ph) [Referral]
refactor to resource file (www.google.co.in) [Referral]
asp.net extract integer from string (www.google.com) [Referral]
asp.net resx tool (www.google.com.mt) [Referral]
extract images from .resx files (www.google.it) [Referral]
.net c# xml xml:space="preserve" append (www.google.co.kr) [Referral]
asp.net resource string (www.google.nl) [Referral]
refactor vba (www.google.com.au) [Referral]
asp resources literal (www.google.com) [Referral]
visual studio extract text for resources (www.google.co.cr) [Referral]
asp net delete image path file (www.google.com) [Referral]
www.nerd.co.il/folder.asp (search.walla.co.il) [Referral]
asp.net resource file images (www.google.com) [Referral]
macro in asp.net (www.google.co.in) [Referral]
how to insert data in resx .net (www.google.com) [Referral]
click (search.live.com) [Referral]
how do i work with Resource Refactor (www.google.pt) [Referral]
asp.net images in resource (www.google.com) [Referral]
c# literal resource string (www.google.com) [Referral]
asp.net refactor resource (www.google.co.uk) [Referral]
selection resource asp.net (www.google.be) [Referral]
string to xmlelement (www.google.com) [Referral]
ASP.net Resource Files and ASP:Literal (www.google.ca) [Referral]
extract image from .resx c# (www.google.com) [Referral]
c# resource extract (www.google.de) [Referral]
asp.net resources resx (www.google.co.uk) [Referral]
literal resources c# aspx (www.google.com) [Referral]
All comments require the approval of the site owner before being displayed.
Name
E-mail
(will show your gravatar icon)
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Live Comment Preview