Skip to content

Commit 4b3aa8d

Browse files
committed
feature #616 [Translator] Add Symfony UX Translator package (Kocal)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [Translator] Add Symfony UX Translator package | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes <!-- 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 Hi everyone, this PR add a new UX Translator package, which allow to re-use the same logic than the Symfony Translator, but in JavaScript. The Intl ICU syntax is also supported. # Why? This package is not a revolution, there is already https://github.com/willdurand/BazingaJsTranslationBundle which allows to re-use translations (defined in your Symfony app) in JavaScript. ## Loading issues However, there is one big difference between https://github.com/willdurand/BazingaJsTranslationBundle and this new UX package: the way translations are passed to JavaScript. To me, Bazinga contains performance issues. If you choose to dump your translations into a separated `.js` or `.json`, or if you choose to load translations through a separate HTTP request, in each case you will need to dump and load the **whole** catalogue (for a given domain), which contains messages that you will not use into the webpage. With the Symfony UX Translator package's logic, you explicitly define which translations you wants to load, making HTTP responses **less heavier** and/or JavaScript execution **faster**. ## Types definition Another issue is that Bazinga does not provide types definitions in the same repository. Instead, you must install [``@types`/bazinga-translator`](https://www.npmjs.com/package/`@types`/bazinga-translator) package separately, which can lead to some "desynchronization" between the upstream code and types. # How Everything is documented in https://github.com/Kocal/symfony-ux/blob/feat/translator/src/Translator/doc/index.rst, but to be quick: - a CacheWarmer dumps your app's translations as constants into a JavaScript file (types definitions are generated too), that you can imports later in your JavaScript - a function `trans()` can be imported from ``@symfony`/ux-translator`, which: - works the same as `TranslatorInterface#trans()` from Symfony (PHP) - fully supports ICU, thanks to the code ported from https://formatjs.io/docs/icu-messageformat-parser - support default locale (configured, or from the `<html>`'s `lang` attribute, and locales fallbacks - support TypeScript types definitions, telling you and validate which parameters are usable or not, their types, which domain or locale is usable, ... - the bundle's size will stay as lightweight as possible, thanks to the Tree Shaking Commits ------- 78f3aa7 [Translator] Add Symfony UX Translator package
2 parents ce06df3 + 78f3aa7 commit 4b3aa8d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+4823
-0
lines changed

.github/workflows/test.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,15 @@ jobs:
164164
working-directory: src/Autocomplete
165165
run: php vendor/bin/simple-phpunit
166166

167+
- name: Translator Dependencies
168+
uses: ramsey/composer-install@v2
169+
with:
170+
working-directory: src/Translator
171+
dependency-versions: lowest
172+
- name: Translator Tests
173+
run: php vendor/bin/simple-phpunit
174+
working-directory: src/Translator
175+
167176
tests-php-high-deps:
168177
runs-on: ubuntu-latest
169178
steps:
@@ -261,6 +270,14 @@ jobs:
261270
- name: Notify Tests
262271
working-directory: src/Notify
263272
run: php vendor/bin/simple-phpunit
273+
274+
- name: Translator Dependencies
275+
uses: ramsey/composer-install@v2
276+
with:
277+
working-directory: src/Translator
278+
- name: Translator Tests
279+
working-directory: src/Translator
280+
run: php vendor/bin/simple-phpunit
264281

265282
tests-js:
266283
runs-on: ubuntu-latest

src/Translator/.gitattributes

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/.gitattributes export-ignore
2+
/.gitignore export-ignore
3+
/.symfony.bundle.yaml export-ignore
4+
/phpunit.xml.dist export-ignore
5+
/assets/src export-ignore
6+
/assets/test export-ignore
7+
/assets/jest.config.js export-ignore
8+
/tests export-ignore

src/Translator/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
vendor
2+
composer.lock
3+
.php_cs.cache
4+
.phpunit.result.cache

src/Translator/.symfony.bundle.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
branches: ["2.x"]
2+
maintained_branches: ["2.x"]
3+
doc_dir: "doc"

src/Translator/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# CHANGELOG
2+
3+
## Unreleased
4+
5+
- Component added

src/Translator/LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2023-present Fabien Potencier
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

src/Translator/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Symfony UX Translator
2+
3+
**EXPERIMENTAL** This component is currently experimental and is
4+
likely to change, or even change drastically.
5+
6+
Symfony UX Translator integrates [Symfony Translation](https://symfony.com/doc/current/translation.html) for JavaScript.
7+
8+
**This repository is a READ-ONLY sub-tree split**. See
9+
https://github.com/symfony/ux to create issues or submit pull requests.
10+
11+
## Resources
12+
13+
- [Documentation](https://symfony.com/bundles/ux-translator/current/index.html)
14+
- [Report issues](https://github.com/symfony/ux/issues) and
15+
[send Pull Requests](https://github.com/symfony/ux/pulls)
16+
in the [main Symfony UX repository](https://github.com/symfony/ux)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export declare function format(id: string, parameters: Record<string, string | number> | undefined, locale: string): string;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export declare function formatIntl(id: string, parameters: Record<string, string | number> | undefined, locale: string): string;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export type DomainType = string;
2+
export type LocaleType = string;
3+
export type TranslationsType = Record<DomainType, {
4+
parameters: ParametersType;
5+
}>;
6+
export type NoParametersType = Record<string, never>;
7+
export type ParametersType = Record<string, string | number> | NoParametersType;
8+
export type RemoveIntlIcuSuffix<T> = T extends `${infer U}+intl-icu` ? U : T;
9+
export type DomainsOf<M> = M extends Message<infer Translations, LocaleType> ? keyof Translations : never;
10+
export type LocaleOf<M> = M extends Message<TranslationsType, infer Locale> ? Locale : never;
11+
export type ParametersOf<M, D extends DomainType> = M extends Message<infer Translations, LocaleType> ? Translations[D] extends {
12+
parameters: infer Parameters;
13+
} ? Parameters : never : never;
14+
export interface Message<Translations extends TranslationsType, Locale extends LocaleType> {
15+
id: string;
16+
translations: {
17+
[domain in DomainType]: {
18+
[locale in Locale]: string;
19+
};
20+
};
21+
}
22+
export declare function setLocale(locale: LocaleType | null): void;
23+
export declare function getLocale(): LocaleType;
24+
export declare function setLocaleFallbacks(localeFallbacks: Record<LocaleType, LocaleType>): void;
25+
export declare function getLocaleFallbacks(): Record<LocaleType, LocaleType>;
26+
export declare function trans<M extends Message<TranslationsType, LocaleType>, D extends DomainsOf<M>, P extends ParametersOf<M, D>>(...args: P extends NoParametersType ? [message: M, parameters?: P, domain?: RemoveIntlIcuSuffix<D>, locale?: LocaleOf<M>] : [message: M, parameters: P, domain?: RemoveIntlIcuSuffix<D>, locale?: LocaleOf<M>]): string;

0 commit comments

Comments
 (0)