Skip to content

[FrameworkBundle] [Translation] Update XLIFF source tags with new translation:update-xliff-sources command #53630

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

Open
wants to merge 3 commits into
base: 7.4
Choose a base branch
from

Conversation

squrious
Copy link
Contributor

@squrious squrious commented Jan 24, 2024

Q A
Branch? 7.1
Bug fix? no
New feature? yes
Deprecations? no
Issues Fix #33041
License MIT

Context

Currently, XLIFF files generated with the translation:extract command use the translation key to create the <source> tag:

<trans-unit id="hVNhFLX" resname="navbar.home">
  <source>navbar.home</source>
  <target>__navbar.home</target>
</trans-unit>

But usually this <source> tag contains the default locale text to be translated (XLIFF 1.2, XLIFF 2.0).

Also, the <source> tag is already used in XliffFileLoader to populate the catalogue metadata (but only for v1).

Proposed solution

This PR adds three features:

  • use the source metadata (if available) to create the source tag in XliffFileDumper
  • use the source tag to populate the source metadata in XliffFileLoader for XLIFF 2.0
  • add a translation:update-xliff-sources command to update the source tag in XLIFF files

Result

Given the following entries in XLIFF files:

<!-- translations/messages.en.xlf -->

<trans-unit id="hVNhFLX" resname="navbar.home">
  <source>navbar.home</source>
  <target>Home</target>
</trans-unit>
<!-- translations/messages.fr.xlf -->

<trans-unit id="hVNhFLX" resname="navbar.home">
  <source>navbar.home</source>
  <target>__navbar.home</target>
</trans-unit>

Running

bin/console translation:update-xliff-sources

Will produce updates the files with:

<!-- translations/messages.en.xlf -->

<trans-unit id="hVNhFLX" resname="navbar.home">
  <source>Home</source>
  <target>Home</target>
</trans-unit>
<!-- translations/messages.fr.xlf -->

<trans-unit id="hVNhFLX" resname="navbar.home">
  <source>Home</source>
  <target>__navbar.home</target>
</trans-unit>

The command has options to specify locales, domains, and paths:

Description:
  Update source tags with default locale targets in XLIFF files.

Usage:
  translation:update-xliff-sources [options] [--] [<paths>...]

Arguments:
  paths                    Paths to look for translations.

Options:
      --format=FORMAT      Override the default output format. [default: "xlf12"]
      --domains[=DOMAINS]  Specify the domain to update. (multiple values allowed)
      --locales=LOCALES    Specify the locale to update. (multiple values allowed)
  -h, --help               Display help for the given command. When no command is given display help for the list command
  -q, --quiet              Do not output any message
  -V, --version            Display this application version
      --ansi|--no-ansi     Force (or disable --no-ansi) ANSI output
  -n, --no-interaction     Do not ask any interactive question
  -e, --env=ENV            The Environment name. [default: "dev"]
      --no-debug           Switch off debug mode.
      --profile            Enables profiling (requires debug).
  -v|vv|vvv, --verbose     Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Help:
  The translation:update-xliff-sources command updates the source tags in XLIFF files with the default locale translation if available.
  
  You can specify directories to update in the command arguments: 
  
    php bin/console translation:update-xliff-sources path/to/dir path/to/another/dir
  
  To restrict the updates to one or more locales, including the default locale itself, use the --locales option:
  
    php bin/console translation:update-xliff-sources --locales en --locales fr
  
  You can specify one or more domains to target with the --domains option. By default, all available domains for the targeted locales are used.
  
    php bin/console translation:update-xliff-sources --domains messages
  

@squrious squrious requested a review from welcoMattic as a code owner January 24, 2024 17:02
@carsonbot carsonbot added this to the 7.1 milestone Jan 24, 2024
@squrious squrious changed the title Add --update-source option in translation:extract [FrameworkBundle] [Translation] Add --update-source option in translation:extract Jan 24, 2024
@squrious squrious force-pushed the translation/update-source-option-in-extract-command branch from 5219f97 to 708ab39 Compare January 24, 2024 17:04
@stof
Copy link
Member

stof commented Jan 24, 2024

Copying the default locale in the source tags of files for other locales should probably be done in a separate command than translation:extract as this is a different kind of change. This action would be doable for all keys that exist in the translation files, not only for keys that can be extracted from usages.

