Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Translator] Add "by-domain" translation dumper #1927

Open
wants to merge 3 commits into
base: 2.x
Choose a base branch
from

Conversation

maelanleborgne
Copy link
Contributor

@maelanleborgne maelanleborgne commented Jun 19, 2024

Q A
Bug fix? no
New feature? yes
Issues Fix #1225
License MIT

Will follow #1930

  • Adds a new translation dumper to dump translations by domains in separated files. Theses files can be imported and registered with the new registerDomain(domain) js function, and accessed using the existing trans function by passing the translation key as a first argument.

TODO:

  • Add documentation
  • Add entry to changelog
  • Add better TS typing to the new functions/object
  • Add a TS declaration file for the new domains files ?

@carsonbot carsonbot added Feature New Feature Status: Needs Review Needs to be reviewed labels Jun 19, 2024
@maelanleborgne maelanleborgne changed the title Add global domains filters + add "by-domain" translation dumper [Translator] Add global domains filters + add "by-domain" translation dumper Jun 19, 2024
@maelanleborgne maelanleborgne force-pushed the feature/split-translation-dump-by-domain branch from 683c1ec to 79f0a13 Compare June 19, 2024 13:16
Comment on lines 41 to 46
$webpackDumperDefinition = $container->getDefinition('ux.translator.translations_dumper.webpack');
$webpackDumperDefinition->setArgument(0, $config['dump_directory']);

$assetMapperDumperDefinition = $container->getDefinition('ux.translator.translations_dumper.asset_mapper');
// Setting to the default value because the folder isn't configurable with asset mapper
$assetMapperDumperDefinition->setArgument(0, $this->processConfiguration($configuration, [])['dump_directory']);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use some configuration for the user to chose which service he wants to use..
... and alias it as "TranslationDumper".

$domainSplit[$domainFileName] ??= [];
$domainSplit[$domainFileName][$translationId] ??= ['id' => $translationId, 'translations' => []];
$domainSplit[$domainFileName][$translationId]['translations'][$domain] ??= [];
$domainSplit[$domainFileName][$translationId]['translations'][$domain][$locale] = $translationDetails['message'];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you'd keep all the locale in one file ?

Copy link
Contributor Author

@maelanleborgne maelanleborgne Jun 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that's the plan right now, otherwise we'd have to import too many modules (1 per combination of domain/locale). Though a filter on the dumped locales could help remove the unnecessary ones. Unless you have something else in mind ?

Comment on lines 85 to 90
if (\in_array($domain, $this->excludedDomains, true)) {
continue;
}
if (0 !== \count($this->domains) && !\in_array($domain, $this->domains, true)) {
continue;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we maybe should use "includedDomain" / "excludedDomain" or something similar, maybe just me but the difference in naming makes it less clear

Comment on lines 42 to 45
$this->filesystem->dumpFile($this->dumpDir.'/configuration.js', sprintf(
"export const localeFallbacks = %s;\n",
json_encode($this->getLocaleFallbacks(...$catalogues), \JSON_THROW_ON_ERROR)
));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a simple concatenation here ?

Comment on lines 46 to 51
$this->filesystem->dumpFile($this->dumpDir.'/configuration.d.ts', <<<'TS'
import { LocaleType } from '@symfony/ux-translator';

export declare const localeFallbacks: Record<LocaleType, LocaleType>;
TS
);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this file static ?

Comment on lines 76 to 86
}

public function setExcludedDomains(array $domains = []): void
{
$this->excludedDomains = $domains;
}

public function setDomains(array $domains): void
{
$this->domains = $domains;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if this should either

  • be in an abstractDumper
  • be in a new TranslationDumpProvider (find a better name^^)

Because there is a certain amount of duplicated code there

Comment on lines 239 to 240
"translations": {
"foobar": {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this "domain as key" necessary ?

Comment on lines 266 to 259
function resetRegisteredDomains() {
_registeredTranslations = {};
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the use case ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was mainly to reset the translations between tests, but I changed the behavior to allow overriding already registered translations, so I got rid of this method.

@@ -30,8 +30,11 @@
* @phpstan-type Locale string
* @phpstan-type MessageId string
*/
class TranslationsDumper
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a massive BC break... not sure the "experimental" would be enough :)

Maybe should we keep it as alias / deprecated ?

@kbond ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about keeping the TranslationDumper as a wrapper that would call either or both the Webpack and AssetMapper dumper + the config dumper ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not ! That could reduce some code duplication too!

Copy link
Collaborator

@Kocal Kocal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, and thanks for your contribution! :)

I didn't look for everything, but isn't there too much things for a single PR?
I see dump per domains, global domains filters, but also a new dumper for AssetMapper... is it too much?

@maelanleborgne
Copy link
Contributor Author

Hi, and thanks for your contribution! :)

I didn't look for everything, but isn't there too much things for a single PR? I see dump per domains, global domains filters, but also a new dumper for AssetMapper... is it too much?

Hi, I thought indeed about making two PRs : one for the filtering and another one for the 'per-domain' dump (which is what the AssetMapperDumper does, there won't be any change on the actual webpack-oriented behavior). I made it in one PR because it all came from the same issue, but I can still split the PR if need be.

@maelanleborgne maelanleborgne changed the title [Translator] Add global domains filters + add "by-domain" translation dumper [Draft] [Translator] Add "by-domain" translation dumper Jun 20, 2024
@maelanleborgne maelanleborgne marked this pull request as draft June 20, 2024 08:50
@maelanleborgne maelanleborgne force-pushed the feature/split-translation-dump-by-domain branch 2 times, most recently from d0b0305 to a370cb7 Compare July 2, 2024 08:59
@maelanleborgne maelanleborgne force-pushed the feature/split-translation-dump-by-domain branch from a370cb7 to 108ed58 Compare July 2, 2024 09:06
@maelanleborgne maelanleborgne force-pushed the feature/split-translation-dump-by-domain branch from e0be201 to 9e3f254 Compare July 2, 2024 09:52
@maelanleborgne maelanleborgne changed the title [Draft] [Translator] Add "by-domain" translation dumper [Translator] Add "by-domain" translation dumper Jul 2, 2024
@maelanleborgne maelanleborgne marked this pull request as ready for review July 2, 2024 09:56
@smnandre
Copy link
Collaborator

Hi @maelanleborgne : do you need anything on this one ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature New Feature Status: Needs Review Needs to be reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Translator] Filter dumped domains
4 participants