Brain-WP / Cortex

Routing system for WordPress
MIT License
347 stars 20 forks source link

WPML and Cortex #3

Closed meszarosrob closed 9 years ago

meszarosrob commented 9 years ago

Hello Guiseppe,

have you tried using Cortex's routing capabilities with a site that uses WPML for making it multilingual? I guess it won't really work out of the box. Do you think with route groups and and callback it would be manageable? Or in general how would you aproach a site with multiple languages? Multisites?

gmazzap commented 9 years ago

Well I think it works, indeed.

If you use subdomains you can manage it in Cortex using host() method in route definition and inside query, access the subdomain set in the $matches array. This is not documented in Cortex docs, because it is a Symphony routing component feature (see point 5 here).

If you use subdirectory you can use a route variable for it.

Example for subdomains:

Brain\Routes::add( '/{page}' )
    ->host( '{lang}.example.com' )
    ->defaults( [ 'lang' => 'en' ]  )
     ->query( function( $matches ) {
        // $matches['lang'] contain the language, e.g. "en", "it", "es" and so on
        return [
            'post_type'   => 'page',
            'name' => $matches['page'],
        ];
    });

Example for directories:

Brain\Routes::add( '{lang}/{page}' )
    ->defaults( [ 'lang' => 'en' ]  )
    ->query( function( $matches ) {
        // $matches['lang'] contain the language, e.g. "en", "it", "es" and so on
        return [
            'post_type'   => 'page',
            'name' => $matches['page'],
        ];
    });

WPML _should_ set automatically the language and properly show result.

Consider that, internally, a route like that is very similar to a WordPress "standard" rewrite rule and, if I'm not wrong, WPML works with custom rewrite rules.

However it isn't tested, and I can't test it at the moment.

If you can, I would be happy to know if it works and help you to find a solution if not.

Consider that a route group will not work, because as you can read in docs:

Properties set in the group are merged with route properties when the route matched: it makes no sense put in the group properties that act on route matching process (like requirements, defaults…) because when the group is merged matching already happen.

and once host and directory act on route matching, you can use them in route groups.

However, consider that you can create your own route class extendind the default in a way that the language is automatically set to all routes.

gmazzap commented 9 years ago

Hi @meszarosrob once I get no response in a long time, I'm going to close this issue. If you still have some issues feel free to re-open it. Thanks.