Skip to content

Commit c14f393

Browse files
committed
minor #826 [ux.symfony.com] Add demo page for Translator (Kocal)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [ux.symfony.com] Add demo page for Translator | Q | A | ------------- | --- | Bug fix? | no | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Tickets | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT Needs #822 Added interactive demo page for the Translator package, where the user can see the code, the output, and some inputs to play with. https://user-images.githubusercontent.com/2103975/235448398-5cc4e45f-8d5f-475f-8351-c43d57f6a3c7.mov Commits ------- 0e713d2 [ux.symfony.com] Add demo page for Translator
2 parents 247a7ea + 0e713d2 commit c14f393

22 files changed

+828
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
import { Controller } from '@hotwired/stimulus';
2+
import {
3+
trans,
4+
setLocale,
5+
HELLO,
6+
SAY_HELLO,
7+
INVITATION_TITLE,
8+
NUM_OF_APPLES,
9+
FINISH_PLACE,
10+
PUBLISHED_AT,
11+
PROGRESS,
12+
VALUE_OF_OBJECT
13+
} from '../translator';
14+
15+
import hljs from 'highlight.js/lib/core';
16+
import javascript from 'highlight.js/lib/languages/javascript';
17+
18+
hljs.registerLanguage('javascript', javascript);
19+
20+
function highlight({ code, language = 'javascript' }) {
21+
return hljs.highlight(code, { language }).value;
22+
}
23+
24+
export default class extends Controller {
25+
static targets = [
26+
'helloCode',
27+
'helloOutput',
28+
29+
'sayHelloNameInput',
30+
'sayHelloCode',
31+
'sayHelloOutput',
32+
33+
'invitationTitleOrganizationGenderInput',
34+
'invitationTitleOrganizationNameInput',
35+
'invitationTitleCode',
36+
'invitationTitleOutput',
37+
38+
'numOfApplesCountInput',
39+
'numOfApplesCode',
40+
'numOfApplesOutput',
41+
42+
'finishPlacePlaceInput',
43+
'finishPlaceCode',
44+
'finishPlaceOutput',
45+
46+
'publishedAtDateInput',
47+
'publishedAtCode',
48+
'publishedAtOutput',
49+
50+
'progressAtProgressInput',
51+
'progressAtCode',
52+
'progressAtOutput',
53+
54+
'valueOfObjectValueInput',
55+
'valueOfObjectCode',
56+
'valueOfObjectOutput',
57+
]
58+
59+
connect() {
60+
this.render();
61+
}
62+
63+
setLocale(e) {
64+
setLocale(e.target.value);
65+
this.render();
66+
}
67+
68+
render() {
69+
this.helloCodeTarget.innerHTML = highlight({ code: `import { trans, SAY_HELLO } from '../translator';
70+
71+
trans(HELLO)` });
72+
this.helloOutputTarget.textContent = trans(HELLO);
73+
74+
this.sayHelloCodeTarget.innerHTML = highlight({
75+
code: `import { trans, SAY_HELLO } from '../translator';
76+
77+
trans(SAY_HELLO, {
78+
name: ${this.#quoteValue(this.sayHelloNameInputTarget.value)}
79+
})`
80+
});
81+
this.sayHelloOutputTarget.textContent = trans(SAY_HELLO, {
82+
name: this.sayHelloNameInputTarget.value
83+
});
84+
85+
this.invitationTitleCodeTarget.innerHTML = highlight({
86+
code: `import { trans, INVITATION_TITLE } from '../translator';
87+
88+
trans(INVITATION_TITLE, {
89+
organizer_gender: ${this.#quoteValue(this.invitationTitleOrganizationGenderInputTarget.value)},
90+
organizer_name: ${this.#quoteValue(this.invitationTitleOrganizationNameInputTarget.value)},
91+
})`
92+
}) ;
93+
this.invitationTitleOutputTarget.textContent = trans(INVITATION_TITLE, {
94+
organizer_gender: this.invitationTitleOrganizationGenderInputTarget.value,
95+
organizer_name: this.invitationTitleOrganizationNameInputTarget.value
96+
});
97+
98+
this.numOfApplesCodeTarget.innerHTML = highlight({
99+
code: `import { trans, NUM_OF_APPLES } from '../translator';
100+
101+
trans(NUM_OF_APPLES, {
102+
apples: ${this.numOfApplesCountInputTarget.value}
103+
})`
104+
});
105+
this.numOfApplesOutputTarget.textContent = trans(NUM_OF_APPLES, {
106+
apples: this.numOfApplesCountInputTarget.value
107+
})
108+
109+
this.finishPlaceCodeTarget.innerHTML = highlight({
110+
code: `import { trans, FINISH_PLACE } from '../translator';
111+
112+
trans(FINISH_PLACE, {
113+
place: ${this.finishPlacePlaceInputTarget.value}
114+
})`
115+
});
116+
this.finishPlaceOutputTarget.textContent = trans(FINISH_PLACE, {
117+
place: this.finishPlacePlaceInputTarget.value,
118+
});
119+
120+
this.publishedAtCodeTarget.innerHTML = highlight({
121+
code: `import { trans, PUBLISHED_AT } from '../translator';
122+
123+
trans(PUBLISHED_AT, {
124+
publication_date: new Date('${this.publishedAtDateInputTarget.value}')
125+
})`
126+
});
127+
this.publishedAtOutputTarget.textContent = trans(PUBLISHED_AT, {
128+
publication_date: new Date(this.publishedAtDateInputTarget.value),
129+
});
130+
131+
this.progressAtCodeTarget.innerHTML = highlight({
132+
code: `import { trans, PROGRESS } from '../translator';
133+
134+
trans(PROGRESS, {
135+
progress: ${this.progressAtProgressInputTarget.value / 100},
136+
})`
137+
});
138+
this.progressAtOutputTarget.textContent = trans(PROGRESS, {
139+
progress: this.progressAtProgressInputTarget.value / 100,
140+
});
141+
142+
this.valueOfObjectCodeTarget.innerHTML = highlight({
143+
code: `import { trans, VALUE_OF_OBJECT } from '../translator';
144+
145+
trans(VALUE_OF_OBJECT, {
146+
value: ${this.valueOfObjectValueInputTarget.value}
147+
})`
148+
});
149+
this.valueOfObjectOutputTarget.textContent = trans(VALUE_OF_OBJECT, {
150+
value: this.valueOfObjectValueInputTarget.value
151+
});
152+
}
153+
154+
#quoteValue(value) {
155+
return '\'' + value.replace('\'', '\\\'') + '\'';
156+
}
157+
}
-2.77 KB
Binary file not shown.
+3
Loading
Loading

