Skip to content

Commit

Permalink
Add localization strings
Browse files Browse the repository at this point in the history
  • Loading branch information
adamfraser committed Aug 21, 2024
1 parent 6243d74 commit ccb1f77
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 16 deletions.
24 changes: 24 additions & 0 deletions indexer/packages/notifications/__tests__/localization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
NotificationType,
NotificationDynamicFieldKey,
createNotification,
isValidLanguageCode,
} from '../src/types';

describe('deriveLocalizedNotificationMessage', () => {
Expand Down Expand Up @@ -38,4 +39,27 @@ describe('deriveLocalizedNotificationMessage', () => {
const result = deriveLocalizedNotificationMessage(notification);
expect(result).toEqual(expected);
});

describe('isValidLanguageCode', () => {
test('should return true for valid language codes', () => {
const validCodes = ['en', 'es', 'fr', 'de', 'it', 'ja', 'ko', 'zh'];
validCodes.forEach((code) => {
expect(isValidLanguageCode(code)).toBe(true);
});
});

test('should return false for invalid language codes', () => {
const invalidCodes = ['', 'EN', 'eng', 'esp', 'fra', 'deu', 'ita', 'jpn', 'kor', 'zho', 'xx'];
invalidCodes.forEach((code) => {
expect(isValidLanguageCode(code)).toBe(false);
});
});

test('should return false for non-string inputs', () => {
const nonStringInputs = [null, undefined, 123, {}, []];
nonStringInputs.forEach((input) => {
expect(isValidLanguageCode(input as any)).toBe(false);
});
});
});
});
26 changes: 11 additions & 15 deletions indexer/packages/notifications/src/localization.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
import { LOCALIZED_MESSAGES } from './localizedMessages';
import {
LocalizationKey,
Notification,
NotificationMesage,
LanguageCode,
} from './types';

function replacePlaceholders(template: string, variables: Record<string, string>): string {
return template.replace(/{(\w+)}/g, (_, key) => variables[key] || `{${key}}`);
}

