mirror of
https://github.com/marcogll/passkit-generator.git
synced 2026-03-15 21:25:26 +00:00
Changed .localize method to need mandatory translations to be passed as second parameter
This commit is contained in:
@@ -584,39 +584,60 @@ describe("PKPass", () => {
|
|||||||
|
|
||||||
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
|
||||||
expect(() => pass.localize(null)).toThrowError(
|
expect(() => pass.localize(null)).toThrowError(
|
||||||
TypeError,
|
TypeError,
|
||||||
Messages.LANGUAGES.INVALID_TYPE.replace("%s", "object"),
|
Messages.LANGUAGES.INVALID_LANG.replace("%s", "object"),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// @ts-expect-error
|
||||||
expect(() => pass.localize(undefined)).toThrowError(
|
expect(() => pass.localize(undefined)).toThrowError(
|
||||||
TypeError,
|
TypeError,
|
||||||
Messages.LANGUAGES.INVALID_TYPE.replace("%s", "undefined"),
|
Messages.LANGUAGES.INVALID_LANG.replace("%s", "undefined"),
|
||||||
);
|
);
|
||||||
|
|
||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
expect(() => pass.localize(5)).toThrowError(
|
expect(() => pass.localize(5)).toThrowError(
|
||||||
TypeError,
|
TypeError,
|
||||||
Messages.LANGUAGES.INVALID_TYPE.replace("%s", "number"),
|
Messages.LANGUAGES.INVALID_LANG.replace("%s", "number"),
|
||||||
);
|
);
|
||||||
|
|
||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
expect(() => pass.localize(true)).toThrowError(
|
expect(() => pass.localize(true)).toThrowError(
|
||||||
TypeError,
|
TypeError,
|
||||||
Messages.LANGUAGES.INVALID_TYPE.replace("%s", "boolean"),
|
Messages.LANGUAGES.INVALID_LANG.replace("%s", "boolean"),
|
||||||
);
|
);
|
||||||
|
|
||||||
// @ts-expect-error
|
// @ts-expect-error
|
||||||
expect(() => pass.localize({})).toThrowError(
|
expect(() => pass.localize({})).toThrowError(
|
||||||
TypeError,
|
TypeError,
|
||||||
Messages.LANGUAGES.INVALID_TYPE.replace("%s", "object"),
|
Messages.LANGUAGES.INVALID_LANG.replace("%s", "object"),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should create a new language record inside class props", () => {
|
it("should warn developer if no translations have been passed", () => {
|
||||||
|
console.warn = jasmine.createSpy("log");
|
||||||
|
// @ts-expect-error
|
||||||
pass.localize("en");
|
pass.localize("en");
|
||||||
|
pass.localize("en", {});
|
||||||
|
|
||||||
expect(pass[localizationSymbol]["en"]).toEqual({});
|
expect(console.warn).toHaveBeenCalledWith(
|
||||||
|
Messages.LANGUAGES.NO_TRANSLATIONS.replace("%s", "en"),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(console.warn).toHaveBeenCalledTimes(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should create a new language record if some translations are specifies", () => {
|
||||||
|
pass.localize("en", {
|
||||||
|
buon_giorno: "Good Morning",
|
||||||
|
buona_sera: "Good Evening",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(pass[localizationSymbol]["en"]).toEqual({
|
||||||
|
buon_giorno: "Good Morning",
|
||||||
|
buona_sera: "Good Evening",
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should accept later translations and merge them with existing ones", () => {
|
it("should accept later translations and merge them with existing ones", () => {
|
||||||
@@ -647,9 +668,13 @@ describe("PKPass", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("should always return undefined", () => {
|
it("should always return undefined", () => {
|
||||||
expect(pass.localize("it", undefined)).toBeUndefined();
|
|
||||||
expect(pass.localize("it", null)).toBeUndefined();
|
expect(pass.localize("it", null)).toBeUndefined();
|
||||||
expect(pass.localize("it", {})).toBeUndefined();
|
expect(
|
||||||
|
pass.localize("it", {
|
||||||
|
say_good_morning: "buongiorno",
|
||||||
|
say_good_evening: "buonasera",
|
||||||
|
}),
|
||||||
|
).toBeUndefined();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -669,8 +669,8 @@ export default class PKPass extends Bundle {
|
|||||||
// ************************** //
|
// ************************** //
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allows to specify a language to be added to the
|
* Allows to add a localization details to the
|
||||||
* final bundle, along with some optionals translations.
|
* final bundle with some translations.
|
||||||
*
|
*
|
||||||
* If the language already exists, translations will be
|
* If the language already exists, translations will be
|
||||||
* merged with the existing ones.
|
* merged with the existing ones.
|
||||||
@@ -685,11 +685,11 @@ export default class PKPass extends Bundle {
|
|||||||
|
|
||||||
public localize(
|
public localize(
|
||||||
lang: string,
|
lang: string,
|
||||||
translations?: { [key: string]: string } | null,
|
translations: { [key: string]: string } | null,
|
||||||
) {
|
) {
|
||||||
if (typeof lang !== "string") {
|
if (typeof lang !== "string") {
|
||||||
throw new TypeError(
|
throw new TypeError(
|
||||||
formatMessage(Messages.LANGUAGES.INVALID_TYPE, typeof lang),
|
formatMessage(Messages.LANGUAGES.INVALID_LANG, typeof lang),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -698,6 +698,13 @@ export default class PKPass extends Bundle {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!translations || !Object.keys(translations).length) {
|
||||||
|
console.warn(
|
||||||
|
formatMessage(Messages.LANGUAGES.NO_TRANSLATIONS, lang),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
this[localizationSymbol][lang] ??= {};
|
this[localizationSymbol][lang] ??= {};
|
||||||
|
|
||||||
if (typeof translations === "object" && !Array.isArray(translations)) {
|
if (typeof translations === "object" && !Array.isArray(translations)) {
|
||||||
|
|||||||
@@ -36,6 +36,8 @@ export const DATE = {
|
|||||||
export const LANGUAGES = {
|
export const LANGUAGES = {
|
||||||
INVALID_TYPE:
|
INVALID_TYPE:
|
||||||
"Cannot set localization. Expected a string for 'lang' but received %s",
|
"Cannot set localization. Expected a string for 'lang' but received %s",
|
||||||
|
NO_TRANSLATIONS:
|
||||||
|
"Cannot create or use language %s. If your itention was to just add a language (.lproj) folder to the bundle, both specify some translations or use .addBuffer to add some media.",
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
export const BARCODES = {
|
export const BARCODES = {
|
||||||
|
|||||||
Reference in New Issue
Block a user