CmfRoutingBundle & PHPCR-ODM Custom Translation Strategy

243 Views Asked by At

I'm trying to put together the following phpcr structure, which consists of freezing out certain versions of documents as references that are then pushed to external system apis for translation. Once a translation is complete for a locale it is sent back and the relevant locale node is updated with translation content and version reference.

ROOT:
    mstr:
        a-doc
    ref:
        a-doc:
           1.0:
           1.3:
           1.7:
    locale:
        es-ES
           a-doc

So thats all up and working.

Now I'm adding the CmfRountingBundle, and can't figure out how to get the locale routing to work (the add locale pattern option).

I figure I have to create a custom TranslationStrategy, and in my case I just need the loadTranslation method to find the related document in the locale path.

So this is what I've got:

<?php

namespace My\Project\Translation\TranslationStrategy;

use Doctrine\ODM\PHPCR\Translation\TranslationStrategy\TranslationStrategyInterface;
use Doctrine\ODM\PHPCR\Mapping\ClassMetadata;
use PHPCR\NodeInterface;

class CustomTranslationStrategy implements TranslationStrategyInterface
{
   /**
    * {@inheritdoc}
    */
   public function loadTranslation($document, NodeInterface $node, ClassMetadata $metadata, $locale)
   {
       throw new \Exception('Load translation not yet implemented ...');
   }
   ....

I added a translator attribute to my document:

/**
 * @PHPCR\Document(
 *   versionable="full",
 *   translator="custom",
 *   mixins={"mix:created", "mix:lastModified"}
 * )
 */
class Page implements ContentInterface, VersionableContentInterface {

    /**
     * The language this document currently is in
     * @PHPCR\Locale()
     */
    protected $locale;

Updated my config.yml

parameters:
    translation_locales: [zh-CN,de-DE,es-ES,fr-FR,it-IT,ja-JP,ko-KR,en-UK,en-US]

doctrine_phpcr:
    session:
        backend: "%phpcr_backend%"
        workspace: "%phpcr_workspace%"
        username: "%phpcr_user%"
        password: "%phpcr_password%"
    odm:
        auto_mapping: true
        auto_generate_proxy_classes: "%kernel.debug%"
        locales:
            en: [de, fr]
            de: [en, fr]
            fr: [en, de]
            #es: [en]

cmf_core:
  persistence:
    phpcr: 
        translation_strategy: My\Project\Translation\TranslationStrategy\CustomTranslationStrategy
    # if you want another basepath
    # basepath: /custom/basepath
    publish_workflow: false

cmf_routing:
    chain:
        routers_by_id:
            cmf_routing.dynamic_router: 20
            router.default: 100
    dynamic:
        locales: %translation_locales%
        persistence:
            phpcr:
                use_sonata_admin: auto
                content_basepath: /mstr
                admin_basepath: /cms/routes
        templates_by_class:
            %my_page_document%:  MyDemoBundle:Page:simple.html.twig

Now when I go to /fr-FR/a-doc I just see the english content of the document and when i go to /es-ES/a-doc I get a 500 error: The locale 'es-ES' is not present in the list of available locales (i have it commented in the odm locales).

I'm obviously missing the $dm->setTranslationStrategy call the documentation mentions, just not sure how to inject it into the cmf routing bundle so that I get my Exception in loadTranslation.

Any advice on how i can get this working?!

0

There are 0 best solutions below