-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcreatestringspo_dlc.lua
292 lines (235 loc) · 8.51 KB
/
createstringspo_dlc.lua
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
--
-- Revised version by WrathOf using msgctxt field for po/t file
-- msgctxt is set to the "path" in the table structure which is guaranteed unique
-- versus the string values (msgid) which are not.
--
-- Expanded to support v1 and v2 format in event user needs help converting a
-- strings.lua based translation. Must use a mod to load the translated strings.lua
-- file into a seperate environment and then pass the alternate strings table to
-- CreateStringsPOT along with the actual global strings table for use as a lookup table.
--
-- prefer loading scripts from DLC directory, only afterwards get from base
package.path = "..\\DLC0001\\scripts\\?.lua;..\\DLC0002\\scripts\\?.lua;?.lua"
-- In case strings.lua queries this (tgus needs to be before the require)
function IsDLCEnabled(val)
if val == REIGN_OF_GIANTS then
return true
end
end
require "strings"
require "io"
local REIGN_OF_GIANTS = 1
local path = "STRINGS"
--Used by v1
local msgids = {}
--Used by translated string functions
local STRINGS_LOOKUP = {}
--
-- Version 1 msgid based, original
--
local function PrintStringTableV1( base, tbl, file )
for k,v in pairs(tbl) do
local path = base.."."..k
if type(v) == "table" then
PrintStringTableV1(path, v, file)
else
local str = string.gsub(v, "\n", "\\n")
str = string.gsub(str, "\r", "\\r")
str = string.gsub(str, "\"", "\\\"")
if msgids[str] then
print("duplicate msgid found: "..str.." (skipping...)")
else
msgids[str] = true
file:write("#. "..path)
file:write("\n")
file:write("#: "..path)
file:write("\n")
file:write("msgid \""..str.."\"")
file:write("\n")
file:write("msgstr \"\"")
file:write("\n\n")
end
end
end
end
function PrintTranslatedStringTableV1( base_dta, tbl_dta, lkp_var, file )
for k,v in pairs(tbl_dta) do
local path = base_dta.."."..k
if type(v) == "table" then
PrintTranslatedStringTableV1(path, v, lkp_var, file)
else
local idstr = LookupIdValue(lkp_var, path)
if idstr then
idstr = string.gsub(idstr, "\n", "\\n")
idstr = string.gsub(idstr, "\r", "\\r")
idstr = string.gsub(idstr, "\"", "\\\"")
else
idstr = ""
end
local str = v
str = string.gsub(str, "\n", "\\n")
str = string.gsub(str, "\r", "\\r")
str = string.gsub(str, "\"", "\\\"")
if idstr ~= "" and msgids[idstr] then
print("duplicate msgid found: "..idstr.." (skipping...)")
else
msgids[idstr] = true
file:write("#. "..path)
file:write("\n")
file:write("#: "..path)
file:write("\n")
file:write("msgid \""..idstr.."\"")
file:write("\n")
file:write("msgstr \""..str.."\"")
file:write("\n\n")
end
end
end
end
local function IsValidString( str )
for i = 1, #str do
local a = string.byte( str, i, i)
if a < 32 or a > 127 then
return false
end
end
return true
end
--
-- Version 2 msgctxt based
--
--Recursive function to process table structure
local function PrintStringTableV2( base, tbl, file )
for k,v in pairs(tbl) do
local path = base.."."..k
if type(v) == "table" then
PrintStringTableV2(path, v, file)
else
local str = string.gsub(v, "\n", "\\n")
str = string.gsub(str, "\r", "\\r")
str = string.gsub(str, "\"", "\\\"")
if IsValidString( str ) then
file:write("#. "..path)
file:write("\n")
file:write("msgctxt \""..path.."\"")
file:write("\n")
file:write("msgid \""..str.."\"")
file:write("\n")
file:write("msgstr \"\"")
file:write("\n\n")
end
end
end
end
local function PrintTranslatedStringTableV2( base, tbl_dta, lkp_var, file )
for k,v in pairs(tbl_dta) do
local path = base.."."..k
if type(v) == "table" then
PrintTranslatedStringTableV2(path, v, lkp_var, file)
else
local idstr = LookupIdValue(lkp_var, path)
if idstr then
idstr = string.gsub(idstr, "\n", "\\n")
idstr = string.gsub(idstr, "\r", "\\r")
idstr = string.gsub(idstr, "\"", "\\\"")
else
idstr = ""
end
local str = v
str = string.gsub(str, "\n", "\\n")
str = string.gsub(str, "\r", "\\r")
str = string.gsub(str, "\"", "\\\"")
if idstr ~= "" then
file:write("#. "..path)
file:write("\n")
file:write("msgctxt \""..path.."\"")
file:write("\n")
file:write("msgid \""..idstr.."\"")
file:write("\n")
file:write("msgstr \""..str.."\"")
file:write("\n\n")
end
end
end
end
--LookupIdValue (common function)
-- lkp_var = name of variable holding lookup table as string
-- path = dot delimited indexes into the lookup table, first token assumed
-- to be original table var name and substituted with lkp_var
-- returns value stored in lookup table variable if found, otherwise nil
--
local function LookupIdValue(lkp_var, path)
--print(lkp_var, path)
--capture original tbl var name and remaining indexes
--OLD_TBL_VAR.LEVEL1.LEVEL2.LEVEL3 --> OLD_TBL_VAR, LEVEL1.LEVEL2.LEVEL3.1
local sidx, eidx, tblvar, str = string.find(path, "([^%.]*).(.*)")
--attempt to capture ending numeric index, store and remove if found
local sidx, eidx, endnum1 = string.find(str, "%.(%d*)$")
if endnum1 then str = string.sub(str,1,string.len(str)-string.len(endnum1)-1) end
--attempt to capture second ending numeric index, store and remove if found
local sidx, eidx, endnum2 = string.find(str, "%.(%d*)$")
if endnum2 then str = string.sub(str,1,string.len(str)-string.len(endnum2)-1) end
--replace dots with bracket quote syntax
--LEVEL1.LEVEL2.LEVEL3 --> LEVEL1"]["LEVEL2"]["LEVEL3
str = string.gsub(str, "%.", "\"][\"")
--build eval string for returning value in lookup table using bracket quote syntax
--'return LKP_TBL_VAR["LEVEL1"]["LEVEL2"]["LEVEL3"]'
local evalstr = "return "..lkp_var.."[\""..str.."\"]"
if endnum2 then evalstr = evalstr.."["..endnum2.."]" end
if endnum1 then evalstr = evalstr.."["..endnum1.."]" end
local result, val = pcall(function() return loadstring(evalstr)() end)
--print(evalstr,result,val)
if result and type(val) == "string" then return val else return nil end
end
--
-- Public functions
--
function CreateStringsPOTv1(filename, root, tbl_dta, tbl_lkp)
filename = filename or "data\\scripts\\languages\\temp_v1.pot"
root = root or "STRINGS"
local file = io.open(filename, "w")
if tbl_lkp then
STRINGS_LOOKUP = tbl_lkp
PrintTranslatedStringTableV1( root, tbl_dta, "STRINGS_LOOKUP", file )
else
PrintStringTablev1( root, tbl_dta, file )
end
file:close()
end
function CreateStringsPOTv2(filename, root, tbl_dta, tbl_lkp)
filename = filename or "data\\DLC0001\\scripts\\languages\\temp_v2.pot"
root = root or "STRINGS"
local file = io.open(filename, "w")
--Add file format info
file:write("\"Application: Dont' Starve\\n\"")
file:write("\n")
file:write("\"POT Version: 2.0\\n\"")
file:write("\n")
file:write("\n")
if tbl_lkp then
STRINGS_LOOKUP = tbl_lkp
PrintTranslatedStringTableV2( root, tbl_dta, "STRINGS_LOOKUP", file )
else
PrintStringTableV2( root, tbl_dta, file )
end
file:close()
end
-- *** INSTRUCTIONS ***
-- To generate strings for Reign of Giants (DLC):
-- 1. Backup the speech_[character].lua files in DontStarve\data\scripts
-- 2. Copy the speech_[character].lua files in DontStarve\data\DLC0001\scripts and paste them into DontStarve\data\scripts
-- 3. Open strings.lua and find the STRINGS.CHARACTERS table.
-- Add "WATHGRITHR = require "speech_wathgrithr"" and "WEBBER = require "speech_webber"" (without outer quotes) to the table and save the file.
-- 4. Open cmd and navigate to the DontStarve\data\scripts folder
-- 5. Enter "..\..\tools\LUA\lua.exe createstringspo_dlc.lua" (without quotes) into the cmd line and press return
-- 6. Undo your changes to strings.lua and restore the backed up speech files to their previous location in data\scripts
--Only run if this is not the released game version
--if BRANCH == "release" then
print("Generating PO/T files from strings table....")
--Create POT file for STRINGS table in original v1 format
--CreateStringsPOTv1("data\\scripts\\languages\\strings_v1.pot", "STRINGS", STRINGS)
--Create POT file for STRINGS table in new v2 format
CreateStringsPOTv2("..\\DLC0001\\scripts\\languages\\strings.pot", "STRINGS", STRINGS)
--Create english.po file for new translations (or just copy the pot file create above, not sure how from lua)
--CreateStringsPOTv2("languages\\english.po", "STRINGS", STRINGS)
--end