doublesecretagency / craft-siteswitcher

Site Switcher plugin for Craft CMS
MIT License
67 stars 7 forks source link

Translate Non Element URL Path #17

Open erktime opened 4 years ago

erktime commented 4 years ago

Your plugin does what i want it to do in all cases except when i have a URL of a non-element page that has translated paths.

For example: site.com/house site.com/es/casa

What do you think about sending the path you generate from the request through the craft translation service for the locale you're generating a link for:

Basically translate the string you get on this line: https://github.com/doublesecretagency/craft-siteswitcher/blob/v2/src/services/SiteSwitcherService.php#L115

lindseydiloreto commented 4 years ago

Hi there. Not sure I'm following.

How do you have translated paths for a non-element page? Where are those routes being stored?

How would you recommend translating the string from that line?

erktime commented 4 years ago

An example of this use case is: when you define a section in craft that has multiple sites enabled you can define a custom URI for that section per site. So if you have the following sites:

You can define a section URI in English and Spanish. You could also do various routing in your config/routes.php or in the routes admin area.

However you do it though, you would eventually need to write links to those pages. I've written my own site switcher at this point that does this. For non entry URLs i do something along these lines to correctly translate the path. This assumes you have the correct mapping in translation files:

// Get page URI
$pageUri = Craft::$app->getRequest()->getPathInfo();
// Translate path
$pageUri = Craft::t('site', $pageUri, null, $site->language);
// Return localized non-element URL
$url = UrlHelper::siteUrl($pageUri, null, null, $site->id);

At this point i don't need the fix, but when evaluating the plugin, I needed this capability.

ryanmwc commented 4 years ago

The title of this post fits my problem but I'm not able to fully follow what's been said, so here's my take:

I have English and German sites. I'm creating a Craft Commerce checkout flow and these pages don't have corresponding entries, so I've created custom routes in my ./config/routes.php file:

$routes = [
    // default site's handle is 'en'
    'en' => [
        'order' => ['template' => '_checkout/index'],
        'order/selection' => ['template' => '_checkout/selection'],
        'order/delivery' => ['template' => '_checkout/delivery'],
        'order/overview' => ['template' => '_checkout/overview'],
        'order/confirmation' => ['template' => '_checkout/confirmation'],
    ],

    // second site's handle is 'de'
    'de' => [
        'bestellen' => ['template' => '_checkout/index'],
        'bestellen/auswahl' => ['template' => '_checkout/selection'],
        'bestellen/lieferdetails' => ['template' => '_checkout/delivery'],
        'bestellen/uebersicht' => ['template' => '_checkout/overview'],
        'bestellen/bestaetigung' => ['template' => '_checkout/confirmation'],
    ],
];

How can I use the siteswitcher plugin to flip back and forth between each sites' counterpart route? For example, if I'm on /en/order/selection and click the switcher, how can the plugin know to switch to /de/bestellen/auswahl?

lindseydiloreto commented 4 years ago

Hi @ryanmwc,

At the moment, Site Switcher really can't account for routes set outside of the section URL definition. There is currently no way for it to know which routes belong to which pages.

I believe it's the same issue that the OP was having. I'm not sure what his fix was, but it sounds like he either forked this plugin, or wrote his own custom solution.

So as it currently stands, this is simply beyond the capabilities of Site Switcher. For now, we'll just keep this thread open as a feature request! 🙂

arentsen commented 3 years ago

@ryanmwc You could add a Single section for ie. /order/selection and set the URI for each language, then Craft will be able to translate them, IF there is no slug behind /order/selection. You would have to add quite a few single pages.

For me, it does not work because my URI does have a slug after the translated route: /path-that-needs-translation/{slug}

Obviously, I want the path to be translated through the translation config file and the slug through the SiteSwitcher looking up my entries and categories. Any suggestions @erktime ?

juddlyon commented 3 years ago

Same scenario for me, I'm also in need of support for translated routes.

scoch5 commented 5 months ago

I'm also in need of support for translated routes.