From ac51cad07562789e097612ba6392944e7be270bb Mon Sep 17 00:00:00 2001 From: Michel Heusschen <59014050+michelheusschen@users.noreply.github.com> Date: Mon, 1 Jul 2024 18:54:13 +0200 Subject: [PATCH] feat(web): html tags inside plural and select messages (#10696) * feat(web): html tags inside plural and select messages * add component docs --- .../i18n/__test__/format-message.spec.ts | 27 +++++ .../lib/components/i18n/format-message.svelte | 102 +++++++++++++++--- .../photos-page/delete-asset-dialog.svelte | 10 +- web/src/lib/i18n/en.json | 1 + 4 files changed, 121 insertions(+), 19 deletions(-) diff --git a/web/src/lib/components/i18n/__test__/format-message.spec.ts b/web/src/lib/components/i18n/__test__/format-message.spec.ts index be3e36bfa8..589d9024e7 100644 --- a/web/src/lib/components/i18n/__test__/format-message.spec.ts +++ b/web/src/lib/components/i18n/__test__/format-message.spec.ts @@ -13,6 +13,9 @@ describe('FormatMessage component', () => { html: 'Hello {name}', plural: 'You have {count, plural, one {# item} other {# items}}', xss: '', + plural_with_html: 'You have {count, plural, other {# items}}', + select_with_html: 'Item is {status, select, other {disabled}}', + ordinal_with_html: '{count, selectordinal, other {#th}} item', }), ); @@ -76,4 +79,28 @@ describe('FormatMessage component', () => { render(FormatMessage, { key: 'invalid.key' }); expect(screen.getByText('invalid.key')).toBeInTheDocument(); }); + + it('supports html tags inside plurals', () => { + const { container } = render(FormatTagB, { + key: 'plural_with_html', + values: { count: 10 }, + }); + expect(container.innerHTML).toBe('You have 10 items'); + }); + + it('supports html tags inside select', () => { + const { container } = render(FormatTagB, { + key: 'select_with_html', + values: { status: true }, + }); + expect(container.innerHTML).toBe('Item is disabled'); + }); + + it('supports html tags inside selectordinal', () => { + const { container } = render(FormatTagB, { + key: 'ordinal_with_html', + values: { count: 4 }, + }); + expect(container.innerHTML).toBe('4th item'); + }); }); diff --git a/web/src/lib/components/i18n/format-message.svelte b/web/src/lib/components/i18n/format-message.svelte index 62ccead471..9b91d1b7f4 100644 --- a/web/src/lib/components/i18n/format-message.svelte +++ b/web/src/lib/components/i18n/format-message.svelte @@ -1,10 +1,20 @@ + {#each parts as { tag, message }} {#if tag} {message} diff --git a/web/src/lib/components/photos-page/delete-asset-dialog.svelte b/web/src/lib/components/photos-page/delete-asset-dialog.svelte index 56ab150b4e..84782b2d7f 100644 --- a/web/src/lib/components/photos-page/delete-asset-dialog.svelte +++ b/web/src/lib/components/photos-page/delete-asset-dialog.svelte @@ -4,6 +4,7 @@ import { showDeleteModal } from '$lib/stores/preferences.store'; import Checkbox from '$lib/components/elements/checkbox.svelte'; import { t } from 'svelte-i18n'; + import FormatMessage from '$lib/components/i18n/format-message.svelte'; export let size: number; @@ -30,12 +31,9 @@ >

- Are you sure you want to permanently delete - {#if size > 1} - these {size} assets? This will also remove them from their album(s). - {:else} - this asset? This will also remove it from its album(s). - {/if} + + {message} +

{$t('cannot_undo_this_action')}

diff --git a/web/src/lib/i18n/en.json b/web/src/lib/i18n/en.json index 6aa325d0fd..01b7a5e475 100644 --- a/web/src/lib/i18n/en.json +++ b/web/src/lib/i18n/en.json @@ -868,6 +868,7 @@ "permanent_deletion_warning_setting_description": "Show a warning when permanently deleting assets", "permanently_delete": "Permanently delete", "permanently_delete_assets_count": "Permanently delete {count, plural, one {asset} other {assets}}", + "permanently_delete_assets_prompt": "Are you sure you want to permanently delete {count, plural, one {this asset?} other {these # assets?}} This will also remove {count, plural, one {it from its} other {them from their}} album(s).", "permanently_deleted_asset": "Permanently deleted asset", "permanently_deleted_assets_count": "Permanently deleted {count, plural, one {# asset} other {# assets}}", "person": "Person",