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

Lazy translations for queue jobs #9745

Merged

Conversation

brandonkelly
Copy link
Member

@brandonkelly brandonkelly commented Aug 20, 2021

Description

Adds a new craft\i18n\Translation class with two static methods:

  • prep() – Has same arguments as Craft::t(), but instead of translating the message immediately, it will package the arguments up into a JSON-encoded string prefixed with t9n:.
  • translate() – Takes a prepared string from prep() and unpacks it, then translates it for the current application language (which could be different than whatever it was when prep() was called).

All built-in queue jobs are now calling Translation::prep() instead of Craft::t() when defining their descriptions and progress labels, and the built-in queue driver is calling Translation::translate() on job descriptions and progress labels from getJobDetails() and getJobInfo().

The result is that queue messages are now always translated for the current user’s language, rather than whatever language was active at the time the job was queued up, or whatever language the queue runner is using.

Examples

Queuing up a job with a custom description:

use craft\elements\Entry;
use craft\helpers\Queue;
use craft\i18n\Translation;
use craft\queue\jobs\ResaveElements;

Queue::push(new ResaveElements([
    'elementType' => Entry::class,
    'criteria' => ['section' => 'news'],
    'description' => Translation::prep('site', 'Resaving News entries'),
]));

Defining a default description from a job type:

use craft\i18n\Translation;

protected function defaultDescription(): ?string
{
    return Translation::prep('app', 'Resaving {type}', [
        'type' => $this->elementType::pluralLowerDisplayName(),
    ]);
}

Setting the progress:

use craft\i18n\Translation;

$progress = ($step - 1) / $total;
$this->setProgress($queue, $progress, Translation::prep('app', '{step, number} of {total, number}', [
    'step' => $step,
    'total' => $total,
]));

Related issues

@linear
Copy link

linear bot commented Aug 20, 2021

DEV-97 Translate queue job descriptions/progress from UI

Will require storing translation categories/mesages/params in an array.

The Yii2 Inspections plugin is going to need to be updated to understand these as well.

1 similar comment
@linear
Copy link

linear bot commented Aug 20, 2021

DEV-97 Translate queue job descriptions/progress from UI

Will require storing translation categories/mesages/params in an array.

The Yii2 Inspections plugin is going to need to be updated to understand these as well.

@brandonkelly brandonkelly changed the base branch from develop to 4.0 August 20, 2021 15:00
@brandonkelly brandonkelly merged commit 9c17e3f into 4.0 Aug 20, 2021
@brandonkelly brandonkelly deleted the feature/dev-97-translate-queue-job-descriptionsprogress branch August 20, 2021 17:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant