OSDN Git Service

PATCH: [ 2794514 ] Create translated ShellExtension RC files from PO files
[winmerge-jp/winmerge-jp.git] / Translations / ShellExtension / CreateTranslatedRcFiles.vbs
1 Option Explicit
2 ''
3 ' This script creates the translated RC files for the shell extension.
4 '
5 ' Copyright (C) 2007-2009 by Tim Gerundt
6 ' Released under the "GNU General Public License"
7 '
8 ' ID line follows -- this is updated by SVN
9 ' $Id$
10
11 Const ForReading = 1
12
13 Const NO_BLOCK = 0
14 Const STRINGTABLE_BLOCK = 1
15
16 Const PATH_SHELLEXTTEMPLATE_RC = "../../ShellExtension/Languages/ShellExtensionTemplate.rc"
17
18 Dim oFSO, bRunFromCmd
19
20 Set oFSO = CreateObject("Scripting.FileSystemObject")
21
22 bRunFromCmd = False
23 If LCase(oFSO.GetFileName(Wscript.FullName)) = "cscript.exe" Then
24   bRunFromCmd = True
25 End If
26
27 Call Main
28
29 ''
30 ' ...
31 Sub Main
32   Dim oLanguages, sLanguage
33   Dim oLanguageTranslations, sLanguagePoFilePath
34   Dim StartTime, EndTime, Seconds
35   
36   StartTime = Time
37   
38   InfoBox "Warning: " & Wscript.ScriptName & " can take several seconds to finish!", 3
39   
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
44     End If
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
48     End If
49   Next
50   
51   EndTime = Time
52   Seconds = DateDiff("s", StartTime, EndTime)
53   
54   InfoBox Wscript.ScriptName & " finished after " & Seconds & " seconds!", 3
55 End Sub
56
57 ''
58 ' ...
59 Function GetLanguages()
60   Dim oLanguages, oFile
61   
62   Set oLanguages = CreateObject("Scripting.Dictionary")
63   
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
67     End If
68   Next
69   Set GetLanguages = oLanguages
70 End Function
71
72 ''
73 ' ...
74 Function GetTranslationsFromPoFile(ByVal sPoPath)
75   Dim oTranslations, oTextFile, sLine
76   Dim oMatch, iMsgStarted, sMsgId, sMsgStr
77   Dim reMsgId, reMsgStr, reMsgContinued
78   
79   Set reMsgId = New RegExp
80   reMsgId.Pattern = "^msgid ""(.*)""$"
81   reMsgId.IgnoreCase = True
82   
83   Set reMsgStr = New RegExp
84   reMsgStr.Pattern = "^msgstr ""(.*)""$"
85   reMsgStr.IgnoreCase = True
86   
87   Set reMsgContinued = New RegExp
88   reMsgContinued.Pattern = "^""(.*)""$"
89   reMsgContinued.IgnoreCase = True
90   
91   Set oTranslations = CreateObject("Scripting.Dictionary")
92   
93   If (oFSO.FileExists(sPoPath) = True) Then 'If the PO file exists...
94     iMsgStarted = 0
95     sMsgId = ""
96     sMsgStr = ""
97     Set oTextFile = oFSO.OpenTextFile(sPoPath, ForReading)
98     Do Until oTextFile.AtEndOfStream = True 'For all lines...
99       sLine = Trim(oTextFile.ReadLine)
100       
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"...
108             iMsgStarted = 1
109             Set oMatch = reMsgId.Execute(sLine)(0)
110             sMsgId = oMatch.SubMatches(0)
111           ElseIf reMsgStr.Test(sLine) Then 'If "msgstr"...
112             iMsgStarted = 2
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)
121             End If
122           End If
123           sMsgId = Replace(sMsgId, vbFormFeed, """""")
124           sMsgStr = Replace(sMsgStr, vbFormFeed, """""")
125         End If
126       Else 'If empty line
127         iMsgStarted = 0
128       End If
129       
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
133         End If
134         sMsgId = ""
135         sMsgStr = ""
136       End If
137     Loop
138     oTextFile.Close
139   End If
140   Set GetTranslationsFromPoFile = oTranslations
141 End Function
142
143 ''
144 ' ...
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
150   
151   Set reLanguage = New RegExp
152   reLanguage.Pattern = "LANGUAGE (LANG_\w*, SUBLANG_\w*)"
153   reLanguage.IgnoreCase = True
154   
155   Set reCodePage = New RegExp
156   reCodePage.Pattern = "code_page\(([\d]+)\)"
157   reCodePage.IgnoreCase = True
158   
159   Set reString = New RegExp
160   reString.Pattern = """(.*?)"""
161   reString.IgnoreCase = True
162   
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!
171       
172       If (sMasterLine = "STRINGTABLE") Then 'STRINGTABLE...
173         iBlockType = STRINGTABLE_BLOCK
174       ElseIf (sMasterLine = "BEGIN") Then 'BEGIN...
175         'IGNORE FOR SPEEDUP!
176       ElseIf (sMasterLine = "END") Then 'END...
177         If (iBlockType = STRINGTABLE_BLOCK) Then 'If inside stringtable...
178           iBlockType = NO_BLOCK
179         End If
180       ElseIf (Left(sMasterLine, 2) = "//") Then 'If comment line...
181         'IGNORE FOR SPEEDUP!
182       ElseIf (sMasterLine <> "") Then 'If NOT empty line...
183         Select Case iBlockType
184           Case NO_BLOCK:
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)
191               End If
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 & ")")
198               End If
199             End If
200             
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 & """")
214                   End If
215                 Next
216               End If
217             End If
218             
219         End Select
220       End If
221       oLanguageRcFile.WriteLine sLanguageLine
222     Loop
223     oMasterRcFile.Close
224     oLanguageRcFile.Close
225   End If
226 End Sub
227
228 ''
229 ' ...
230 Function InfoBox(ByVal sText, ByVal iSecondsToWait)
231   Dim oShell
232   
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...
237     Wscript.Echo sText
238   End If
239 End Function