3 ' This script creates the translated RC files for the shell extension.
5 ' Copyright (C) 2007-2009 by Tim Gerundt
6 ' Released under the "GNU General Public License"
8 ' ID line follows -- this is updated by SVN
14 Const STRINGTABLE_BLOCK = 1
16 Const PATH_SHELLEXTTEMPLATE_RC = "../../ShellExtension/Languages/ShellExtensionTemplate.rc"
20 Set oFSO = CreateObject("Scripting.FileSystemObject")
23 If LCase(oFSO.GetFileName(Wscript.FullName)) = "cscript.exe" Then
32 Dim oLanguages, sLanguage
33 Dim oLanguageTranslations, sLanguagePoFilePath
34 Dim StartTime, EndTime, Seconds
38 InfoBox "Warning: " & Wscript.ScriptName & " can take several seconds to finish!", 3
40 Set oLanguages = GetLanguages
41 For Each sLanguage In oLanguages.Keys 'For all languages...
42 If (bRunFromCmd = True) Then 'If run from command line...
43 Wscript.Echo sLanguage
45 Set oLanguageTranslations = GetTranslationsFromPoFile(oLanguages(sLanguage))
46 If (oLanguageTranslations.Count > 0) Then 'If translations exists...
47 CreateRcFileWithTranslations PATH_SHELLEXTTEMPLATE_RC, "../../ShellExtension/Languages/ShellExtension" & sLanguage & ".rc", oLanguageTranslations
52 Seconds = DateDiff("s", StartTime, EndTime)
54 InfoBox Wscript.ScriptName & " finished after " & Seconds & " seconds!", 3
59 Function GetLanguages()
62 Set oLanguages = CreateObject("Scripting.Dictionary")
64 For Each oFile In oFSO.GetFolder(".").Files 'For all files in the current folder...
65 If (LCase(oFSO.GetExtensionName(oFile.Name)) = "po") Then 'If a PO file...
66 oLanguages.Add oFSO.GetBaseName(oFile.Name), oFile.Path
69 Set GetLanguages = oLanguages
74 Function GetTranslationsFromPoFile(ByVal sPoPath)
75 Dim oTranslations, oTextFile, sLine
76 Dim oMatch, iMsgStarted, sMsgId, sMsgStr
77 Dim reMsgId, reMsgStr, reMsgContinued
79 Set reMsgId = New RegExp
80 reMsgId.Pattern = "^msgid ""(.*)""$"
81 reMsgId.IgnoreCase = True
83 Set reMsgStr = New RegExp
84 reMsgStr.Pattern = "^msgstr ""(.*)""$"
85 reMsgStr.IgnoreCase = True
87 Set reMsgContinued = New RegExp
88 reMsgContinued.Pattern = "^""(.*)""$"
89 reMsgContinued.IgnoreCase = True
91 Set oTranslations = CreateObject("Scripting.Dictionary")
93 If (oFSO.FileExists(sPoPath) = True) Then 'If the PO file exists...
97 Set oTextFile = oFSO.OpenTextFile(sPoPath, ForReading)
98 Do Until oTextFile.AtEndOfStream = True 'For all lines...
99 sLine = Trim(oTextFile.ReadLine)
101 If (sLine <> "") Then 'If NOT empty line...
102 If (Left(sLine, 1) <> "#") Then 'If NOT comment line...
103 '--------------------------------------------------------------------------------
104 ' Note: We must replace \" temporary with FormFeed and convert them later to ""
105 '--------------------------------------------------------------------------------
106 sLine = Replace(sLine, "\""", vbFormFeed)
107 If reMsgId.Test(sLine) Then 'If "msgid"...
109 Set oMatch = reMsgId.Execute(sLine)(0)
110 sMsgId = oMatch.SubMatches(0)
111 ElseIf reMsgStr.Test(sLine) Then 'If "msgstr"...
113 Set oMatch = reMsgStr.Execute(sLine)(0)
114 sMsgStr = oMatch.SubMatches(0)
115 ElseIf reMsgContinued.Test(sLine) Then 'If "msgid" or "msgstr" continued...
116 Set oMatch = reMsgContinued.Execute(sLine)(0)
117 If (iMsgStarted = 1) Then
118 sMsgId = sMsgId & oMatch.SubMatches(0)
119 ElseIf (iMsgStarted = 2) Then
120 sMsgStr = sMsgStr & oMatch.SubMatches(0)
123 sMsgId = Replace(sMsgId, vbFormFeed, """""")
124 sMsgStr = Replace(sMsgStr, vbFormFeed, """""")
130 If (iMsgStarted = 0) Then 'If NOT inside a translation...
131 If (sMsgId <> "") And (sMsgStr <> "") And (sMsgId <> sMsgStr) Then 'If translated...
132 oTranslations.Add sMsgId, sMsgStr
140 Set GetTranslationsFromPoFile = oTranslations
145 Sub CreateRcFileWithTranslations(ByVal sMasterRcPath, ByVal sLanguageRcPath, ByVal oTranslations)
146 Dim oMasterRcFile, sMasterLine
147 Dim oLanguageRcFile, sLanguageLine
148 Dim iBlockType, oMatches, oMatch, sMsgId, sMsgStr
149 Dim reLanguage, reCodePage, reString, sTemp
151 Set reLanguage = New RegExp
152 reLanguage.Pattern = "LANGUAGE (LANG_\w*, SUBLANG_\w*)"
153 reLanguage.IgnoreCase = True
155 Set reCodePage = New RegExp
156 reCodePage.Pattern = "code_page\(([\d]+)\)"
157 reCodePage.IgnoreCase = True
159 Set reString = New RegExp
160 reString.Pattern = """(.*?)"""
161 reString.IgnoreCase = True
163 If (oFSO.FileExists(sMasterRcPath) = True) Then 'If the master RC file exists...
164 iBlockType = NO_BLOCK
165 Set oMasterRcFile = oFSO.OpenTextFile(sMasterRcPath, ForReading)
166 Set oLanguageRcFile = oFSO.CreateTextFile(sLanguageRcPath, True)
167 Do Until oMasterRcFile.AtEndOfStream = True 'For all lines...
168 sMasterLine = oMasterRcFile.ReadLine
169 sLanguageLine = sMasterLine
170 sMasterLine = Trim(sMasterLine) 'Save Masterline trimmed!
172 If (sMasterLine = "STRINGTABLE") Then 'STRINGTABLE...
173 iBlockType = STRINGTABLE_BLOCK
174 ElseIf (sMasterLine = "BEGIN") Then 'BEGIN...
176 ElseIf (sMasterLine = "END") Then 'END...
177 If (iBlockType = STRINGTABLE_BLOCK) Then 'If inside stringtable...
178 iBlockType = NO_BLOCK
180 ElseIf (Left(sMasterLine, 2) = "//") Then 'If comment line...
182 ElseIf (sMasterLine <> "") Then 'If NOT empty line...
183 Select Case iBlockType
185 If reLanguage.Test(sMasterLine) Then 'LANGUAGE...
186 Set oMatch = reLanguage.Execute(sMasterLine)(0)
187 sMsgId = oMatch.SubMatches(0)
188 If (sMsgId <> "") And (oTranslations.Exists(sMsgId) = True) Then 'If translation located...
189 sMsgStr = oTranslations(sMsgId)
190 sLanguageLine = Replace(sLanguageLine, "LANGUAGE " & sMsgId, "LANGUAGE " & sMsgStr)
192 ElseIf reCodePage.Test(sMasterLine) Then 'code_page...
193 Set oMatch = reCodePage.Execute(sMasterLine)(0)
194 sMsgId = oMatch.SubMatches(0)
195 If (sMsgId <> "") And (oTranslations.Exists(sMsgId) = True) Then 'If translation located...
196 sMsgStr = oTranslations(sMsgId)
197 sLanguageLine = Replace(sLanguageLine, "code_page(" & sMsgId & ")", "code_page(" & sMsgStr & ")")
201 Case STRINGTABLE_BLOCK:
202 If (InStr(sMasterLine, """") > 0) Then 'If quote found (for speedup)...
203 '--------------------------------------------------------------------------------
204 ' Note: We must replace "" temporary with FormFeed...
205 '--------------------------------------------------------------------------------
206 sTemp = Replace(sMasterLine, """""", vbFormFeed)
207 If reString.Test(sTemp) Then 'String...
208 Set oMatches = reString.Execute(sTemp)
209 For Each oMatch In oMatches 'For all strings...
210 sMsgId = Replace(oMatch.SubMatches(0), vbFormFeed, """""")
211 If (sMsgId <> "") And (oTranslations.Exists(sMsgId) = True) Then 'If translation located...
212 sMsgStr = oTranslations(sMsgId)
213 sLanguageLine = Replace(sLanguageLine, """" & sMsgId & """", """" & sMsgStr & """")
221 oLanguageRcFile.WriteLine sLanguageLine
224 oLanguageRcFile.Close
230 Function InfoBox(ByVal sText, ByVal iSecondsToWait)
233 If (bRunFromCmd = False) Then 'If run from command line...
234 Set oShell = Wscript.CreateObject("WScript.Shell")
235 InfoBox = oShell.Popup(sText, iSecondsToWait, Wscript.ScriptName, 64)
236 Else 'If NOT run from command line...