@squrious squrious changed the title [FrameworkBundle] [Translation] Add --update-source option in translation:extract [FrameworkBundle] [Translation] Update XLIFF source tags with new translation:update-xliff-sources command Jan 25, 2024
@squrious
Copy link
Contributor Author

I created a dedicated command in the Translation component

@squrious squrious force-pushed the translation/update-source-option-in-extract-command branch from 6095355 to efcfc4d Compare January 25, 2024 16:56
@squrious squrious force-pushed the translation/update-source-option-in-extract-command branch from efcfc4d to 496b27a Compare March 18, 2024 09:50
@xabbuh xabbuh modified the milestones: 7.1, 7.2 May 15, 2024
Copy link
Member

@welcoMattic welcoMattic left a comment

Choose a reason for hiding this comment

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

Thanks @squrious for this great work! I have 2 small comments about wording.
Otherwise your PR looks good to me 👍

Can you rebase your branch please?

$defaultLocaleCatalogue = $translatorBag->getCatalogue($this->defaultLocale);

if (!$defaultLocaleCatalogue instanceof MetadataAwareInterface) {
$io->error(sprintf('The default locale catalogue must implement "%s" to be used in this tool.', MetadataAwareInterface::class));
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
$io->error(sprintf('The default locale catalogue must implement "%s" to be used in this tool.', MetadataAwareInterface::class));
$io->error(sprintf('The default locale catalogue must implement "%s" to be used in by this command.', MetadataAwareInterface::class));

$currentCatalogue = $translatorBag->getCatalogue($locale);

if (!$currentCatalogue instanceof MessageCatalogue) {
$io->warning(sprintf('The catalogue for locale "%s" must be an instance of "%s" to be used in this tool.', $locale, MessageCatalogue::class));
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
$io->warning(sprintf('The catalogue for locale "%s" must be an instance of "%s" to be used in this tool.', $locale, MessageCatalogue::class));
$io->warning(sprintf('The catalogue for locale "%s" must be an instance of "%s" to be used by this command.', $locale, MessageCatalogue::class));

new InputOption('locales', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Specify the locale to update.', $this->enabledLocales),
])
->setHelp(<<<EOF
The <info>%command.name%</info> command updates the source tags in XLIFF files with the default locale translation if available.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
The <info>%command.name%</info> command updates the source tags in XLIFF files with the default locale translation if available.
The <info>%command.name%</info> command updates the source tags in XLIFF files with the default translation locale if available.

?

Comment on lines 84 to 90
$format = $input->getOption('format');

if (!\array_key_exists($format, self::FORMATS)) {
$io->error(sprintf('Unknown format "%s". Available formats are: %s.', $format, implode(', ', array_map(fn ($f) => '"'.$f.'"', array_keys(self::FORMATS)))));

return self::INVALID;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't we move this to initialize method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure about that, I can't return any code from the initialize method to indicate the option is invalid, so I would have to throw an exception instead.

I used the same logic as in TranslationUpdateCommand.

* file that was distributed with this source code.
*/

namespace Command;
Copy link
Contributor

Choose a reason for hiding this comment

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

namespace is wrong

@squrious squrious force-pushed the translation/update-source-option-in-extract-command branch 2 times, most recently from ff77984 to f2c0cf4 Compare September 21, 2024 08:22
@squrious
Copy link
Contributor Author

Hello, I updated the PR according to your comments (not sure about the use of initialize to check command options though).

Also moved the changelog entry in the translation component to 7.2.

Low deps tests fail but I guess it's expected as the PR covers both framework bundle and translation component.

@squrious squrious force-pushed the translation/update-source-option-in-extract-command branch from f2c0cf4 to 5e27384 Compare September 21, 2024 08:32
@fabpot fabpot modified the milestones: 7.2, 7.3 Nov 20, 2024
@fabpot
Copy link
Member

fabpot commented Mar 29, 2025

Low deps tests fail but I guess it's expected as the PR covers both framework bundle and translation component.

To fix the low deps tests, you need to bump the required version of the component in the bundle.

@fabpot fabpot modified the milestones: 7.3, 7.4 May 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

XLIFF export should contain default locale default texts in source tags
7 participants