Added basic language support to CardCreator

This commit is contained in:
Carlo Morgenstern 2021-10-24 17:03:15 +02:00
parent 0a39d21f29
commit ad98ec4d74

View file

@ -81,6 +81,33 @@ const infoTextSmallStartY = rectStartRow3Y + infoTextStartSpacing;
const infoTextBigStartY = infoTextSmallStartY + 25; const infoTextBigStartY = infoTextSmallStartY + 25;
const infoTextSpacing = 50; const infoTextSpacing = 50;
const languageStrings = {
en: {
raceAndClan: 'Race & Clan',
guardian: 'Guardian',
grandCompany: 'Grand Company',
freeCompany: 'Free Company',
elementalLevel: 'Elemental Level',
eurekaLevel: 'Level',
resistanceRank: 'Resistance Rank',
bozjaRank: 'Rank',
mounts: 'Mounts',
minions: 'Minions',
},
de: {
raceAndClan: 'Volk & Stamm',
guardian: 'Schutzgott',
grandCompany: 'Staatliche Gesellschaft',
freeCompany: 'Freie Gesellschaft',
elementalLevel: 'Das Verbotene Land Eureka',
eurekaLevel: 'Elementarstufe',
resistanceRank: 'Bozja-Südfront',
bozjaRank: 'Widerstandsstufe',
mounts: 'Reittiere',
minions: 'Begleiter',
},
};
class CardCreator { class CardCreator {
/** /**
* Creates a new card creator. * Creates a new card creator.
@ -296,6 +323,7 @@ class CardCreator {
* The image should be the same resolution as the default image. The default image size can be * The image should be the same resolution as the default image. The default image size can be
* retrieved with {@link CardCreator#canvasSize}. May be a URL, `data: `URI, a local file path, * retrieved with {@link CardCreator#canvasSize}. May be a URL, `data: `URI, a local file path,
* or a Buffer instance. * or a Buffer instance.
* @param {string} [language] The language that the cards should be in use for the request
* @example * @example
* const fs = require("fs"); * const fs = require("fs");
* *
@ -310,14 +338,15 @@ class CardCreator {
* }); * });
* @returns {Promise<Buffer>} A promise representating the construction of the card's image data. * @returns {Promise<Buffer>} A promise representating the construction of the card's image data.
*/ */
async createCard(charaId, customImage) { async createCard(charaId, customImage, language = 'en') {
const characterInfoUrl = `https://xivapi.com/character/${charaId}?extended=1&data=FC,mimo`; const strings = Object.keys(languageStrings).includes(language) ? languageStrings[language] : languageStrings.en;
const characterInfoUrl = `https://xivapi.com/character/${charaId}?language=${language}&extended=1&data=FC,mimo`;
let response = await fetch(characterInfoUrl); let response = await fetch(characterInfoUrl);
if (!response.ok) { if (!response.ok) {
// Retry once if the request fails // Retry once if the request fails
response = await fetch(characterInfoUrl); response = await fetch(characterInfoUrl);
} }
const data = await response.json(); const data = await response.json();
const canvasSize = this.canvasSize; const canvasSize = this.canvasSize;
@ -370,16 +399,16 @@ class CardCreator {
// Race, Clan, Guardian, GC, FC Titles // Race, Clan, Guardian, GC, FC Titles
ctx.font = small; ctx.font = small;
ctx.textAlign = "left"; ctx.textAlign = "left";
ctx.fillText("Race & Clan", 480, infoTextSmallStartY); ctx.fillText(strings.raceAndClan, 480, infoTextSmallStartY);
ctx.fillText("Guardian", 480, infoTextSmallStartY + infoTextSpacing); ctx.fillText(strings.guardian, 480, infoTextSmallStartY + infoTextSpacing);
if (data.Character.GrandCompany.Company != null) { if (data.Character.GrandCompany.Company != null) {
ctx.fillText("Grand Company", 480, infoTextSmallStartY + infoTextSpacing * 2); ctx.fillText(strings.grandCompany, 480, infoTextSmallStartY + infoTextSpacing * 2);
} }
if (data.Character.FreeCompanyName != null) { if (data.Character.FreeCompanyName != null) {
ctx.fillText("Free Company", 480, infoTextSmallStartY + infoTextSpacing * 3); ctx.fillText(strings.freeCompany, 480, infoTextSmallStartY + infoTextSpacing * 3);
} }
ctx.fillText("Elemental Level", 480, 425); ctx.fillText(strings.elementalLevel, 480, 425);
ctx.fillText("Resistance Rank", 480, 475); ctx.fillText(strings.resistanceRank, 480, 475);
ctx.fillStyle = grey; ctx.fillStyle = grey;
@ -410,7 +439,7 @@ class CardCreator {
ctx.drawImage(deityIcon, deityIconX, deityIconY, 28, 28); ctx.drawImage(deityIcon, deityIconX, deityIconY, 28, 28);
if (data.Character.GrandCompany.Company != null) { if (data.Character.GrandCompany.Company != null) {
ctx.fillText(data.Character.GrandCompany.Company.Name, 480, infoTextBigStartY + infoTextSpacing * 2); ctx.fillText(data.Character.GrandCompany.Company.Name.replace('[p]', ''), 480, infoTextBigStartY + infoTextSpacing * 2);
var gcRankIcon = await loadImage('https://xivapi.com/' + data.Character.GrandCompany.Rank.Icon); var gcRankIcon = await loadImage('https://xivapi.com/' + data.Character.GrandCompany.Rank.Icon);
ctx.drawImage(gcRankIcon, gcRankIconX, gcRankIconY, 40, 40); ctx.drawImage(gcRankIcon, gcRankIconX, gcRankIconY, 40, 40);
@ -435,15 +464,15 @@ class CardCreator {
ctx.fillStyle = white; ctx.fillStyle = white;
if (data.Character.ClassJobsElemental.Level != null) { if (data.Character.ClassJobsElemental.Level != null) {
ctx.fillText(`Level ${data.Character.ClassJobsElemental.Level}`, 480, 450); ctx.fillText(`${strings.eurekaLevel} ${data.Character.ClassJobsElemental.Level}`, 480, 450);
} else { } else {
ctx.fillText(`Level 0`, 480, 450); ctx.fillText(`${strings.eurekaLevel} 0`, 480, 450);
} }
if (data.Character.ClassJobsBozjan.Level != null) { if (data.Character.ClassJobsBozjan.Level != null) {
ctx.fillText(`Rank ${data.Character.ClassJobsBozjan.Level}`, 480, 500); ctx.fillText(`${strings.bozjaRank} ${data.Character.ClassJobsBozjan.Level}`, 480, 500);
} else { } else {
ctx.fillText(`Rank 0`, 480, 500); ctx.fillText(`${strings.bozjaRank} 0`, 480, 500);
} }
// Minion & Mount percentages // Minion & Mount percentages
@ -466,8 +495,8 @@ class CardCreator {
ctx.fillStyle = grey; ctx.fillStyle = grey;
ctx.font = small; ctx.font = small;
ctx.fillText("Mounts", 480 + mountsMeasure.width + 5, textMountMinionY); ctx.fillText(strings.mounts, 480 + mountsMeasure.width + 5, textMountMinionY);
ctx.fillText("Minions", 685 + minionsMeasure.width + 5, textMountMinionY); ctx.fillText(strings.minions, 685 + minionsMeasure.width + 5, textMountMinionY);
ctx.drawImage(this.imgMount, 620, iconMountMinionY, 32, 32); ctx.drawImage(this.imgMount, 620, iconMountMinionY, 32, 32);
ctx.drawImage(this.imgMinion, 834, iconMountMinionY, 19, 32); ctx.drawImage(this.imgMinion, 834, iconMountMinionY, 19, 32);