export function deriveLocalizedNotificationMessage(notification: Notification): NotificationMesage {
const tempLocalizationFields = {
[LocalizationKey.DEPOSIT_SUCCESS_TITLE]: 'Deposit Successful',
[LocalizationKey.DEPOSIT_SUCCESS_BODY]: 'You have successfully deposited {AMOUNT} {MARKET} to your dYdX account.',
[LocalizationKey.ORDER_FILLED_TITLE]: 'Order Filled',
// eslint-disable-next-line no-template-curly-in-string
[LocalizationKey.ORDER_FILLED_BODY]: 'Your order for {AMOUNT} {MARKET} was filled at ${AVERAGE_PRICE}',
// eslint-disable-next-line no-template-curly-in-string
[LocalizationKey.ORDER_TRIGGERED_BODY]: 'Your order for {AMOUNT} {MARKET} was triggered at ${PRICE}',
[LocalizationKey.ORDER_TRIGGERED_TITLE]: '{MARKET} Order Triggered',
};
export function deriveLocalizedNotificationMessage(
notification: Notification,
languageCode: LanguageCode = 'en',
): NotificationMesage {
const localizationFields = LOCALIZED_MESSAGES[languageCode] || LOCALIZED_MESSAGES.en;

return {
title: replacePlaceholders(
tempLocalizationFields[notification.titleKey],
notification.dynamicValues),
localizationFields[notification.titleKey],
notification.dynamicValues,
),
body: replacePlaceholders(
tempLocalizationFields[notification.bodyKey],
localizationFields[notification.bodyKey],
notification.dynamicValues,
),
};
Expand Down
69 changes: 69 additions & 0 deletions indexer/packages/notifications/src/localizedMessages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* eslint-disable no-template-curly-in-string */
import { LocalizationKey, LanguageCode } from './types';

export const LOCALIZED_MESSAGES: Record<LanguageCode, Record<LocalizationKey, string>> = {
en: {
[LocalizationKey.DEPOSIT_SUCCESS_TITLE]: 'Deposit Successful',
[LocalizationKey.DEPOSIT_SUCCESS_BODY]: 'You have successfully deposited {AMOUNT} {MARKET} to your dYdX account.',
[LocalizationKey.ORDER_FILLED_TITLE]: 'Order Filled',
[LocalizationKey.ORDER_FILLED_BODY]: 'Your order for {AMOUNT} {MARKET} was filled at ${AVERAGE_PRICE}',
[LocalizationKey.ORDER_TRIGGERED_TITLE]: '{MARKET} Order Triggered',
[LocalizationKey.ORDER_TRIGGERED_BODY]: 'Your order for {AMOUNT} {MARKET} was triggered at ${PRICE}',
},
es: {
[LocalizationKey.DEPOSIT_SUCCESS_TITLE]: 'Depósito Exitoso',
[LocalizationKey.DEPOSIT_SUCCESS_BODY]: 'Has depositado exitosamente {AMOUNT} {MARKET} en tu cuenta dYdX.',
[LocalizationKey.ORDER_FILLED_TITLE]: 'Orden Ejecutada',
[LocalizationKey.ORDER_FILLED_BODY]: 'Tu orden de {AMOUNT} {MARKET} se ejecutó a ${AVERAGE_PRICE}',
[LocalizationKey.ORDER_TRIGGERED_TITLE]: 'Orden de {MARKET} Activada',
[LocalizationKey.ORDER_TRIGGERED_BODY]: 'Tu orden de {AMOUNT} {MARKET} se activó a ${PRICE}',
},
fr: {
[LocalizationKey.DEPOSIT_SUCCESS_TITLE]: 'Dépôt Réussi',
[LocalizationKey.DEPOSIT_SUCCESS_BODY]: 'Vous avez déposé avec succès {AMOUNT} {MARKET} sur votre compte dYdX.',
[LocalizationKey.ORDER_FILLED_TITLE]: 'Ordre Exécuté',
[LocalizationKey.ORDER_FILLED_BODY]: 'Votre ordre de {AMOUNT} {MARKET} a été exécuté à ${AVERAGE_PRICE}',
[LocalizationKey.ORDER_TRIGGERED_TITLE]: 'Ordre {MARKET} Déclenché',
[LocalizationKey.ORDER_TRIGGERED_BODY]: 'Votre ordre de {AMOUNT} {MARKET} a été déclenché à ${PRICE}',
},
de: {
[LocalizationKey.DEPOSIT_SUCCESS_TITLE]: 'Einzahlung Erfolgreich',
[LocalizationKey.DEPOSIT_SUCCESS_BODY]: 'Sie haben erfolgreich {AMOUNT} {MARKET} auf Ihr dYdX-Konto eingezahlt.',
[LocalizationKey.ORDER_FILLED_TITLE]: 'Auftrag Ausgeführt',
[LocalizationKey.ORDER_FILLED_BODY]: 'Ihr Auftrag für {AMOUNT} {MARKET} wurde zu ${AVERAGE_PRICE} ausgeführt',
[LocalizationKey.ORDER_TRIGGERED_TITLE]: '{MARKET} Auftrag Ausgelöst',
[LocalizationKey.ORDER_TRIGGERED_BODY]: 'Ihr Auftrag für {AMOUNT} {MARKET} wurde bei ${PRICE} ausgelöst',
},
it: {
[LocalizationKey.DEPOSIT_SUCCESS_TITLE]: 'Deposito Riuscito',
[LocalizationKey.DEPOSIT_SUCCESS_BODY]: 'Hai depositato con successo {AMOUNT} {MARKET} sul tuo account dYdX.',
[LocalizationKey.ORDER_FILLED_TITLE]: 'Ordine Eseguito',
[LocalizationKey.ORDER_FILLED_BODY]: 'Il tuo ordine di {AMOUNT} {MARKET} è stato eseguito a ${AVERAGE_PRICE}',
[LocalizationKey.ORDER_TRIGGERED_TITLE]: 'Ordine {MARKET} Attivato',
[LocalizationKey.ORDER_TRIGGERED_BODY]: 'Il tuo ordine di {AMOUNT} {MARKET} è stato attivato a ${PRICE}',
},
ja: {
[LocalizationKey.DEPOSIT_SUCCESS_TITLE]: '入金成功',
[LocalizationKey.DEPOSIT_SUCCESS_BODY]: 'dYdXアカウントに{AMOUNT} {MARKET}を正常に入金しました。',
[LocalizationKey.ORDER_FILLED_TITLE]: '注文約定',
[LocalizationKey.ORDER_FILLED_BODY]: '{AMOUNT} {MARKET}の注文が${AVERAGE_PRICE}で約定しました',
[LocalizationKey.ORDER_TRIGGERED_TITLE]: '{MARKET}注文トリガー',
[LocalizationKey.ORDER_TRIGGERED_BODY]: '{AMOUNT} {MARKET}の注文が${PRICE}でトリガーされました',
},
ko: {
[LocalizationKey.DEPOSIT_SUCCESS_TITLE]: '입금 성공',
[LocalizationKey.DEPOSIT_SUCCESS_BODY]: 'dYdX 계정에 {AMOUNT} {MARKET}을(를) 성공적으로 입금했습니다.',
[LocalizationKey.ORDER_FILLED_TITLE]: '주문 체결',
[LocalizationKey.ORDER_FILLED_BODY]: '{AMOUNT} {MARKET} 주문이 ${AVERAGE_PRICE}에 체결되었습니다',
[LocalizationKey.ORDER_TRIGGERED_TITLE]: '{MARKET} 주문 트리거',
[LocalizationKey.ORDER_TRIGGERED_BODY]: '{AMOUNT} {MARKET} 주문이 ${PRICE}에서 트리거되었습니다',
},
zh: {
[LocalizationKey.DEPOSIT_SUCCESS_TITLE]: '存款成功',
[LocalizationKey.DEPOSIT_SUCCESS_BODY]: '您已成功将 {AMOUNT} {MARKET} 存入您的 dYdX 账户。',
[LocalizationKey.ORDER_FILLED_TITLE]: '订单已成交',
[LocalizationKey.ORDER_FILLED_BODY]: '您的 {AMOUNT} {MARKET} 订单已以 ${AVERAGE_PRICE} 成交',
[LocalizationKey.ORDER_TRIGGERED_TITLE]: '{MARKET} 订单已触发',
[LocalizationKey.ORDER_TRIGGERED_BODY]: '您的 {AMOUNT} {MARKET} 订单已在 ${PRICE} 触发',
},
};
7 changes: 6 additions & 1 deletion indexer/packages/notifications/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export enum Topic {
PRICE_ALERTS = 'price_alerts',
}

export type LanguageCode = 'en' | 'es' | 'fr' | 'de' | 'it' | 'ja' | 'ko' | 'zh';
export function isValidLanguageCode(code: string): code is LanguageCode {
return ['en', 'es', 'fr', 'de', 'it', 'ja', 'ko', 'zh'].includes(code);
}

interface BaseNotification <T extends Record<string, string>> {
type: NotificationType,
titleKey: LocalizationKey;
Expand Down Expand Up @@ -144,4 +149,4 @@ export function createNotification<T extends NotificationType>(
default:
throw new Error('Unknown notification type');
}
}
}

0 comments on commit ccb1f77

Please sign in to comment.