From f2f79d684401e9fd6078a7e86a79fc36ebd922fa Mon Sep 17 00:00:00 2001 From: Alba Rivas Date: Tue, 22 Oct 2024 08:53:12 +0200 Subject: [PATCH] Add i18n recipe (Esteve Graells code) (#955) * additional own recipes: i18n and lwclogger Own recipes created to showcase i18n and how to send events from LWC to Event Monitoring * New LWC for sending events to Event Monitoring * Run prettier and fix lint errors * Rename component * Add recipe to misc tab, refactor * Remove logger * Remove wrong recipe * Remove CSS file * Add test * Add requested changes --------- Co-authored-by: Esteve Graells --- .../Misc_Techniques.flexipage-meta.xml | 6 ++ .../lwc/miscI18n/__tests__/miscI18n.test.js | 75 +++++++++++++++++++ .../main/default/lwc/miscI18n/miscI18n.html | 38 ++++++++++ .../main/default/lwc/miscI18n/miscI18n.js | 36 +++++++++ .../default/lwc/miscI18n/miscI18n.js-meta.xml | 10 +++ 5 files changed, 165 insertions(+) create mode 100644 force-app/main/default/lwc/miscI18n/__tests__/miscI18n.test.js create mode 100644 force-app/main/default/lwc/miscI18n/miscI18n.html create mode 100644 force-app/main/default/lwc/miscI18n/miscI18n.js create mode 100644 force-app/main/default/lwc/miscI18n/miscI18n.js-meta.xml diff --git a/force-app/main/default/flexipages/Misc_Techniques.flexipage-meta.xml b/force-app/main/default/flexipages/Misc_Techniques.flexipage-meta.xml index 57662e09f..044c3cf3f 100644 --- a/force-app/main/default/flexipages/Misc_Techniques.flexipage-meta.xml +++ b/force-app/main/default/flexipages/Misc_Techniques.flexipage-meta.xml @@ -29,6 +29,12 @@ miscModal + + + miscI18n + c_miscI18n + + region2 Region diff --git a/force-app/main/default/lwc/miscI18n/__tests__/miscI18n.test.js b/force-app/main/default/lwc/miscI18n/__tests__/miscI18n.test.js new file mode 100644 index 000000000..e343bd928 --- /dev/null +++ b/force-app/main/default/lwc/miscI18n/__tests__/miscI18n.test.js @@ -0,0 +1,75 @@ +import { createElement } from 'lwc'; +import MiscI18n from 'c/miscI18n'; +import USER_LOCALE from '@salesforce/i18n/locale'; +import USER_CURRENCY from '@salesforce/i18n/currency'; + +describe('c-misc-i18n', () => { + afterEach(() => { + // The jsdom instance is shared across test cases in a single file so reset the DOM + while (document.body.firstChild) { + document.body.removeChild(document.body.firstChild); + } + }); + + it('component is initialized with correct variables', () => { + // Create initial element + const element = createElement('c-misc-i18n', { + is: MiscI18n + }); + document.body.appendChild(element); + + const userLocale = element.shadowRoot.querySelector('span.userLocale'); + expect(userLocale).not.toBeNull(); + expect(userLocale.textContent).toBe(USER_LOCALE); + + const dateUserLocale = element.shadowRoot.querySelector( + 'span.dateUserLocale' + ); + expect(dateUserLocale).not.toBeNull(); + expect(dateUserLocale.textContent).toBe( + new Intl.DateTimeFormat(USER_LOCALE).format(new Date()) + ); + + const currencyUserLocale = element.shadowRoot.querySelector( + 'span.currencyUserLocale' + ); + expect(currencyUserLocale).not.toBeNull(); + expect(currencyUserLocale.textContent).toBe( + new Intl.NumberFormat(USER_LOCALE, { + style: 'currency', + currency: USER_CURRENCY, + currencyDisplay: 'symbol' + }).format(100) + ); + + const dateJapanLocale = element.shadowRoot.querySelector( + 'span.dateJapanLocale' + ); + expect(dateJapanLocale).not.toBeNull(); + expect(dateJapanLocale.textContent).toBe( + new Intl.DateTimeFormat('ja-JP').format(new Date()) + ); + + const currencyJapanLocale = element.shadowRoot.querySelector( + 'span.currencyJapanLocale' + ); + expect(currencyJapanLocale).not.toBeNull(); + expect(currencyJapanLocale.textContent).toBe( + new Intl.NumberFormat('ja-JP', { + style: 'currency', + currency: 'JPY', + currencyDisplay: 'symbol' + }).format(100) + ); + }); + + it('is accessible', async () => { + const element = createElement('c-misc-i18n', { + is: MiscI18n + }); + + document.body.appendChild(element); + + await expect(element).toBeAccessible(); + }); +}); diff --git a/force-app/main/default/lwc/miscI18n/miscI18n.html b/force-app/main/default/lwc/miscI18n/miscI18n.html new file mode 100644 index 000000000..ea314f298 --- /dev/null +++ b/force-app/main/default/lwc/miscI18n/miscI18n.html @@ -0,0 +1,38 @@ + diff --git a/force-app/main/default/lwc/miscI18n/miscI18n.js b/force-app/main/default/lwc/miscI18n/miscI18n.js new file mode 100644 index 000000000..f66e702b9 --- /dev/null +++ b/force-app/main/default/lwc/miscI18n/miscI18n.js @@ -0,0 +1,36 @@ +import { LightningElement } from 'lwc'; +import USER_LOCALE from '@salesforce/i18n/locale'; +import USER_CURRENCY from '@salesforce/i18n/currency'; + +export default class I18n extends LightningElement { + userLocale = USER_LOCALE; + userCurrency = USER_CURRENCY; + japanLocale = 'ja-JP'; + japanCurrency = 'JPY'; + + get dateUserLocale() { + const date = new Date(); + return new Intl.DateTimeFormat(USER_LOCALE).format(date); + } + + get dateJapanLocale() { + const date = new Date(); + return new Intl.DateTimeFormat(this.japanLocale).format(date); + } + + get currencyUserLocale() { + return new Intl.NumberFormat(USER_LOCALE, { + style: 'currency', + currency: USER_CURRENCY, + currencyDisplay: 'symbol' + }).format(100); + } + + get currencyJapanLocale() { + return new Intl.NumberFormat(this.japanLocale, { + style: 'currency', + currency: this.japanCurrency, + currencyDisplay: 'symbol' + }).format(100); + } +} diff --git a/force-app/main/default/lwc/miscI18n/miscI18n.js-meta.xml b/force-app/main/default/lwc/miscI18n/miscI18n.js-meta.xml new file mode 100644 index 000000000..f7942ea17 --- /dev/null +++ b/force-app/main/default/lwc/miscI18n/miscI18n.js-meta.xml @@ -0,0 +1,10 @@ + + + 61.0 + true + + lightning__RecordPage + lightning__AppPage + lightning__HomePage + +