-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Accessibility: translate internal component labels (#3717)
Co-authored-by: Jakob Engelbrecht <jakob@basher.dk>
- Loading branch information
1 parent
aef5a86
commit 532ed37
Showing
21 changed files
with
377 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
apps/cookbook/src/app/localization/locale-provider/da-locale-provider.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Component, LOCALE_ID } from '@angular/core'; | ||
import { TranslationService } from '@kirbydesign/designsystem/shared'; | ||
|
||
@Component({ | ||
selector: 'cookbook-da-locale-provider', | ||
template: '<ng-content></ng-content>', | ||
standalone: true, | ||
providers: [{ provide: LOCALE_ID, useValue: 'da' }, TranslationService], | ||
}) | ||
export class DaLocaleProviderComponent {} |
10 changes: 10 additions & 0 deletions
10
apps/cookbook/src/app/localization/locale-provider/en-locale-provider.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Component, LOCALE_ID } from '@angular/core'; | ||
import { TranslationService } from '@kirbydesign/designsystem/shared'; | ||
|
||
@Component({ | ||
selector: 'cookbook-en-locale-provider', | ||
template: '<ng-content></ng-content>', | ||
standalone: true, | ||
providers: [{ provide: LOCALE_ID, useValue: 'en' }, TranslationService], | ||
}) | ||
export class EnLocaleProviderComponent {} |
108 changes: 108 additions & 0 deletions
108
apps/cookbook/src/app/localization/localization.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<article> | ||
<h1>Localization</h1> | ||
<p> | ||
Locale information is used by components such as | ||
<code>kirby-calendar</code> | ||
and | ||
<code>input type="date"</code> | ||
to automatically format values according to locale. Support has been added for three locales out | ||
of the box: American English ( | ||
<code>en-US</code> | ||
), British English ( | ||
<code>en-GB</code> | ||
) and Danish ( | ||
<code>da</code> | ||
). | ||
</p> | ||
|
||
<p> | ||
Locale is usually configured for the entire app in the | ||
<code>AppModule</code> | ||
. | ||
</p> | ||
<cookbook-example-viewer | ||
class="code-only-example" | ||
[ts]="localeConfigCodeSnippet" | ||
></cookbook-example-viewer> | ||
<p> | ||
Specifying the locale will also ensure translation of | ||
<em>internal</em> | ||
component labels and texts that are not configurable. This allows assistive technology such as | ||
screen readers to announce labels in the correct language. | ||
</p> | ||
|
||
<p> | ||
In this example visible information such as the month and weekday headers, and accessible names | ||
for headers, calendar cells and interactive buttons have been automatically translated by | ||
specifying the locale. | ||
</p> | ||
|
||
<cookbook-example-viewer> | ||
<div class="example-frame"> | ||
<div class="locale-examples"> | ||
<cookbook-da-locale-provider> | ||
<kirby-card> | ||
<kirby-calendar [selectedDate]="selectedDate"></kirby-calendar> | ||
</kirby-card> | ||
<kirby-avatar | ||
class="locale-avatar" | ||
size="sm" | ||
imageSrc="assets/images/dk.svg" | ||
altText="Danish locale" | ||
></kirby-avatar> | ||
</cookbook-da-locale-provider> | ||
<cookbook-en-locale-provider> | ||
<kirby-card> | ||
<kirby-calendar [selectedDate]="selectedDate"></kirby-calendar> | ||
</kirby-card> | ||
<kirby-avatar | ||
class="locale-avatar" | ||
size="sm" | ||
imageSrc="assets/images/gb.svg" | ||
altText="English locale" | ||
></kirby-avatar> | ||
</cookbook-en-locale-provider> | ||
</div> | ||
</div> | ||
</cookbook-example-viewer> | ||
|
||
<h2>Using the service directly</h2> | ||
<p> | ||
When developing custom components closely coupled to Kirby that need to use identical | ||
translations, | ||
<code>TranslationService</code> | ||
can be used to fetch and display translated strings. | ||
</p> | ||
<p> | ||
This can be done by injecting the service into the component and get any key defined in the | ||
<a | ||
href="https://github.com/kirbydesign/designsystem/tree/develop/libs/designsystem/shared/src/translation/translation.interface.ts" | ||
> | ||
Translation | ||
</a> | ||
interface. | ||
</p> | ||
<cookbook-code-viewer [ts]="translationGetterCodeSnippet"></cookbook-code-viewer> | ||
|
||
<h2>Additional locales</h2> | ||
<p> | ||
If support for other locales or additional translations are needed please | ||
<!-- prettier-ignore --> | ||
<a class="kirby-external-icon" href="https://github.com/kirbydesign/designsystem/issues/new/choose">create an enhancement issue on Github</a> | ||
and consider contributing the code to make it happen. Please refer to the | ||
<a | ||
href="https://github.com/kirbydesign/designsystem/tree/develop/libs/designsystem/shared/src/translation/translations" | ||
> | ||
existing translation files | ||
</a> | ||
and how | ||
<a | ||
href="https://github.com/kirbydesign/designsystem/tree/develop/libs/designsystem/shared/src/translation/translation.service.ts" | ||
> | ||
translation registering | ||
</a> | ||
is done in the | ||
<code>TranslationService</code> | ||
. | ||
</p> | ||
</article> |
34 changes: 34 additions & 0 deletions
34
apps/cookbook/src/app/localization/localization.component.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
@use 'sass:map'; | ||
@use '../showcase/showcase.shared'; | ||
@use '@kirbydesign/core/src/scss/utils'; | ||
|
||
$avatar-offset: -1 * utils.size('s'); | ||
|
||
:host { | ||
display: block; | ||
container-type: inline-size; | ||
|
||
:has(> .locale-avatar) { | ||
position: relative; | ||
} | ||
} | ||
|
||
.locale-examples { | ||
display: flex; | ||
justify-content: center; | ||
gap: var(--kirby-spacing-xxxl); | ||
|
||
@container (width < #{map.get(utils.$breakpoints, small)}) { | ||
flex-direction: column; | ||
align-items: center; | ||
} | ||
} | ||
|
||
.locale-avatar { | ||
position: absolute; | ||
inset: $avatar-offset $avatar-offset auto auto; | ||
} | ||
|
||
.code-only-example { | ||
margin-block-end: utils.size('s'); | ||
} |
51 changes: 51 additions & 0 deletions
51
apps/cookbook/src/app/localization/localization.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { Component } from '@angular/core'; | ||
|
||
import { CardModule } from '@kirbydesign/designsystem/card'; | ||
import { CalendarComponent } from '@kirbydesign/designsystem/calendar'; | ||
import { TranslationService } from '@kirbydesign/designsystem/shared'; | ||
import { AvatarComponent } from '@kirbydesign/designsystem/avatar'; | ||
import { CodeViewerModule } from '../shared/code-viewer/code-viewer.module'; | ||
import { ExamplesModule } from '../examples/examples.module'; | ||
import { ShowcaseModule } from '../showcase/showcase.module'; | ||
import { DaLocaleProviderComponent } from './locale-provider/da-locale-provider.component'; | ||
import { EnLocaleProviderComponent } from './locale-provider/en-locale-provider.component'; | ||
|
||
@Component({ | ||
selector: 'cookbook-localization', | ||
templateUrl: './localization.component.html', | ||
styleUrls: ['./localization.component.scss'], | ||
standalone: true, | ||
imports: [ | ||
CodeViewerModule, | ||
ShowcaseModule, | ||
ExamplesModule, | ||
DaLocaleProviderComponent, | ||
EnLocaleProviderComponent, | ||
CalendarComponent, | ||
CardModule, | ||
AvatarComponent, | ||
], | ||
}) | ||
export class LocalizationComponent { | ||
localeConfigCodeSnippet = `import { registerLocaleData } from '@angular/common'; | ||
import localeData from '@angular/common/locales/da'; | ||
import { LOCALE_ID, NgModule } from '@angular/core'; | ||
registerLocaleData(localeData); | ||
@NgModule({ | ||
..., | ||
providers: [ | ||
{ provide: LOCALE_ID, useValue: 'da' }, | ||
], | ||
}) | ||
export class AppModule {}`; | ||
translationGetterCodeSnippet = `constructor(private translationService: TranslationService) {} | ||
get previousMonthLabel(): string { | ||
return this.translationService.get('previousMonth'); | ||
}`; | ||
|
||
constructor(public translations: TranslationService) {} | ||
selectedDate: Date = new Date(2025, 0, 1); | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
libs/designsystem/shared/src/translation/translation.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export interface Translation { | ||
$code: string; | ||
back: string; | ||
close: string; | ||
nextMonth: string; | ||
nextSlide: string; | ||
previousMonth: string; | ||
previousSlide: string; | ||
selectYear: string; | ||
} |
Oops, something went wrong.