Added localize implementation along with its tests

This commit is contained in:
Alexander Cerutti
2021-09-25 21:41:06 +02:00
parent 7a23cba582
commit 344c33c54c
2 changed files with 75 additions and 14 deletions

View File

@@ -1,4 +1,8 @@
import { default as PKPass } from "../lib/PKPass";
import {
default as PKPass,
localizationSymbol,
propsSymbol,
} from "../lib/PKPass";
describe("PKPass", () => {
describe("setBeacons", () => {
@@ -389,4 +393,48 @@ describe("PKPass", () => {
expect(passCP.transitType).toBeUndefined(); */
});
});
describe("localize", () => {
const pass = new PKPass({}, {}, {});
it("should create a new language record inside class props", () => {
pass.localize("en");
expect(pass[localizationSymbol]["en"]).toEqual({});
});
it("should save some translations to be exported later", () => {
pass.localize("it", {
say_hi: "ciao",
say_gb: "arrivederci",
});
expect(pass[localizationSymbol]["it"]).toEqual({
say_hi: "ciao",
say_gb: "arrivederci",
});
});
it("should accept later translations and merge them with existing ones", () => {
pass.localize("it", {
say_good_morning: "buongiorno",
say_good_evening: "buonasera",
});
expect(pass[localizationSymbol]["it"]).toEqual({
say_hi: "ciao",
say_gb: "arrivederci",
say_good_morning: "buongiorno",
say_good_evening: "buonasera",
});
});
it("should delete a language and its all translations when null is passed as parameter", () => {
pass.localize("it", null);
pass.localize("en", null);
expect(pass[localizationSymbol]["it"]).toBeUndefined();
expect(pass[localizationSymbol]["en"]).toBeUndefined();
});
});
});

View File

@@ -5,10 +5,12 @@ import { getModelFolderContents } from "./parser";
import * as Schemas from "./schemas";
import { Stream } from "stream";
/** Exporting for tests specs */
export const propsSymbol = Symbol("props");
export const localizationSymbol = Symbol("pass.l10n");
const fieldKeysPoolSymbol = Symbol("fieldKeysPoolSymbol");
const propsSymbol = Symbol("props");
const importMetadataSymbol = Symbol("import.pass.metadata");
const localizationSymbol = Symbol("pass.l10n");
interface NamedBuffers {
[key: string]: Buffer;
@@ -363,6 +365,7 @@ export default class PKPass extends Bundle {
* @TODO compile this pass into something usable
* @TODO like _patch on old version
* @TODO share implementation with getAsStream
* @TODO Build back translations
*/
return super.getAsBuffer();
@@ -381,6 +384,7 @@ export default class PKPass extends Bundle {
* @TODO compile this pass into something usable
* @TODO like _patch on old version
* @TODO share implementation with getAsBuffer
* @TODO Build back translations
*/
return super.getAsStream();
@@ -392,24 +396,33 @@ export default class PKPass extends Bundle {
/**
* Allows to specify a language to be added to the
* final bundle, along with some optionals / additional
* translations.
* final bundle, along with some optionals translations.
*
* If the language already exists in the origin source,
* translations will be added to the existing ones.
* If the language already exists, translations will be
* merged with the existing ones.
*
* Setting `translations` to `null`, fully deletes a language
* and its translations.
*
* @see https://developer.apple.com/documentation/walletpasses/creating_the_source_for_a_pass#3736718
* @param lang
* @param translations
*/
localize(lang: string, translations?: any): this {
/**
* @TODO change translations format
* @TODO specify a way to get current ones deleted
* @TODO Default languages from source
* @TODO print warning if lang is already in selection?
*/
localize(
lang: string,
translations?: { [key: string]: string } | null,
): this {
if (translations === null) {
delete this[localizationSymbol][lang];
return this;
}
this[localizationSymbol][lang] ??= {};
if (typeof translations === "object" && !Array.isArray(translations)) {
Object.assign(this[localizationSymbol][lang], translations);
}
return this;
}