feat: add support for translating Category elements#119
Open
abhinaykhalatkar wants to merge 1 commit into
Open
feat: add support for translating Category elements#119abhinaykhalatkar wants to merge 1 commit into
abhinaykhalatkar wants to merge 1 commit into
Conversation
- new findTargetCategory() in TranslateService with structure-preserving propagation (uses propagateElement, never duplicateElement) - explicit Category branch in ElementHelper::query() — removes a latent Entry-fallback footgun for any future supported element type - type-aware debug-log propagationMethod via match(true) (also unmasks a latent Asset bug under debug=true that produced wrong values) - save Category targets with propagate=false (Craft's category propagation pipeline otherwise silently rejects per-site rows with diverged translated values) - BulkTranslateJob: per-element try/catch around UnsupportedSiteException and InvalidConfigException so one unsupported target site no longer fails the whole bulk job - README: new "Supported element types" section + Category slug listener recipe via afterElementTranslation - CHANGELOG: Unreleased entry documenting the additions + fixes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this does
Adds support for translating Craft Categories. Until now the plugin only handled Entries, Assets, and Commerce Products/Variants : Categories were the obvious missing piece. After this PR they get the same treatment: the Translate button in the sidebar, the bulk action on the index page, the review modal, and the lifecycle events.
Why
Categories in Craft are first-class translatable elements : per-site titles, slugs, custom fields, URIs, the lot. But
MultiTranslator::getSupportedElementClasses()didn't include them, so anyone with translatable categories had to write their own module on top ofTranslateServiceto handle them. This was the biggest gap on the free tier and it shouldn't be one.What changed (6 steps, in this order)
ElementHelper::query(): added an explicit branch forCategory::class. The old code fell back toEntry::find()in anelse, which was a footgun waiting to happen: any new element type would silently get routed as an Entry. Added a comment so future contributors don't trip on this.TranslateService::findTargetCategory(): new method, modelled onfindTargetEntry(). UsesElements::propagateElementso the structure topology is preserved. If the source's group isn't enabled for the target site, it throwsUnsupportedSiteExceptioninstead of silently falling through toduplicateElement(which would orphan the structure).Debug-log block in
TranslateService: was hard-coded to read$source?->section->propagationMethod, which obviously doesn't work for anything that isn't an Entry. Replaced with amatch(true)that picks the right descriptor per element class. As a side benefit this also fixes a quiet bug where Assets produced wrong values in the debug log (Assets don't have->sectioneither).Save block in
TranslateService: Categories are saved withpropagate=false. Craft's category propagation silently rejects per-site updates when the translated values diverge, sopropagate=truewould lose the translation.falseupdates just the per-site row, which is what we want : the row already exists by this point (either from step 2 or from Craft auto-propagating on first save).MultiTranslator::getSupportedElementClasses(): addedCategory::class. This is the actual "switch it on" change, and it lands last on purpose so the sidebar button never shows up before the dispatch underneath it works.BulkTranslateJob: wrapped each element in atry/catchforUnsupportedSiteException | InvalidConfigException. One bad target site no longer takes down the whole bulk job.There are two behaviour changes, both strictly more lenient:
propagationMethodfield now emits sensible values instead ofnull(or throwing) for Categories and AssetsOne thing to flag on permissions: anyone who already has Translate access on Entries will automatically have it on Categories. That's intentional and called out in the CHANGELOG, but worth surfacing here too.
How I tested it
Against Craft 5 with DeepL on a 2-site setup (en-US + de-DE), plus a third unsupported site (fr-FR) to exercise the failure paths.
content_blockswith nested content (Books→Bücher, Electronics→Elektronik, Fiction→Belletristik, Programming→Programmierung, Phones→Handys, Laptops→Laptops)deis thedeBooks : same canonical id as ondefault)debug=true, the log line for a Category translation contains'group:testCategories'and doesn't throwUnsupportedSiteExceptionflashes cleanly on single-translate, and the bulk job logs and moves past itsourceElement/targetElement