Skip to content

Commit 1ab621d

Browse files
filipbechiOvergaard
authored andcommitted
Refactor umb-localize into individual element
1 parent ad7cd14 commit 1ab621d

File tree

6 files changed

+167
-29
lines changed

6 files changed

+167
-29
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
import './localize.element.js';
2+
import './localize-date.element.js';
3+
import './localize-number.element.js';
4+
import './localize-relative-time.element.js';
25

36
export * from './registry/localization.registry.js';
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { css, customElement, html, property, state, unsafeHTML } from '@umbraco-cms/backoffice/external/lit';
2+
import { UmbLitElement } from '@umbraco-cms/internal/lit-element';
3+
4+
/**
5+
* This element allows you to localize a date
6+
* @element umb-localize-date
7+
* @slot - The fallback value if the key is not found.
8+
*/
9+
@customElement('umb-localize-date')
10+
export class UmbLocalizeDateElement extends UmbLitElement {
11+
/**
12+
* The date to localize.
13+
* @attr
14+
* @example date="Sep 22 2023"
15+
*/
16+
@property()
17+
date!: string | Date;
18+
19+
/**
20+
* Formatting options
21+
* @attr
22+
* @example options={ dateStyle: 'full', timeStyle: 'long', timeZone: 'Australia/Sydney' }
23+
*/
24+
@property()
25+
options?: Intl.DateTimeFormatOptions;
26+
27+
@state()
28+
protected get text(): string {
29+
return this.localize.date(this.date, this.options);
30+
}
31+
32+
protected render() {
33+
return this.date
34+
? html`${unsafeHTML(this.text)}`
35+
: html`<slot></slot>`
36+
}
37+
38+
static styles = [
39+
css`
40+
:host {
41+
display: contents;
42+
}
43+
`,
44+
];
45+
}
46+
47+
declare global {
48+
interface HTMLElementTagNameMap {
49+
'umb-localize-date': UmbLocalizeDateElement;
50+
}
51+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { css, customElement, html, property, state, unsafeHTML } from '@umbraco-cms/backoffice/external/lit';
2+
import { UmbLitElement } from '@umbraco-cms/internal/lit-element';
3+
4+
/**
5+
* This element allows you to localize a number
6+
* @element umb-localize-number
7+
* @slot - The fallback value if the key is not found.
8+
*/
9+
@customElement('umb-localize-number')
10+
export class UmbLocalizeNumberElement extends UmbLitElement {
11+
/**
12+
* The number to localize.
13+
* @attr
14+
* @example number=1_000_000
15+
*/
16+
@property()
17+
number!: number | string;
18+
19+
/**
20+
* Formatting options
21+
* @attr
22+
* @example options={ style: 'currency', currency: 'EUR' }
23+
*/
24+
@property()
25+
options?: Intl.NumberFormatOptions;
26+
27+
@state()
28+
protected get text(): string {
29+
return this.localize.number(this.number, this.options);
30+
}
31+
32+
protected render() {
33+
return this.number
34+
? html`${unsafeHTML(this.text)}`
35+
: html`<slot></slot>`
36+
}
37+
38+
static styles = [
39+
css`
40+
:host {
41+
display: contents;
42+
}
43+
`,
44+
];
45+
}
46+
47+
declare global {
48+
interface HTMLElementTagNameMap {
49+
'umb-localize-number': UmbLocalizeNumberElement;
50+
}
51+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { css, customElement, html, property, state, unsafeHTML } from '@umbraco-cms/backoffice/external/lit';
2+
import { UmbLitElement } from '@umbraco-cms/internal/lit-element';
3+
4+
/**
5+
* This element allows you to localize a relative time
6+
* @element umb-localize-relative-time
7+
* @slot - The fallback value if the key is not found.
8+
*/
9+
@customElement('umb-localize-relative-time')
10+
export class UmbLocalizeRelativeTimeElement extends UmbLitElement {
11+
/**
12+
* The date to localize.
13+
* @attr
14+
* @example time=10
15+
*/
16+
@property()
17+
time!: number;
18+
19+
/**
20+
* Formatting options
21+
* @attr
22+
* @example options={ dateStyle: 'full', timeStyle: 'long', timeZone: 'Australia/Sydney' }
23+
*/
24+
@property()
25+
options?: Intl.RelativeTimeFormatOptions;
26+
27+
/**
28+
* Unit
29+
* @attr
30+
* @example unit='seconds'
31+
*/
32+
@property()
33+
unit: Intl.RelativeTimeFormatUnit = 'seconds';
34+
35+
@state()
36+
protected get text(): string {
37+
return this.localize.relativeTime(this.time, this.unit, this.options);
38+
}
39+
40+
protected render() {
41+
return this.time
42+
? html`${unsafeHTML(this.text)}`
43+
: html`<slot></slot>`
44+
}
45+
46+
static styles = [
47+
css`
48+
:host {
49+
display: contents;
50+
}
51+
`,
52+
];
53+
}
54+
55+
declare global {
56+
interface HTMLElementTagNameMap {
57+
'umb-localize-relative-time': UmbLocalizeRelativeTimeElement;
58+
}
59+
}

src/packages/core/localization/localize.element.ts

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { UmbLitElement } from '@umbraco-cms/internal/lit-element';
99
@customElement('umb-localize')
1010
export class UmbLocalizeElement extends UmbLitElement {
1111
/**
12-
* The key to localize. The key is case sensitive. For non-term localizations, this is the input
12+
* The key to localize. The key is case sensitive.
1313
* @attr
1414
* @example key="general_ok"
1515
*/
@@ -33,37 +33,11 @@ export class UmbLocalizeElement extends UmbLitElement {
3333
*/
3434
@property()
3535
debug = false;
36-
37-
@property()
38-
type: 'term' | 'date' | 'number' | 'relativeTime' = 'term';
39-
40-
@property()
41-
unit: Intl.RelativeTimeFormatUnit = 'seconds';
42-
43-
@property()
44-
options?: Intl.DateTimeFormatOptions | Intl.NumberFormatOptions | Intl.RelativeTimeFormatOptions;
4536

4637
@state()
4738
protected get text(): string {
48-
let localizedValue = '';
39+
const localizedValue = this.localize.term(this.key, ...(this.args ?? []));
4940

50-
switch (this.type) {
51-
case 'term':
52-
localizedValue = this.localize.term(this.key, ...(this.args ?? []));
53-
break;
54-
case 'date':
55-
localizedValue = this.localize.date(this.key, this.options as Intl.DateTimeFormatOptions);
56-
break;
57-
case 'number':
58-
localizedValue = this.localize.number(this.key, this.options as Intl.NumberFormatOptions);
59-
break;
60-
case 'relativeTime':
61-
localizedValue = this.localize.relativeTime(+this.key, this.unit, this.options as Intl.RelativeTimeFormatOptions);
62-
break;
63-
default:
64-
throw('unsupported type')
65-
break;
66-
}
6741
// If the value is the same as the key, it means the key was not found.
6842
if (localizedValue === this.key) {
6943
(this.getHostElement() as HTMLElement).setAttribute('data-localize-missing', this.key);

src/packages/documents/documents/workspace/views/info/document-info-workspace-view.element.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ export class UmbDocumentInfoWorkspaceViewElement extends UmbLitElement {
177177
</div>
178178
<div class="general-item">
179179
<strong><umb-localize key="content_createDate"></umb-localize></strong>
180-
<span><umb-localize key="${new Date}" type="date"></umb-localize></span>
180+
<span><umb-localize-date date="${new Date}"></umb-localize-date></span>
181181
</div>
182182
<div class="general-item">
183183
<strong><umb-localize key="content_documentType"></umb-localize></strong>

0 commit comments

Comments
 (0)