Added languages getter

This commit is contained in:
Alexander Cerutti
2021-10-31 20:54:31 +01:00
parent 7a417e45c5
commit a0cdc218dd
4 changed files with 52 additions and 12 deletions

View File

@@ -8,8 +8,6 @@ import { app } from "./webserver";
import { getCertificates } from "./shared"; import { getCertificates } from "./shared";
import path from "path"; import path from "path";
import { PKPass } from "passkit-generator"; import { PKPass } from "passkit-generator";
/** Symbols are exported just for tests and examples. Replicate only if really needed. */
import { localizationSymbol } from "passkit-generator/lib/PKPass";
app.route("/localize/:modelName").get(async (request, response) => { app.route("/localize/:modelName").get(async (request, response) => {
const passName = const passName =
@@ -51,10 +49,7 @@ app.route("/localize/:modelName").get(async (request, response) => {
// This language does not exist but is still added as .lproj folder // This language does not exist but is still added as .lproj folder
pass.localize("zu", {}); pass.localize("zu", {});
console.log( console.log("Added languages", Object.keys(pass.languages).join(", "));
"Added languages",
Object.keys(pass[localizationSymbol]).join(", "),
);
const stream = pass.getAsStream(); const stream = pass.getAsStream();

View File

@@ -1,7 +1,6 @@
import { finish400WithoutModelName, createPassGenerator } from "../shared"; import { finish400WithoutModelName, createPassGenerator } from "../shared";
import type { ALBEvent, ALBResult } from "aws-lambda"; import type { ALBEvent, ALBResult } from "aws-lambda";
import type { PKPass } from "passkit-generator"; import type { PKPass } from "passkit-generator";
import { localizationSymbol } from "passkit-generator/lib/PKPass";
/** /**
* Lambda for localize example * Lambda for localize example
@@ -36,10 +35,7 @@ export async function localize(event: ALBEvent) {
LOCATION: "plassering", LOCATION: "plassering",
}); });
console.log( console.log("Added languages", Object.keys(pass.languages).join(", "));
"Added languages",
Object.keys(pass[localizationSymbol]).join(", "),
);
return (await passGenerator.next(pass as PKPass)).value as ALBResult; return (await passGenerator.next(pass as PKPass)).value as ALBResult;
} }

View File

@@ -417,7 +417,7 @@ describe("PKPass", () => {
}); });
}); });
describe("certificates setter", () => { describe("certificates", () => {
it("should throw an error if certificates provided are not complete or invalid", () => { it("should throw an error if certificates provided are not complete or invalid", () => {
expect(() => { expect(() => {
// @ts-expect-error // @ts-expect-error
@@ -583,6 +583,47 @@ describe("PKPass", () => {
}); });
}); });
describe("languages", () => {
it("should get updated when translations gets added through localize", () => {
expect(pass.languages.length).toBe(0);
expect(pass.languages).toEqual([]);
pass.localize("en", {
buon_giorno: "Good Morning",
buona_sera: "Good Evening",
});
expect(pass.languages.length).toBe(1);
expect(pass.languages).toEqual(["en"]);
});
it("should get updated when translations are added through .addBuffer", () => {
const validTranslationStringsEN = `
/* Insert Element menu item */
"Insert Element" = "Insert Element";
/* Error string used for unknown error types. */
"ErrorString_1" = "An unknown error occurred.";
`;
pass.addBuffer(
"en.lproj/pass.strings",
Buffer.from(validTranslationStringsEN),
);
const validTranslationStringsIT = `
"Insert Element" = "Inserisci elemento";
"ErrorString_1" = "Un errore sconosciuto è accaduto";
`;
pass.addBuffer(
"it.lproj/pass.strings",
Buffer.from(validTranslationStringsIT),
);
expect(pass.languages).toEqual(["en", "it"]);
});
});
describe("localize", () => { describe("localize", () => {
it("should fail throw if lang is not a string", () => { it("should fail throw if lang is not a string", () => {
// @ts-expect-error // @ts-expect-error

View File

@@ -179,6 +179,14 @@ export default class PKPass extends Bundle {
this[certificatesSymbol] = certs; this[certificatesSymbol] = certs;
} }
/**
* Allows retrieving current languages
*/
public get languages() {
return Object.keys(this[localizationSymbol]);
}
/** /**
* Allows getting an image of the props * Allows getting an image of the props
* that are composing your pass instance. * that are composing your pass instance.