Skip to content

Commit 193128d

Browse files
authored
Showcase: Convert internationalization routes to TS (#3105)
1 parent 8dc558c commit 193128d

File tree

8 files changed

+163
-109
lines changed

8 files changed

+163
-109
lines changed

showcase/app/controllers/internationalization/translation.js

Lines changed: 0 additions & 70 deletions
This file was deleted.
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
* Copyright (c) HashiCorp, Inc.
3+
* SPDX-License-Identifier: MPL-2.0
4+
*/
5+
6+
import Controller from '@ember/controller';
7+
import { tracked } from '@glimmer/tracking';
8+
import { action } from '@ember/object';
9+
import { service } from '@ember/service';
10+
11+
import type { IntlService } from 'ember-intl';
12+
import type HdsIntlService from '@hashicorp/design-system-components/services/hds-intl';
13+
import type { PageInternationalizationTranslationModel } from 'showcase/routes/page-internationalization/translation';
14+
15+
type ServiceTranslation = {
16+
key: string;
17+
default: string;
18+
};
19+
20+
type ServiceTranslationsMap = {
21+
title: ServiceTranslation;
22+
body: ServiceTranslation;
23+
primaryAction: ServiceTranslation;
24+
secondaryAction: ServiceTranslation;
25+
};
26+
27+
type ServiceTranslationsOutput = {
28+
title: string;
29+
body: string;
30+
primaryAction: string;
31+
secondaryAction: string;
32+
};
33+
34+
const SERVICE_TRANSLATIONS: ServiceTranslationsMap = {
35+
title: {
36+
key: 'showcase.pages.internationalization.translation.title',
37+
default: 'Selected language (Fallback translation).',
38+
},
39+
body: {
40+
key: 'showcase.pages.internationalization.translation.body',
41+
default:
42+
'The text is displayed in the selected language (Fallback translation).',
43+
},
44+
primaryAction: {
45+
key: 'showcase.pages.internationalization.translation.actions.primary',
46+
default: 'Primary action (Fallback translation)',
47+
},
48+
secondaryAction: {
49+
key: 'showcase.pages.internationalization.translation.actions.secondary',
50+
default: 'Secondary action (Fallback translation)',
51+
},
52+
};
53+
54+
export default class PageInternationalizationTranslationController extends Controller {
55+
declare model: PageInternationalizationTranslationModel;
56+
57+
@service intl!: IntlService;
58+
@service hdsIntl!: HdsIntlService;
59+
60+
@tracked lang: string | undefined = undefined;
61+
62+
get hdsIntlTranslations(): ServiceTranslationsOutput {
63+
return Object.entries(SERVICE_TRANSLATIONS).reduce(
64+
(acc, [key, translationKey]) => {
65+
acc[key as keyof ServiceTranslationsMap] = this.hdsIntl.t(
66+
translationKey.key,
67+
{
68+
default: translationKey.default,
69+
},
70+
);
71+
return acc;
72+
},
73+
{} as Record<keyof ServiceTranslationsMap, string>,
74+
);
75+
}
76+
77+
get emberIntlTranslations(): ServiceTranslationsOutput {
78+
return Object.entries(SERVICE_TRANSLATIONS).reduce(
79+
(acc, [key, translationKey]) => {
80+
acc[key as keyof ServiceTranslationsMap] = this.intl.t(
81+
translationKey.key,
82+
);
83+
return acc;
84+
},
85+
{} as Record<keyof ServiceTranslationsMap, string>,
86+
);
87+
}
88+
89+
@action
90+
setLang(event: Event) {
91+
const { value } = event.target as HTMLInputElement;
92+
this.lang = value;
93+
94+
try {
95+
this.intl.setLocale(value);
96+
} catch {
97+
console.error(
98+
`No locale found for the provided language code. Using fallback translation.`,
99+
);
100+
}
101+
}
102+
}

showcase/app/router.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,11 @@ Router.map(function () {
143143
this.route('page-overrides', { path: 'overrides' }, function () {
144144
this.route('power-select');
145145
});
146-
this.route('internationalization', function () {
147-
this.route('translation');
148-
});
146+
this.route(
147+
'page-internationalization',
148+
{ path: 'internationalization' },
149+
function () {
150+
this.route('translation');
151+
},
152+
);
149153
});

showcase/app/routes/internationalization/translation.js

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Copyright (c) HashiCorp, Inc.
3+
* SPDX-License-Identifier: MPL-2.0
4+
*/
5+
6+
import Route from '@ember/routing/route';
7+
8+
export default class PageInternationalizationRoute extends Route {}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright (c) HashiCorp, Inc.
3+
* SPDX-License-Identifier: MPL-2.0
4+
*/
5+
6+
import Route from '@ember/routing/route';
7+
import { service } from '@ember/service';
8+
9+
import type { IntlService } from 'ember-intl';
10+
import type { ModelFrom } from 'showcase/utils/ModelFromRoute';
11+
import type PageInternationalizationTranslationController from 'showcase/controllers/page-internationalization/translation';
12+
13+
export type PageInternationalizationTranslationModel =
14+
ModelFrom<PageInternationalizationTranslationRoute>;
15+
16+
export default class PageInternationalizationTranslationRoute extends Route {
17+
@service intl!: IntlService;
18+
19+
_localeOnEntry: string | undefined = undefined;
20+
21+
// store the locale on entry so we can restore it on exit
22+
beforeModel() {
23+
this._localeOnEntry = this.intl.primaryLocale;
24+
}
25+
26+
// set the locale in the controller so that it doesn't persist between visits
27+
setupController(
28+
controller: PageInternationalizationTranslationController,
29+
model: PageInternationalizationTranslationModel,
30+
) {
31+
super.setupController(controller, model);
32+
33+
controller.lang = this._localeOnEntry;
34+
}
35+
36+
// reset the locale to the one we had on entry
37+
deactivate() {
38+
super.deactivate();
39+
40+
if (this._localeOnEntry) {
41+
this.intl.setLocale(this._localeOnEntry);
42+
}
43+
}
44+
}

showcase/app/templates/index.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<Shw::Text::H2>Internationalization</Shw::Text::H2>
3333
<ol class="shw-text-body">
3434
<li>
35-
<LinkTo @route="internationalization.translation">
35+
<LinkTo @route="page-internationalization.translation">
3636
Translation
3737
</LinkTo>
3838
</li>

showcase/app/templates/internationalization/translation.hbs renamed to showcase/app/templates/page-internationalization/translation.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<code>ember-intl</code>. The locale will be reset back to its initial value when leaving the page.
2424
</Shw::Text::Body>
2525

26-
<Hds::Form::Select::Field @value={{this.lang}} {{on "change" this.setLang}} as |F|>
26+
<Hds::Form::Select::Field {{on "change" this.setLang}} as |F|>
2727
<F.Label>{{hds-t
2828
"showcase.pages.internationalization.translation.language"
2929
default="Language (Fallback translation)"

0 commit comments

Comments
 (0)