1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
' Run "cscript.exe //X "ConvertRcFilesToPoFiles.vbs"" for debugging
Option Explicit
''
' This script converts the language RC files to the language PO files.
'
' Copyright (C) 2007 by Tim Gerundt
' Released under the "GNU General Public License"
'
' ID line follows -- this is updated by SVN
' $Id$
Const ForReading = 1
Const NO_BLOCK = 0
Const MENU_BLOCK = 1
Const DIALOGEX_BLOCK = 2
Const STRINGTABLE_BLOCK = 3
Const VERSIONINFO_BLOCK = 4
Dim oFSO, bRunFromCmd
Set oFSO = CreateObject("Scripting.FileSystemObject")
bRunFromCmd = False
If (LCase(Right(Wscript.FullName, 11))) = "cscript.exe" Then
bRunFromCmd = True
End If
Call Main
''
' ...
Sub Main
Dim oLanguages, sLanguage
Dim oOriginalTranslations, oLanguageTranslations, oMergedTranslations
Dim StartTime, EndTime, Seconds
StartTime = Time
Wscript.Echo "Warning: " & Wscript.ScriptName & " can take several minutes to finish!"
If (oFSO.FileExists("English.pot") = True) Then 'If the master POT file exists...
Set oOriginalTranslations = GetTranslationsFromRcFile("../mplayerc.rc")
'~ Set oLanguageTranslations = GetTranslationsFromRcFile("German\MergeGerman.rc")
'~ Set oMergedTranslations = MergeTranslations(oOriginalTranslations, oLanguageTranslations)
'~ CreatePoFileWithTranslations "English.pot", "German\German.po", oMergedTranslations
'~ Set oLanguageTranslations = GetTranslationsFromRcFile("ChineseSimplified\MergeChineseSimplified.rc")
'~ Set oMergedTranslations = MergeTranslations(oOriginalTranslations, oLanguageTranslations)
'~ CreatePoFileWithTranslations "English.pot", "ChineseSimplified\ChineseSimplified.po", oMergedTranslations
Set oLanguages = GetLanguages
For Each sLanguage In oLanguages.Keys 'For all languages...
If (bRunFromCmd = True) Then 'If run from command line...
Wscript.Echo sLanguage
End If
Set oLanguageTranslations = GetTranslationsFromRcFile(oLanguages(sLanguage))
Set oMergedTranslations = MergeTranslations(oOriginalTranslations, oLanguageTranslations)
If (oMergedTranslations.Count > 0) Then 'If translations exists...
CreatePoFileWithTranslations "English.pot", sLanguage & ".po", oMergedTranslations
End If
Next
End If
EndTime = Time
Seconds = DateDiff("s", StartTime, EndTime)
Wscript.Echo Wscript.ScriptName & " finished after " & Seconds & " seconds!"
End Sub
''
' ...
Function GetLanguages()
Dim oLanguages, oFile, name, ext
Set oLanguages = CreateObject("Scripting.Dictionary")
For Each oFile In oFSO.GetFolder(".").Files 'For all subfolders in the current folder...
ext = oFSO.GetExtensionName(oFile)
name = oFSO.GetBaseName(oFile)
If (ext = "rc") Then
oLanguages.Add name, oFile.Name
End If
Next
Set GetLanguages = oLanguages
End Function
''
' ...
Function GetTranslationsFromRcFile(ByVal sRcPath)
Dim oTranslations, oTextFile, sLine
Dim oMatch, iBlockType, sKey1, sKey2, iPosition, sValue
Dim sLang, sSubLang, sCodePage
Set oTranslations = CreateObject("Scripting.Dictionary")
If (oFSO.FileExists(sRcPath) = True) Then
iBlockType = NO_BLOCK
sKey1 = ""
sKey2 = ""
iPosition = 0
sCodePage = ""
Set oTextFile = oFSO.OpenTextFile(sRcPath, ForReading, False, -1)
Do Until oTextFile.AtEndOfStream = True
sLine = Trim(oTextFile.ReadLine)
sValue = ""
If (FoundRegExpMatch(sLine, "(IDR_.*) MENU", oMatch) = True) Then 'MENU...
iBlockType = MENU_BLOCK
sKey1 = oMatch.SubMatches(0)
iPosition = 0
ElseIf (FoundRegExpMatch(sLine, "(IDD_.*) DIALOGEX", oMatch) = True) Then 'DIALOGEX...
iBlockType = DIALOGEX_BLOCK
sKey1 = oMatch.SubMatches(0)
iPosition = 0
ElseIf (sLine = "STRINGTABLE") Then 'STRINGTABLE...
iBlockType = STRINGTABLE_BLOCK
sKey1 = "STRINGTABLE"
'iPosition = 0
ElseIf (FoundRegExpMatch(sLine, "(VS_.*) VERSIONINFO", oMatch) = True) Then 'VERSIONINFO...
iBlockType = VERSIONINFO_BLOCK
sKey1 = "VERSIONINFO"
iPosition = 0
ElseIf (sLine = "END") Then 'END...
If (iBlockType = STRINGTABLE_BLOCK) Then 'If inside stringtable...
iBlockType = NO_BLOCK
sKey1 = ""
'iPosition = 0
End If
ElseIf (sLine <> "") Then 'If NOT empty line...
Select Case iBlockType
Case NO_BLOCK:
If (FoundRegExpMatch(sLine, "LANGUAGE (LANG_\w*), (SUBLANG_\w*)", oMatch) = True) Then 'LANGUAGE...
sLang = oMatch.SubMatches(0)
sSubLang = oMatch.SubMatches(1)
ElseIf (FoundRegExpMatch(sLine, "code_page\(([\d]+)\)", oMatch) = True) Then 'code_page...
sCodePage = oMatch.SubMatches(0)
End If
Case MENU_BLOCK:
If (FoundRegExpMatch(sLine, "POPUP ""(.*)""", oMatch) = True) Then 'POPUP...
If (InStr(oMatch.SubMatches(0), "_POPUP_") = 0) Then
sKey2 = iPosition
iPosition = iPosition + 1
sValue = oMatch.SubMatches(0)
End If
ElseIf (FoundRegExpMatch(sLine, "MENUITEM.*""(.*)"".*(ID_.*)", oMatch) = True) Then 'MENUITEM...
sKey2 = oMatch.SubMatches(1)
sValue = oMatch.SubMatches(0)
End If
Case DIALOGEX_BLOCK:
If (FoundRegExpMatch(sLine, "CAPTION.*""(.*)""", oMatch) = True) Then 'CAPTION...
sKey2 = "CAPTION"
sValue = oMatch.SubMatches(0)
ElseIf (FoundRegExpMatch(sLine, "PUSHBUTTON.*""(.*)"",(\w+)", oMatch) = True) Then 'DEFPUSHBUTTON/PUSHBUTTON...
sKey2 = oMatch.SubMatches(1)
sValue = oMatch.SubMatches(0)
ElseIf (FoundRegExpMatch(sLine, "[L|R|C]TEXT.*""(.*)"",(\w+)", oMatch) = True) Then 'LTEXT/RTEXT...
If (oMatch.SubMatches(0) <> "") And (oMatch.SubMatches(0) <> "Static") Then
If (oMatch.SubMatches(1) <> "IDC_STATIC") Then
sKey2 = oMatch.SubMatches(1)
Else
sKey2 = iPosition & "_TEXT"
iPosition = iPosition + 1
End If
sValue = oMatch.SubMatches(0)
End If
ElseIf (FoundRegExpMatch(sLine, "[L|R]TEXT.*""(.*)"",", oMatch) = True) Then 'LTEXT/RTEXT (without ID)...
sKey2 = iPosition & "_TEXT"
iPosition = iPosition + 1
sValue = oMatch.SubMatches(0)
ElseIf (FoundRegExpMatch(sLine, "CONTROL +""(.*?)"",(\w+)", oMatch) = True) Then 'CONTROL...
If (oMatch.SubMatches(0) <> "Dif") And (oMatch.SubMatches(0) <> "Btn") And (oMatch.SubMatches(0) <> "Button1") Then
sKey2 = oMatch.SubMatches(1)
sValue = oMatch.SubMatches(0)
End If
ElseIf (FoundRegExpMatch(sLine, "CONTROL +""(.*?)"",", oMatch) = True) Then 'CONTROL (without ID)...
sKey2 = iPosition & "_CONTROL"
iPosition = iPosition + 1
sValue = oMatch.SubMatches(0)
ElseIf (FoundRegExpMatch(sLine, "GROUPBOX +""(.*?)"",(\w+)", oMatch) = True) Then 'GROUPBOX...
If (oMatch.SubMatches(1) <> "IDC_STATIC") Then
sKey2 = oMatch.SubMatches(1)
Else
sKey2 = iPosition & "_GROUPBOX"
iPosition = iPosition + 1
End If
sValue = oMatch.SubMatches(0)
End If
Case STRINGTABLE_BLOCK:
If (FoundRegExpMatch(sLine, "(\w+).*""(.*)""", oMatch) = True) Then 'String...
sKey2 = oMatch.SubMatches(0)
sValue = oMatch.SubMatches(1)
ElseIf (FoundRegExpMatch(sLine, """(.*)""", oMatch) = True) Then 'String (without ID)...
sKey2 = iPosition
iPosition = iPosition + 1
sValue = oMatch.SubMatches(0)
End If
Case VERSIONINFO_BLOCK:
If (FoundRegExpMatch(sLine, "BLOCK ""([0-9A-F]+)""", oMatch) = True) Then 'StringFileInfo.Block...
sKey2 = "STRINGFILEINFO_BLOCK"
sValue = oMatch.SubMatches(0)
ElseIf (FoundRegExpMatch(sLine, "VALUE ""(.*?)"", ""(.*?)\\?0?""", oMatch) = True) Then 'StringFileInfo.Value...
sKey2 = "STRINGFILEINFO_" & oMatch.SubMatches(0)
sValue = oMatch.SubMatches(1)
ElseIf (FoundRegExpMatch(sLine, "VALUE ""Translation"", (.*?)$", oMatch) = True) Then 'VarFileInfo.Translation...
sKey2 = "VARFILEINFO_TRANSLATION"
sValue = oMatch.SubMatches(0)
End If
End Select
End If
If (sValue <> "") Then
Dim key
key = sKey1 & "." & sKey2
If (not oTranslations.Exists(key)) Then
oTranslations.Add key, sValue
Else
Dim newKey, oldValue, iNum
iNum = 1
newKey = key
Do Until not oTranslations.Exists(newKey)
oldValue = oTranslations(newKey)
iNum = iNum + 1
newKey = key & iNum
Loop
oTranslations.Add newKey, sValue
End If
End If
Loop
oTextFile.Close
oTranslations.Add "__LANGUAGE__", sLang & ", " & sSubLang
oTranslations.Add "__CODEPAGE__", sCodePage
End If
Set GetTranslationsFromRcFile = oTranslations
End Function
''
' ...
Function MergeTranslations(ByVal oOriginalTranslations, ByVal oLanguageTranslations)
Dim oMergedTranslations, sKey
Dim sOriginalTranslation, sLanguageTranslation
Set oMergedTranslations = CreateObject("Scripting.Dictionary")
For Each sKey In oOriginalTranslations.Keys 'For all original translations...
sOriginalTranslation = oOriginalTranslations(sKey)
sLanguageTranslation = oLanguageTranslations(sKey)
If (sOriginalTranslation <> "") And (sOriginalTranslation <> sLanguageTranslation) Then
If (oMergedTranslations.Exists(sOriginalTranslation) = False) Then
oMergedTranslations.Add oOriginalTranslations(sKey), oLanguageTranslations(sKey)
End If
End If
Next
oMergedTranslations.Add "__CODEPAGE__", oLanguageTranslations("__CODEPAGE__")
Set MergeTranslations = oMergedTranslations
End Function
''
' ...
Sub CreatePoFileWithTranslations(ByVal sMasterPotPath, ByVal sLanguagePoPath, ByVal oTranslations)
Dim oMasterPotFile, sMasterLine
Dim oLanguagePoFile, sLanguageLine
Dim oMatch, sMsgId, sMsgStr, sKey
If (oFSO.FileExists(sMasterPotPath) = True) Then 'If the master POT file exists...
sMsgId = ""
sMsgStr = ""
Set oMasterPotFile = oFSO.OpenTextFile(sMasterPotPath, ForReading, False, -1)
Set oLanguagePoFile = oFSO.CreateTextFile(sLanguagePoPath, True, True)
Do Until oMasterPotFile.AtEndOfStream = True 'For all lines...
sMasterLine = oMasterPotFile.ReadLine
sLanguageLine = sMasterLine
If (Trim(sMasterLine) <> "") Then 'If NOT empty line...
If (FoundRegExpMatch(sMasterLine, "msgid ""(.*)""", oMatch) = True) Then 'If "msgid"...
sMsgId = oMatch.SubMatches(0)
If (oTranslations.Exists(sMsgId) = True) Then 'If translation located...
sMsgStr = oTranslations(sMsgId)
End If
ElseIf (FoundRegExpMatch(sMasterLine, "msgstr """"", oMatch) = True) Then 'If "msgstr"...
If (sMsgId = "1252") And (sMsgStr = "") Then 'If same codepage...
sMsgStr = oTranslations("__CODEPAGE__")
End If
If (sMsgStr <> "") Then 'If translated...
sLanguageLine = Replace(sMasterLine, "msgstr """"", "msgstr """ & sMsgStr & """")
End If
ElseIf (FoundRegExpMatch(sMasterLine, "CP1252", oMatch) = True) Then 'If "Codepage"...
sLanguageLine = Replace(sMasterLine, "CP1252", "CP" & oTranslations("__CODEPAGE__"))
ElseIf (FoundRegExpMatch(sMasterLine, "English", oMatch) = True) Then 'If "English"...
sLanguageLine = Replace(sMasterLine, "English", oFSO.GetBaseName(sLanguagePoPath))
End If
Else 'If empty line
sMsgId = ""
sMsgStr = ""
End If
oLanguagePoFile.WriteLine sLanguageLine
Loop
oMasterPotFile.Close
oLanguagePoFile.Close
End If
End Sub
''
' ...
Function FoundRegExpMatch(ByVal sString, ByVal sPattern, ByRef oMatchReturn)
Dim oRegExp, oMatches
Set oRegExp = New RegExp
oRegExp.Pattern = sPattern
oRegExp.IgnoreCase = True
oMatchReturn = Null
FoundRegExpMatch = False
If (oRegExp.Test(sString) = True) Then
Set oMatches = oRegExp.Execute(sString)
Set oMatchReturn = oMatches(0)
FoundRegExpMatch = True
End If
End Function
|