Wednesday, November 28, 2012

Cheating at Android localization

I tried to use Google's localization tool the other day. Is it just me that finds that darned thing utterly unusable? Blimey what a palaver! Anyway. I thought that in the spirit of doing things the quick and simple way I would do another quick macro to strip out the XML.

This macro takes a Strings.xml resource, strips out all the tags, copies the text only parts to the clipboard and shuts the file without saving it. You can then paste directly into Google's translate page and copy-paste the translated stuff back in the order it was created.

Of course, if I was less lazy I would do something more useful but this works for me, especially when I add a section to an Android app and want to propagate that to all the localized files.

Anyway, FWIW, enjoy (or not)


    Sub StringsForTranslation()
        DTE.ExecuteCommand("Edit.Find")
        DTE.Find.FindWhat = "<?xml"
        DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument
        DTE.Find.MatchCase = False
        DTE.Find.MatchWholeWord = False
        DTE.Find.Backwards = False
        DTE.Find.MatchInHiddenText = False
        DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
        DTE.Find.Action = vsFindAction.vsFindActionFind
        If (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) Then
            Throw New System.Exception("vsFindResultNotFound")
        End If
        DTE.ActiveDocument.Selection.EndOfLine(True)
        DTE.ActiveDocument.Selection.Delete(2)
        DTE.ExecuteCommand("Edit.Find")
        DTE.Find.FindWhat = "<resources>"
        DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument
        DTE.Find.MatchCase = False
        DTE.Find.MatchWholeWord = False
        DTE.Find.Backwards = False
        DTE.Find.MatchInHiddenText = False
        DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
        DTE.Find.Action = vsFindAction.vsFindActionFind
        If (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) Then
            Throw New System.Exception("vsFindResultNotFound")
        End If
        DTE.ActiveDocument.Selection.Delete(2)
        DTE.ExecuteCommand("Edit.Find")
        DTE.ExecuteCommand("Edit.SwitchtoQuickReplace")
        DTE.Find.Action = vsFindAction.vsFindActionReplaceAll
        DTE.Find.FindWhat = "<string name="
        DTE.Find.ReplaceWith = ""
        DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument
        DTE.Find.MatchCase = False
        DTE.Find.MatchWholeWord = False
        DTE.Find.MatchInHiddenText = False
        DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
        DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResultsNone
        If (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) Then
            Throw New System.Exception("vsFindResultNotFound")
        End If
        DTE.Find.FindWhat = """>"
        DTE.Find.ReplaceWith = """ = """
        If (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) Then
            Throw New System.Exception("vsFindResultNotFound")
        End If
        DTE.Find.FindWhat = "</string>"
        DTE.Find.ReplaceWith = """"
        If (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) Then
            Throw New System.Exception("vsFindResultNotFound")
        End If
        DTE.Find.FindWhat = "<!--"
        If (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) Then
            Throw New System.Exception("vsFindResultNotFound")
        End If
        DTE.Find.FindWhat = "-->"
        DTE.Find.ReplaceWith = """ /*comment*/"
        If (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) Then
            Throw New System.Exception("vsFindResultNotFound")
        End If
        DTE.ActiveDocument.Selection.SelectAll()
        DTE.ExecuteCommand("Edit.Find")
        DTE.Find.FindWhat = "</resources>"
        DTE.Find.Action = vsFindAction.vsFindActionFind
        DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument
        DTE.Find.MatchCase = False
        DTE.Find.MatchWholeWord = False
        DTE.Find.Backwards = False
        DTE.Find.MatchInHiddenText = False
        DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
        If (DTE.Find.Execute() = vsFindResult.vsFindResultNotFound) Then
            Throw New System.Exception("vsFindResultNotFound")
        End If
        DTE.ActiveDocument.Selection.DeleteLeft(2)
        DTE.ActiveDocument.Selection.SelectAll()
        DTE.ActiveDocument.Selection.Copy()
        DTE.ActiveWindow.Close(vsSaveChanges.vsSaveChangesNo)
    End Sub

No comments: