-
-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathlocalize.ts
More file actions
84 lines (69 loc) · 2.01 KB
/
localize.ts
File metadata and controls
84 lines (69 loc) · 2.01 KB
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
/**
* .localize() methods example
* To see all the included languages, you have to unzip the
* .pkpass file and check for .lproj folders
*/
import path from "node:path";
import { fileURLToPath } from "node:url";
import { PKPass } from "passkit-generator";
import { app } from "./webserver.js";
import { getCertificates } from "./shared.js";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
app.route("/localize/:modelName").get(async (request, response) => {
const passName =
request.params.modelName +
"_" +
new Date().toISOString().split("T")[0].replace(/-/gi, "");
const certificates = await getCertificates();
try {
const pass = await PKPass.from(
{
model: path.resolve(
__dirname,
`../../models/${request.params.modelName}`,
),
certificates: {
wwdr: certificates.wwdr,
signerCert: certificates.signerCert,
signerKey: certificates.signerKey,
signerKeyPassphrase: certificates.signerKeyPassphrase,
},
},
Object.assign(
{
serialNumber: Math.random().toString(36).substring(2, 15),
},
request.body || request.query || {},
),
);
// Italian, already has an .lproj which gets included...
pass.localize("it", {
EVENT: "Evento",
LOCATION: "Dove",
});
// ...while German doesn't, so it gets created
pass.localize("de", {
EVENT: "Ereignis",
LOCATION: "Ort",
});
// This language does not exist but is still added as .lproj folder
pass.localize("zu", {});
console.log("Added languages", Object.keys(pass.languages).join(", "));
if (pass.type === "boardingPass" && !pass.transitType) {
// Just to not make crash the creation if we use a boardingPass
pass.transitType = "PKTransitTypeAir";
}
const stream = pass.getAsStream();
response.set({
"Content-type": pass.mimeType,
"Content-disposition": `attachment; filename=${passName}.pkpass`,
});
stream.pipe(response);
} catch (err) {
console.log(err);
response.set({
"Content-type": "text/html",
});
response.send(err.message);
}
});