mirror of
https://github.com/marcogll/passkit-generator.git
synced 2026-03-15 18:25:24 +00:00
Added localize implementation along with its tests
This commit is contained in:
@@ -1,4 +1,8 @@
|
|||||||
import { default as PKPass } from "../lib/PKPass";
|
import {
|
||||||
|
default as PKPass,
|
||||||
|
localizationSymbol,
|
||||||
|
propsSymbol,
|
||||||
|
} from "../lib/PKPass";
|
||||||
|
|
||||||
describe("PKPass", () => {
|
describe("PKPass", () => {
|
||||||
describe("setBeacons", () => {
|
describe("setBeacons", () => {
|
||||||
@@ -389,4 +393,48 @@ describe("PKPass", () => {
|
|||||||
expect(passCP.transitType).toBeUndefined(); */
|
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();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -5,10 +5,12 @@ import { getModelFolderContents } from "./parser";
|
|||||||
import * as Schemas from "./schemas";
|
import * as Schemas from "./schemas";
|
||||||
import { Stream } from "stream";
|
import { Stream } from "stream";
|
||||||
|
|
||||||
|
/** Exporting for tests specs */
|
||||||
|
export const propsSymbol = Symbol("props");
|
||||||
|
export const localizationSymbol = Symbol("pass.l10n");
|
||||||
|
|
||||||
const fieldKeysPoolSymbol = Symbol("fieldKeysPoolSymbol");
|
const fieldKeysPoolSymbol = Symbol("fieldKeysPoolSymbol");
|
||||||
const propsSymbol = Symbol("props");
|
|
||||||
const importMetadataSymbol = Symbol("import.pass.metadata");
|
const importMetadataSymbol = Symbol("import.pass.metadata");
|
||||||
const localizationSymbol = Symbol("pass.l10n");
|
|
||||||
|
|
||||||
interface NamedBuffers {
|
interface NamedBuffers {
|
||||||
[key: string]: Buffer;
|
[key: string]: Buffer;
|
||||||
@@ -363,6 +365,7 @@ export default class PKPass extends Bundle {
|
|||||||
* @TODO compile this pass into something usable
|
* @TODO compile this pass into something usable
|
||||||
* @TODO like _patch on old version
|
* @TODO like _patch on old version
|
||||||
* @TODO share implementation with getAsStream
|
* @TODO share implementation with getAsStream
|
||||||
|
* @TODO Build back translations
|
||||||
*/
|
*/
|
||||||
|
|
||||||
return super.getAsBuffer();
|
return super.getAsBuffer();
|
||||||
@@ -381,6 +384,7 @@ export default class PKPass extends Bundle {
|
|||||||
* @TODO compile this pass into something usable
|
* @TODO compile this pass into something usable
|
||||||
* @TODO like _patch on old version
|
* @TODO like _patch on old version
|
||||||
* @TODO share implementation with getAsBuffer
|
* @TODO share implementation with getAsBuffer
|
||||||
|
* @TODO Build back translations
|
||||||
*/
|
*/
|
||||||
|
|
||||||
return super.getAsStream();
|
return super.getAsStream();
|
||||||
@@ -392,24 +396,33 @@ export default class PKPass extends Bundle {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Allows to specify a language to be added to the
|
* Allows to specify a language to be added to the
|
||||||
* final bundle, along with some optionals / additional
|
* final bundle, along with some optionals translations.
|
||||||
* translations.
|
|
||||||
*
|
*
|
||||||
* If the language already exists in the origin source,
|
* If the language already exists, translations will be
|
||||||
* translations will be added to the existing ones.
|
* 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
|
* @see https://developer.apple.com/documentation/walletpasses/creating_the_source_for_a_pass#3736718
|
||||||
* @param lang
|
* @param lang
|
||||||
* @param translations
|
* @param translations
|
||||||
*/
|
*/
|
||||||
|
|
||||||
localize(lang: string, translations?: any): this {
|
localize(
|
||||||
/**
|
lang: string,
|
||||||
* @TODO change translations format
|
translations?: { [key: string]: string } | null,
|
||||||
* @TODO specify a way to get current ones deleted
|
): this {
|
||||||
* @TODO Default languages from source
|
if (translations === null) {
|
||||||
* @TODO print warning if lang is already in selection?
|
delete this[localizationSymbol][lang];
|
||||||
*/
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
this[localizationSymbol][lang] ??= {};
|
||||||
|
|
||||||
|
if (typeof translations === "object" && !Array.isArray(translations)) {
|
||||||
|
Object.assign(this[localizationSymbol][lang], translations);
|
||||||
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user