prestaconcept / PrestaSitemapBundle

A symfony bundle that provides tools to build a rich application sitemap. The main goals are : simple, no databases, various namespace (eg. google image), respect constraints etc.
MIT License
359 stars 101 forks source link

How to include translated routes in dump file ? #300

Closed PicassoHouessou closed 10 months ago

PicassoHouessou commented 2 years ago

My links look like: https://127.0.0.1:8000/en/contact https://127.0.0.1:8000/fr/contact https://127.0.0.1:8000/de/contact

presta_sitemap:
    resource: "@PrestaSitemapBundle/config/routing.yml"
    route_annotation_listener: true
    defaults:
        priority: 1
        changefreq: hourly
        lastmod: now
    alternate:
        enabled: true
        default_locale: "fr"
        locales: ["fr", "en", "de"]
        i18n: symfony

When I run php bin/console presta:sitemaps:dump I don't see in the dump file, the translated route. In the dump file we have only link with my default locale How to get the links with all locales?

yann-eugone commented 2 years ago

Hi @PicassoHouessou

For the bundle to be able to create routing alternate, you have to follow one of the supported convention. In your case, you have to follow symfony localized routes method : https://symfony.com/doc/current/routing.html#localized-routes-i18n.

If you are not following this convention, you can create a listener in your project that will add routing alternates. Please have a look to the one listener that is using this config in the bundle : https://github.com/prestaconcept/PrestaSitemapBundle/blob/3.x/src/EventListener/StaticRoutesAlternateEventListener.php

yann-eugone commented 10 months ago

Question answered, closing

Mecanik commented 1 month ago

Hi @PicassoHouessou

For the bundle to be able to create routing alternate, you have to follow one of the supported convention. In your case, you have to follow symfony localized routes method : https://symfony.com/doc/current/routing.html#localized-routes-i18n.

If you are not following this convention, you can create a listener in your project that will add routing alternates. Please have a look to the one listener that is using this config in the bundle : https://github.com/prestaconcept/PrestaSitemapBundle/blob/3.x/src/EventListener/StaticRoutesAlternateEventListener.php

Hi, sorry, but this makes no sense. I have the same issue, and my project is finished and fully translated in 20+ languages using symfony i18n. There is a lack of documentation in this regard when using Event Listener, and one must go through all the code and try to understand what is happening, how it is happening, why it is happening, and then finally try to figure out how to use it...

Can you please include some proper example / code / information on how to include translated routes when using dump method?

Thanks

Mecanik commented 1 month ago

My links look like: https://127.0.0.1:8000/en/contact https://127.0.0.1:8000/fr/contact https://127.0.0.1:8000/de/contact

presta_sitemap:
    resource: "@PrestaSitemapBundle/config/routing.yml"
    route_annotation_listener: true
    defaults:
        priority: 1
        changefreq: hourly
        lastmod: now
    alternate:
        enabled: true
        default_locale: "fr"
        locales: ["fr", "en", "de"]
        i18n: symfony

When I run php bin/console presta:sitemaps:dump I don't see in the dump file, the translated route. In the dump file we have only link with my default locale How to get the links with all locales?

This is what I have done, since the existing code is not triggering/working.

  1. route_annotation_listener: false (unless you need it)
  2. alternate: enabled: false
  3. Implement yourself the alternate links:
use Presta\SitemapBundle\Sitemap\Url\GoogleMultilangUrlDecorator;

... controller code
... define locales array

... static pages:

    public function registerStaticUrls(UrlContainerInterface $urls, UrlGeneratorInterface $router): void
    {
        $staticPages = [
            'app_contact_us_index' => ['priority' => 0.5, 'changefreq' => UrlConcrete::CHANGEFREQ_WEEKLY],
            'app_about_us_index' => ['priority' => 0.5, 'changefreq' => UrlConcrete::CHANGEFREQ_WEEKLY],
        ];

        foreach ($staticPages as $route => $options) {

            $url = new GoogleMultilangUrlDecorator(
                new UrlConcrete(
                    $router->generate($route, [], UrlGeneratorInterface::ABSOLUTE_URL),
                    null,
                    $options['changefreq'],
                    $options['priority']
                )
            );

            foreach ($this->locales as $locale) {
                $translatedUrl = $router->generate($route, ['_locale' => $locale], UrlGeneratorInterface::ABSOLUTE_URL);
                $url->addLink($translatedUrl, $locale);
            }

            $urls->addUrl($url, 'static');
        }
    }

... dynamic pages, do the exact same thing.

Enjoy.