From 90f4e42b11587fca6b9b41770c50f79d5ba8c1f2 Mon Sep 17 00:00:00 2001 From: Renan Pinheiro Date: Sun, 19 Feb 2023 03:17:00 -0300 Subject: [PATCH] feat: add full rpc locatilization for 'PT' --- src/discord.ts | 39 ++++++++------ src/discord/constants.ts | 2 - src/discord/localization/en.ts | 12 +++++ src/discord/localization/localization.ts | 67 ++++++++++++++++++++++++ src/discord/localization/pt.ts | 12 +++++ src/discord/parsers/locationParser.ts | 4 +- src/discord/parsers/roomParser.ts | 4 +- src/discord/requestHandler.ts | 9 ++-- src/store.ts | 1 + src/store/PublicSchema.ts | 1 + 10 files changed, 124 insertions(+), 27 deletions(-) create mode 100644 src/discord/localization/en.ts create mode 100644 src/discord/localization/localization.ts create mode 100644 src/discord/localization/pt.ts diff --git a/src/discord.ts b/src/discord.ts index b5d91bd..280548e 100644 --- a/src/discord.ts +++ b/src/discord.ts @@ -1,7 +1,8 @@ import { Client, register } from "discord-rpc"; import { BrowserWindow, dialog } from "electron"; import { EOL } from "os"; -import { CPPS_MAP, DISCORD_RPC_CLIENT_APP_ID, LARGE_IMAGE_KEY, UNLOGGED, WADDLING } from "./discord/constants"; +import { CPPS_MAP, DISCORD_RPC_CLIENT_APP_ID, LARGE_IMAGE_KEY } from "./discord/constants"; +import { getLocalizedPlaying, getLocalizedTalkingWith, getLocalizedUnlogged, getLocalizedVisiting, getLocalizedWaddling, getLocalizedWaddlingAt } from "./discord/localization/localization"; import { startRequestListener } from "./discord/requestHandler"; import { Store } from "./store"; import { CPLocation, CPLocationType, DiscordState } from "./store/DiscordState"; @@ -39,50 +40,54 @@ export const setLocationsInStore = (store: Store, locations: CPLocation[]) => { setDiscordStateInStore(store, state); }; -export const setUnloggedStatus = (state: DiscordState) => { +export const setUnloggedStatus = (store: Store) => { + const state = getDiscordStateFromStore(store); + return state.client.setActivity({ details: state.gameName, - state: UNLOGGED, + state: getLocalizedUnlogged(store), startTimestamp: state.startTimestamp, largeImageKey: LARGE_IMAGE_KEY, }); }; -const setWaddlingStatus = (state: DiscordState) => { +const setWaddlingStatus = (store: Store) => { + const state = getDiscordStateFromStore(store); + return state.client.setActivity({ details: state.gameName, - state: WADDLING, + state: getLocalizedWaddling(store), startTimestamp: state.startTimestamp, largeImageKey: LARGE_IMAGE_KEY, }); }; -export const setUnknownLocationStatus = (state: DiscordState, match: string) => { +export const setUnknownLocationStatus = (store: Store, state: DiscordState, match: string) => { const result = match.replace(/([A-Z])/g, " $1"); const finalResult = result.charAt(0).toUpperCase() + result.slice(1); return state.client.setActivity({ details: state.gameName, - state: `Waddling at ${finalResult}`, + state: `${getLocalizedWaddlingAt(store)} ${finalResult}`, startTimestamp: state.startTimestamp, largeImageKey: LARGE_IMAGE_KEY, }); }; -export const setLocationStatus = (state: DiscordState, location: CPLocation) => { +export const setLocationStatus = (store: Store, state: DiscordState, location: CPLocation) => { let msgPrefix: string; if (location.name.toLowerCase().includes('sensei')) { - msgPrefix = 'Talking with '; + msgPrefix = getLocalizedTalkingWith(store); } else if (location.type === CPLocationType.Game) { - msgPrefix = 'Playing '; + msgPrefix = getLocalizedPlaying(store); } else if (location.name.toLowerCase().includes('igloo')) { - msgPrefix = 'Visiting an '; + msgPrefix = getLocalizedVisiting(store); } else { - msgPrefix = 'Waddling at '; + msgPrefix = getLocalizedWaddlingAt(store); } - const locationMsg = msgPrefix + location.name; + const locationMsg = msgPrefix + ' ' + location.name; return state.client.setActivity({ details: state.gameName, @@ -123,7 +128,7 @@ const registerWindowReload = (store: Store, mainWindow: BrowserWindow) => { // In case URL changed state.gameName = getGameName(store); - setUnloggedStatus(state); + setUnloggedStatus(store); setDiscordStateInStore(store, state); }); @@ -155,7 +160,7 @@ export const startDiscordRPC = (store: Store, mainWindow: BrowserWindow) => { client.on('ready', () => { client.setActivity({ details: gameName, - state: rpcTrackingEnabled ? UNLOGGED : WADDLING, + state: rpcTrackingEnabled ? getLocalizedUnlogged(store) : getLocalizedWaddling(store), startTimestamp: startTimestamp, largeImageKey: LARGE_IMAGE_KEY, }); @@ -247,12 +252,12 @@ export const enableOrDisableDiscordRPCLocationTracking = async (store: Store, ma updateDiscordRPCTrackingEnabledInStore(store); if (!getDiscordRPCTrackingEnabledFromStore(store)) { - setWaddlingStatus(getDiscordStateFromStore(store)); + setWaddlingStatus(store); return; } - setUnloggedStatus(getDiscordStateFromStore(store)); + setUnloggedStatus(store); mainWindow.reload(); }; \ No newline at end of file diff --git a/src/discord/constants.ts b/src/discord/constants.ts index 05427bd..5cf0f44 100644 --- a/src/discord/constants.ts +++ b/src/discord/constants.ts @@ -7,8 +7,6 @@ export const SWF_QUESTS_GAMES_PATH = '/games/quests/'; export const SWF_IGLOO_ROOM_PATH = 'igloo.swf'; export const DISCORD_RPC_CLIENT_APP_ID = '1076298709073145907'; export const LARGE_IMAGE_KEY = 'main-logo'; -export const UNLOGGED = 'Unlogged'; -export const WADDLING = 'Waddling'; export const CPPS_MAP = new Map([ ['newcp.net', 'New Club Penguin'], ['cpbrasil.pw', 'Club Penguin Brasil'], diff --git a/src/discord/localization/en.ts b/src/discord/localization/en.ts new file mode 100644 index 0000000..d95ba8f --- /dev/null +++ b/src/discord/localization/en.ts @@ -0,0 +1,12 @@ +export const EN_LOCALIZATION = new Map([ + ['UNLOGGED', 'Unlogged'], + ['WADDLING', 'Waddling'], + ['WADDLING_AT', 'Waddling at'], + ['TALKING_WITH', 'Talking with'], + ['PLAYING', 'Playing'], + ['VISITING_IGLOO', 'Visiting an'], +]); + +export const getEnLocalizedString = (stringKey: string) => { + return EN_LOCALIZATION.get(stringKey); +}; \ No newline at end of file diff --git a/src/discord/localization/localization.ts b/src/discord/localization/localization.ts new file mode 100644 index 0000000..c8e4f60 --- /dev/null +++ b/src/discord/localization/localization.ts @@ -0,0 +1,67 @@ +import { Store } from "../../store"; +import { getEnLocalizedString } from "./en"; +import { getPtLocalizedString } from "./pt"; + +export const STRING_KEYS = { + UNLOGGED_KEY: 'UNLOGGED', + WADDLING_KEY: 'WADDLING', + WADDLING_AT_KEY: 'WADDLING_AT', + TALKING_WITH_KEY: 'TALKING_WITH', + PLAYING_KEY: 'PLAYING', + VISITING_IGLOO_KEY: 'VISITING_IGLOO', +}; + +export const SUPPORTED_LANGS = [ + 'en', + 'pt' +]; + +export const setLanguageInStore = (store: Store, language: string) => { + if (!SUPPORTED_LANGS.includes(language)) { + return; + } + + store.public.set('lastLanguage', language); +}; + +export const getLanguageInStore = (store: Store) => { + return store.public.get('lastLanguage'); +}; + +export const getLocalizedUnlogged = (store: Store) => { + return getLocalizedString(store, STRING_KEYS.UNLOGGED_KEY); +}; + +export const getLocalizedWaddling = (store: Store) => { + return getLocalizedString(store, STRING_KEYS.WADDLING_KEY); +}; + +export const getLocalizedWaddlingAt = (store: Store) => { + return getLocalizedString(store, STRING_KEYS.WADDLING_AT_KEY); +}; + +export const getLocalizedTalkingWith = (store: Store) => { + return getLocalizedString(store, STRING_KEYS.TALKING_WITH_KEY); +}; + +export const getLocalizedPlaying = (store: Store) => { + return getLocalizedString(store, STRING_KEYS.PLAYING_KEY); +}; + +export const getLocalizedVisiting = (store: Store) => { + return getLocalizedString(store, STRING_KEYS.VISITING_IGLOO_KEY); +}; + +export const getLocalizedString = (store: Store, stringKey: string) => { + const lang = getLanguageInStore(store); + + switch (lang) { + case 'pt': + return getPtLocalizedString(stringKey); + + case 'en': + default: + return getEnLocalizedString(stringKey); + } +}; + diff --git a/src/discord/localization/pt.ts b/src/discord/localization/pt.ts new file mode 100644 index 0000000..cb6bfdd --- /dev/null +++ b/src/discord/localization/pt.ts @@ -0,0 +1,12 @@ +export const PT_LOCALIZATION = new Map([ + ['UNLOGGED', 'Deslogado'], + ['WADDLING', 'Pinguinando'], + ['WADDLING_AT', 'Pinguinando em'], + ['TALKING_WITH', 'Falando com o'], + ['PLAYING', 'Jogando'], + ['VISITING_IGLOO', 'Visitando um'], +]); + +export const getPtLocalizedString = (stringKey: string) => { + return PT_LOCALIZATION.get(stringKey); +}; \ No newline at end of file diff --git a/src/discord/parsers/locationParser.ts b/src/discord/parsers/locationParser.ts index f1b8519..1273937 100644 --- a/src/discord/parsers/locationParser.ts +++ b/src/discord/parsers/locationParser.ts @@ -78,9 +78,9 @@ export const parseAndUpdateLocation = async (store: Store, params: any) => { state.currentLocation = location; if (!location) { - await setUnknownLocationStatus(state, match); + await setUnknownLocationStatus(store, state, match); } else { - await setLocationStatus(state, location); + await setLocationStatus(store, state, location); } setDiscordStateInStore(store, state); diff --git a/src/discord/parsers/roomParser.ts b/src/discord/parsers/roomParser.ts index b974f6d..511030e 100644 --- a/src/discord/parsers/roomParser.ts +++ b/src/discord/parsers/roomParser.ts @@ -6,7 +6,7 @@ import { getRoomsJsonFromParams } from "../requestHandler"; // eslint-disable-next-line @typescript-eslint/no-explicit-any export const parseAndUpdateRooms = async (store: Store, mainWindow: BrowserWindow, params: any) => { - const result = await getRoomsJsonFromParams(mainWindow, params); + const result = await getRoomsJsonFromParams(store, mainWindow, params); // eslint-disable-next-line @typescript-eslint/no-explicit-any const rooms = Object.values(result.roomsJson) as any[]; @@ -135,7 +135,7 @@ export const parseAndUpdateRooms = async (store: Store, mainWindow: BrowserWindo if (localizedRooms) { const localizedName = localizedRooms.filter(localizedRoom => { return room.display_name === localizedRoom.display_name; - })[0].name; + })[0]?.name; if (localizedName) { name = localizedName; diff --git a/src/discord/requestHandler.ts b/src/discord/requestHandler.ts index 2dc8e29..089ad63 100644 --- a/src/discord/requestHandler.ts +++ b/src/discord/requestHandler.ts @@ -4,6 +4,7 @@ import { ROOMS_JSONP_NAME, ROOMS_PATH, SWF_MIME_FILE } from "./constants"; import { parseAndUpdateLocation } from "./parsers/locationParser"; import { parseAndUpdateRooms } from "./parsers/roomParser"; import fetch from 'electron-fetch'; +import { setLanguageInStore } from "./localization/localization"; const parseJSONP = (jsonp: string, name: string) => { const nameLength = name.length; @@ -19,7 +20,7 @@ type RoomsResponse = { } // eslint-disable-next-line @typescript-eslint/no-explicit-any -export const getRoomsJsonFromParams = async (mainWindow: BrowserWindow, params: any): Promise => { +export const getRoomsJsonFromParams = async (store: Store, mainWindow: BrowserWindow, params: any): Promise => { let plainResponseBody; let localizedResponseBody; @@ -47,6 +48,8 @@ export const getRoomsJsonFromParams = async (mainWindow: BrowserWindow, params: const lang = urlStart.substring(langIndex + 1); + setLanguageInStore(store, lang); + const enUrl = url.replace(lang, 'en'); const enResponse = await fetch(enUrl); @@ -54,13 +57,11 @@ export const getRoomsJsonFromParams = async (mainWindow: BrowserWindow, params: const enResponseBuffer = await enResponse.buffer(); plainResponseBody = enResponseBuffer.toString(); - - } return { roomsJson: parseJSONP(plainResponseBody, ROOMS_JSONP_NAME), - localizedJson: parseJSONP(localizedResponseBody, ROOMS_JSONP_NAME), + localizedJson: localizedResponseBody ? parseJSONP(localizedResponseBody, ROOMS_JSONP_NAME) : undefined, }; }; diff --git a/src/store.ts b/src/store.ts index d297312..901cf03 100644 --- a/src/store.ts +++ b/src/store.ts @@ -15,6 +15,7 @@ export const defaultPublicValues: PublicSchema = { disableAds: false, enableDiscord: false, enableDiscordTracker: false, + lastLanguage: 'pt', }; const createPublicStore = () => { diff --git a/src/store/PublicSchema.ts b/src/store/PublicSchema.ts index ef66b45..c98f6aa 100644 --- a/src/store/PublicSchema.ts +++ b/src/store/PublicSchema.ts @@ -3,4 +3,5 @@ export type PublicSchema = { disableAds: boolean; enableDiscord: boolean; enableDiscordTracker: boolean; + lastLanguage: string; } \ No newline at end of file