Skip to content

Commit aaa5c9d

Browse files
committed
feature #3208 [Translator] Refactor API to use string-based translation keys instead of generated constants (Kocal)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [Translator] Refactor API to use string-based translation keys instead of generated constants | Q | A | -------------- | --- | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- if yes, also update UPGRADE-*.md and src/**/CHANGELOG.md --> | Documentation? | yes <!-- required for new features, or documentation updates --> | Issues | Fix #2619 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT This pull request introduces a **breaking change** to the UX Translator component, refactoring its API to use string-based translation keys instead of generated constants. The main advantages are: - You can now use **exactly the same translation keys** as in your Symfony PHP code - Simpler and more readable code - No need to memorize generated constant names - No need to import translation constants: smaller files - And you can still get autocompletion and type-safety 🚀 **Before:** ```typescript import { trans } from '`@symfony`/ux-translator'; import { SYMFONY_GREAT } from '`@app`/translations'; trans(SYMFONY_GREAT); ``` **After:** ```typescript import { createTranslator } from '`@symfony`/ux-translator'; import { messages } from '../var/translations/index.js'; const { trans } = createTranslator({ messages }); trans('symfony.great'); ``` Changing the import from ``@app`/translations` to `../var/translations/index.js` allows IDEs and other tools to correctly interpret these files, which is required to make `trans()` type-safe. It also make the configuration easier for AssetMapper users, they must now remove these entries from their `importmap.php`: ```php '`@app`/translations' => [ 'path' => './var/translations/index.js', ], '`@app`/translations/configuration' => [ 'path' => './var/translations/configuration.js', ], ``` Commits ------- a8de99d Update Encore app d77ffc0 Update ux.symfony.com app fc0ccfa Update E2E app 2519033 [Translator] Refactor API to use string-based translation keys instead of generated constants
2 parents 303731a + a8de99d commit aaa5c9d

32 files changed

+923
-723
lines changed

apps/e2e/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# E2E App
22

3-
This is a Symfony application designed for end-to-end testing.
3+
This is a Symfony application designed for end-to-end testing.
44

5-
It serves for testing UX packages in a real-world scenario,
5+
It serves for testing UX packages in a real-world scenario,
66
to ensure they work as expected for multiple Symfony versions and various browsers.
77

88
## Requirements
@@ -16,7 +16,7 @@ to ensure they work as expected for multiple Symfony versions and various browse
1616

