Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i18n new LWC (AR+EG) #958

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
<identifier>miscModal</identifier>
</componentInstance>
</itemInstances>
<itemInstances>
<componentInstance>
<componentName>miscI18n</componentName>
<identifier>c_miscI18n</identifier>
</componentInstance>
</itemInstances>
<name>region2</name>
<type>Region</type>
</flexiPageRegions>
Expand Down
75 changes: 75 additions & 0 deletions force-app/main/default/lwc/miscI18n/__tests__/miscI18n.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
38 changes: 38 additions & 0 deletions force-app/main/default/lwc/miscI18n/miscI18n.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<template>
<lightning-card title="MiscI18n" icon-name="custom:custom19">
<div class="slds-var-m-around_medium">
<p>
User's locale:
satyasekharcvb marked this conversation as resolved.
Show resolved Hide resolved
<span class="userLocale"><b>{userLocale}</b></span>
</p>
<p>
Today's date formatted with {userLocale}:
satyasekharcvb marked this conversation as resolved.
Show resolved Hide resolved
<span class="dateUserLocale">
<b>{dateUserLocale}</b>
</span>
</p>
<p>
100 {userCurrency} formatted with {userLocale}:
satyasekharcvb marked this conversation as resolved.
Show resolved Hide resolved
<span class="currencyUserLocale">
<b>{currencyUserLocale}</b>
</span>
</p>
<p>
Today's date formatted with {japanLocale}:
satyasekharcvb marked this conversation as resolved.
Show resolved Hide resolved
<span class="dateJapanLocale">
<b>{dateJapanLocale}</b>
</span>
</p>
<p>
100 {japanCurrency} formatted with {japanLocale}:
satyasekharcvb marked this conversation as resolved.
Show resolved Hide resolved
<span class="currencyJapanLocale">
<b>{currencyJapanLocale}</b>
</span>
</p>
</div>
<c-view-source source="lwc/miscI18n" slot="footer">
Use the standard JS Intl class to format dates and numbers with
different locales and currencies.
</c-view-source>
</lightning-card>
</template>
36 changes: 36 additions & 0 deletions force-app/main/default/lwc/miscI18n/miscI18n.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
10 changes: 10 additions & 0 deletions force-app/main/default/lwc/miscI18n/miscI18n.js-meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>61.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__RecordPage</target>
<target>lightning__AppPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>