From a0cdc218dd335e5a535572377e40d35f0d7e41df Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Sun, 31 Oct 2021 20:54:31 +0100 Subject: [PATCH] Added languages getter --- examples/self-hosted/src/localize.ts | 7 +-- examples/serverless/src/functions/localize.ts | 6 +-- spec/PKPass.ts | 43 ++++++++++++++++++- src/PKPass.ts | 8 ++++ 4 files changed, 52 insertions(+), 12 deletions(-) diff --git a/examples/self-hosted/src/localize.ts b/examples/self-hosted/src/localize.ts index 5d18947..82f0ca7 100644 --- a/examples/self-hosted/src/localize.ts +++ b/examples/self-hosted/src/localize.ts @@ -8,8 +8,6 @@ import { app } from "./webserver"; import { getCertificates } from "./shared"; import path from "path"; 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) => { 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 pass.localize("zu", {}); - console.log( - "Added languages", - Object.keys(pass[localizationSymbol]).join(", "), - ); + console.log("Added languages", Object.keys(pass.languages).join(", ")); const stream = pass.getAsStream(); diff --git a/examples/serverless/src/functions/localize.ts b/examples/serverless/src/functions/localize.ts index 605ccc8..59c0783 100644 --- a/examples/serverless/src/functions/localize.ts +++ b/examples/serverless/src/functions/localize.ts @@ -1,7 +1,6 @@ import { finish400WithoutModelName, createPassGenerator } from "../shared"; import type { ALBEvent, ALBResult } from "aws-lambda"; import type { PKPass } from "passkit-generator"; -import { localizationSymbol } from "passkit-generator/lib/PKPass"; /** * Lambda for localize example @@ -36,10 +35,7 @@ export async function localize(event: ALBEvent) { LOCATION: "plassering", }); - console.log( - "Added languages", - Object.keys(pass[localizationSymbol]).join(", "), - ); + console.log("Added languages", Object.keys(pass.languages).join(", ")); return (await passGenerator.next(pass as PKPass)).value as ALBResult; } diff --git a/spec/PKPass.ts b/spec/PKPass.ts index a553cca..d66491f 100644 --- a/spec/PKPass.ts +++ b/spec/PKPass.ts @@ -417,7 +417,7 @@ describe("PKPass", () => { }); }); - describe("certificates setter", () => { + describe("certificates", () => { it("should throw an error if certificates provided are not complete or invalid", () => { expect(() => { // @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", () => { it("should fail throw if lang is not a string", () => { // @ts-expect-error diff --git a/src/PKPass.ts b/src/PKPass.ts index 5870328..498d71a 100644 --- a/src/PKPass.ts +++ b/src/PKPass.ts @@ -179,6 +179,14 @@ export default class PKPass extends Bundle { this[certificatesSymbol] = certs; } + /** + * Allows retrieving current languages + */ + + public get languages() { + return Object.keys(this[localizationSymbol]); + } + /** * Allows getting an image of the props * that are composing your pass instance.