1717
```shell
1818
docker compose up -d
19-
symfony php ../.github/build-packages.php
19+
symfony php ../../.github/build-packages.php
2020

2121
SYMFONY_REQUIRE=6.4.* symfony composer update
2222
# or...

apps/e2e/assets/app.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { registerVueControllerComponents } from '@symfony/ux-vue';
22
import { registerSvelteControllerComponents } from '@symfony/ux-svelte';
33
import { registerReactControllerComponents } from '@symfony/ux-react';
44
import './bootstrap.js';
5+
import { trans } from "./translator.js";
6+
57
/*
68
* Welcome to your app's main JavaScript file!
79
*
@@ -16,3 +18,5 @@ console.log('This log comes from assets/app.js - welcome to AssetMapper! 🎉');
1618
registerReactControllerComponents();
1719
registerSvelteControllerComponents();
1820
registerVueControllerComponents();
21+
22+
export { trans };

apps/e2e/assets/translator.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { localeFallbacks } from '@app/translations/configuration';
2-
import { trans, getLocale, setLocale, setLocaleFallbacks } from '@symfony/ux-translator';
1+
import { createTranslator } from "@symfony/ux-translator";
2+
import { messages, localeFallbacks } from "../var/translations/index.js";
3+
34
/*
45
* This file is part of the Symfony UX Translator package.
56
*
@@ -9,8 +10,9 @@ import { trans, getLocale, setLocale, setLocaleFallbacks } from '@symfony/ux-tra
910
* If you use TypeScript, you can rename this file to "translator.ts" to take advantage of types checking.
1011
*/
1112

12-
setLocaleFallbacks(localeFallbacks);
13-
14-
export { trans };
13+
export const translator = createTranslator({
14+
messages,
15+
localeFallbacks,
16+
});
1517

16-
export * from '@app/translations';
18+
export const { trans, setLocale } = translator;

apps/e2e/importmap.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,6 @@
139139
'@symfony/ux-translator' => [
140140
'path' => './vendor/symfony/ux-translator/assets/dist/translator_controller.js',
141141
],
142-
'@app/translations' => [
143-
'path' => './var/translations/index.js',
144-
],
145-
'@app/translations/configuration' => [
146-
'path' => './var/translations/configuration.js',
147-
],
148142
'typed.js' => [
149143
'version' => '2.1.0',
150144
],

apps/e2e/templates/ux_translator/basic.html.twig

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111
{% block javascripts %}
1212
{{ parent() }}
1313
<script type="module">
14-
import { trans } from '@symfony/ux-translator';
15-
import { HELLO } from '@app/translations';
14+
import { trans } from 'app';
1615
1716
function render() {
1817
document.querySelector('[data-testid="output"]').innerHTML = `
1918
<h2>Output</h2>
2019
<ul >
21-
<li>🇬🇧 ${trans(HELLO, {}, 'messages', 'en')}</li>
22-
<li>🇫🇷 ${trans(HELLO, {}, 'messages', 'fr')}</li>
20+
<li>🇬🇧 ${trans('hello', {}, 'messages', 'en')}</li>
21+
<li>🇫🇷 ${trans('hello', {}, 'messages', 'fr')}</li>
2322
</ul>
2423
`
2524
}

apps/e2e/templates/ux_translator/icu_date_time.html.twig

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,16 @@
1515
{% block javascripts %}
1616
{{ parent() }}
1717
<script type="module">
18-
import { trans } from '@symfony/ux-translator';
19-
import { PUBLISHED_AT } from '@app/translations';
18+
import { trans } from 'app';
2019
2120
const inputDate = document.querySelector('#date');
2221
2322
function render() {
2423
document.querySelector('[data-testid="output"]').innerHTML = `
2524
<h2>Output</h2>
2625
<ul >
27-
<li>🇬🇧 ${trans(PUBLISHED_AT, { publication_date: new Date(inputDate.value) }, 'messages', 'en')}</li>
28-
<li>🇫🇷 ${trans(PUBLISHED_AT, { publication_date: new Date(inputDate.value) }, 'messages', 'fr')}</li>
26+
<li>🇬🇧 ${trans('published_at', { publication_date: new Date(inputDate.value) }, 'messages', 'en')}</li>
27+
<li>🇫🇷 ${trans('published_at', { publication_date: new Date(inputDate.value) }, 'messages', 'fr')}</li>
2928
</ul>
3029
`
3130
}

apps/e2e/templates/ux_translator/icu_number_currency.html.twig

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,16 @@
1515
{% block javascripts %}
1616
{{ parent() }}
1717
<script type="module">
18-
import { trans } from '@symfony/ux-translator';
19-
import { VALUE_OF_OBJECT } from '@app/translations';
18+
import { trans } from 'app';
2019
2120
const inputPrice = document.querySelector('#price');
2221
2322
function render() {
2423
document.querySelector('[data-testid="output"]').innerHTML = `
2524
<h2>Output</h2>
2625
<ul >
27-
<li>🇬🇧 ${trans(VALUE_OF_OBJECT, { price: inputPrice.value }, 'messages', 'en')}</li>
28-
<li>🇫🇷 ${trans(VALUE_OF_OBJECT, { price: inputPrice.value }, 'messages', 'fr')}</li>
26+
<li>🇬🇧 ${trans('value_of_object', { price: inputPrice.value }, 'messages', 'en')}</li>
27+
<li>🇫🇷 ${trans('value_of_object', { price: inputPrice.value }, 'messages', 'fr')}</li>
2928
</ul>
3029
`
3130
}

apps/e2e/templates/ux_translator/icu_number_percent.html.twig

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,16 @@
1515
{% block javascripts %}
1616
{{ parent() }}
1717
<script type="module">
18-
import { trans } from '@symfony/ux-translator';
19-
import { PROGRESS } from '@app/translations';
18+
import { trans } from 'app';
2019
2120
const inputProgress = document.querySelector('#progress');
2221
2322
function render() {
2423
document.querySelector('[data-testid="output"]').innerHTML = `
2524
<h2>Output</h2>
2625
<ul >
27-
<li>🇬🇧 ${trans(PROGRESS, { progress: inputProgress.value }, 'messages', 'en')}</li>
28-
<li>🇫🇷 ${trans(PROGRESS, { progress: inputProgress.value }, 'messages', 'fr')}</li>
26+
<li>🇬🇧 ${trans('progress', { progress: inputProgress.value }, 'messages', 'en')}</li>
27+
<li>🇫🇷 ${trans('progress', { progress: inputProgress.value }, 'messages', 'fr')}</li>
2928
</ul>
3029
`
3130
}

apps/e2e/templates/ux_translator/icu_plural.html.twig

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,16 @@
1515
{% block javascripts %}
1616
{{ parent() }}
1717
<script type="module">
18-
import { trans } from '@symfony/ux-translator';
19-
import { NUM_OF_APPLES } from '@app/translations';
18+
import { trans } from 'app';
2019
2120
const inputApples = document.querySelector('#apples');
2221
2322
function render() {
2423
document.querySelector('[data-testid="output"]').innerHTML = `
2524
<h2>Output</h2>
2625
<ul >
27-
<li>🇬🇧 ${trans(NUM_OF_APPLES, { apples: inputApples.valueAsNumber }, 'messages', 'en')}</li>
28-
<li>🇫🇷 ${trans(NUM_OF_APPLES, { apples: inputApples.valueAsNumber }, 'messages', 'fr')}</li>
26+
<li>🇬🇧 ${trans('num_of_apples', { apples: inputApples.valueAsNumber }, 'messages', 'en')}</li>
27+
<li>🇫🇷 ${trans('num_of_apples', { apples: inputApples.valueAsNumber }, 'messages', 'fr')}</li>
2928
</ul>
3029
`
3130
}

apps/e2e/templates/ux_translator/icu_select.html.twig

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,16 @@
2020
{% block javascripts %}
2121
{{ parent() }}
2222
<script type="module">
23-
import { trans } from '@symfony/ux-translator';
24-
import { INVITATION_TITLE } from '@app/translations';
23+
import { trans } from 'app';
2524
2625
const selectGender = document.querySelector('#gender');
2726
2827
function render() {
2928
document.querySelector('[data-testid="output"]').innerHTML = `
3029
<h2>Output</h2>
3130
<ul >
32-
<li>🇬🇧 ${trans(INVITATION_TITLE, { organizer_gender: selectGender.value, organizer_name: 'Alex' }, 'messages', 'en')}</li>
33-
<li>🇫🇷 ${trans(INVITATION_TITLE, { organizer_gender: selectGender.value, organizer_name: 'Alex' }, 'messages', 'fr')}</li>
31+
<li>🇬🇧 ${trans('invitation_title', { organizer_gender: selectGender.value, organizer_name: 'Alex' }, 'messages', 'en')}</li>
32+
<li>🇫🇷 ${trans('invitation_title', { organizer_gender: selectGender.value, organizer_name: 'Alex' }, 'messages', 'fr')}</li>
3433
</ul>
3534
`
3635
}

0 commit comments

Comments
 (0)