Hi all gurus, I've seen that VS2005 has a nice new feature (string refactoring) for C#, namely a tool to generate resources from string. Since it's not available in VS2003, I started creating a new macro, to be used for localizing strings embedded in .vb files, by creating resources in a resx file and then retreiving them with a ResourceManager.GetString. Here is the macro code: =========================Macro code======================== Sub main() Dim valore As String = ActiveDocument().Selection.text() If valore.StartsWith("""") And valore.EndsWith("""") Then valore = valore.Substring(1, valore.Length - 2) Dim nome As String = ActiveDocument().Name() nome = nome.Substring(0, nome.Length - 3) + ".resx" Dim rwr As New System.Resources.ResXResourceWriter(nome) Dim key As String = InputBox("Nome della risorsa?", , "_MsgBox_Load.Msg") rwr.AddResource(key, valore) rwr.Generate() rwr.Close() rwr = Nothing ActiveDocument().Selection.text() = "rsrc.GetString(""" + key + """)" valore = InputBox(valore, "Traduzione della risorsa", valore) rwr = New System.Resources.ResXResourceWriter(nome.Replace("resx", "en.resx")) rwr.AddResource(key, valore) rwr.Generate() rwr.Close() rwr = Nothing Dim resType As System.Type Dim rsmgr As System.Resources.ResourceManager = System.Resources.ResourceManager.CreateFileBasedResourceManager(nome.Sub string(0, nome.IndexOf(".")), "C:\Documents and Settings\Saverio.PBELL.000\Documenti\Visual Studio Projects\Fence!", resType) valore = rsmgr.GetString(key) rsmgr = Nothing Else MsgBox("La selezione deve iniziare e finire sulle virgolette", , "Sostituzione non valida") End If End Sub ===================End of Macro code======================= Launching macro in debug, it seems to run flawlessy, and even the "GetString" (inserted for control purposes only) renders the desired string. The resx files are the same (say frmMain.resx and frmMain.en.resx for the files associated with frmMain.vb) used by VB project to store localized text for Windows Form controls (which work correctly switching from a culture to another one, dft to "en-US", at the moment). However, opening this file (after the macro execution) with IDE shows no added string nor anything else, while the desired string has been correctly replaced in .vb source code with the right call to ResourceManager.GetString(key). What could be the problem? Thx 4 any clue.
Well, it seems that this time I'm goin' to answer by myself. I don't know if some1 is interested, but: getting "Name" in macro causes resources to be written in a separate .aspx file, under %Windir%\System32 dir, and it's necessary to get "FullName" instead. However, there's a caveat: WriteResources replace file content, so before writing, it's necessary to rebuild file content. Happy new year!
Don't see what you're looking for? Try a search.
|