ux.symfony.com/assets/styles/_terminal.scss

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
background-color: $n-900;
33
border-radius: 12px;
44
position: relative;
5-
padding: 30px 0px 0px 0px;
5+
padding: 30px 0 0 0;
66
}
77

88
.terminal-code {
@@ -82,6 +82,13 @@
8282
border-top-right-radius: 0;
8383
border-top-left-radius: 0;
8484
}
85+
86+
:not(.terminal-wrapper) .terminal-body {
87+
margin: 0;
88+
border-radius: 6px;
89+
padding: 12px;
90+
}
91+
8592
.homepage-terminal-swapper pre {
8693
margin-bottom: 0;
8794
}

ux.symfony.com/assets/translator.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* This file is part of the Symfony UX Translator package.
3+
*
4+
* If folder "../var/translations" does not exist, or some translations are missing,
5+
* you must warmup your Symfony cache to refresh JavaScript translations.
6+
*
7+
* If you use TypeScript, you can rename this file to "translator.ts" to take advantage of types checking.
8+
*/
9+
10+
import { trans, getLocale, setLocale, setLocaleFallbacks } from '@symfony/ux-translator';
11+
import { localeFallbacks } from '../var/translations/configuration';
12+
13+
setLocaleFallbacks(localeFallbacks);
14+
15+
export { trans, setLocale };
16+
export * from '../var/translations';

ux.symfony.com/composer.json

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"symfony/notifier": "6.2.*",
2727
"symfony/proxy-manager-bridge": "6.2.*",
2828
"symfony/runtime": "6.2.*",
29+
"symfony/translation": "6.2.*",
2930
"symfony/twig-bundle": "6.2.*",
3031
"symfony/ux-autocomplete": "2.x-dev",
3132
"symfony/ux-chartjs": "2.x-dev",
@@ -37,6 +38,7 @@
3738
"symfony/ux-react": "2.x-dev",
3839
"symfony/ux-svelte": "2.x-dev",
3940
"symfony/ux-swup": "2.x-dev",
41+
"symfony/ux-translator": "2.x-dev",
4042
"symfony/ux-turbo": "2.x-dev",
4143
"symfony/ux-twig-component": "2.x-dev",
4244
"symfony/ux-typed": "2.x-dev",

0 commit comments

Comments
 (0)