[Coldbox 3] Language specific base URL

Hi there,

We have a problem trying to implement language specific base URL’s. We want to do this:

http://website.com/en
http://website.com/de
http://website.com/fr
http://website.com/nl

I have found the following topic with the last post suggesting to set the base URL with an interceptor:
https://groups.google.com/forum/#!topic/coldbox/DFRiXydlPJo

Since that looked like a good solution I tried doing this and I am able to set the SES Base URL using following code:

Interceptor:

`

<cfset arguments.event.setSESBaseURL(“http://” & cgi.http_host & “/en”) />


`

Coldbox.cfc interceptor config:

interceptors = [ // multidomain SES {class="coldbox.system.interceptors.MultiDomainSES"}, //SES {class="coldbox.system.interceptors.SES"}, // Language {class="model.interceptors.language"} ];

When navigating to the /en URL I get the correct result from the base URL when dumping it:

<cfdump var=#event.getSESBaseURL()# abort>

However the /en event is still being fired. I would think that since the Base URL is set to /en it would not see this as an event.

Any ideas what is going wrong?

Thanks.

Regards

Tim

You need to adjust your routes, for example.

addRoute(pattern=":language/:handler/:action?");

Will setup a variable you can use in the RC called language, the above assumes that language and handler are mandatory.

Yes that is a possible solution but then I’ll need to rewrite all my routes. And make sure future routes all have that parameter.
I was under the impression that by changing the Base URL I could leave my current routes. Is there a more generic solution available?

The question mark serves as an optional indicator. You can just use the following and then, if the language variable is not passed, it will look for :handler/:action?

addRoute(pattern=":language?:handler/:action?");

This way, all of your existing routes will work, in addition to the language-specific ones.

The base URL only applies when building URLs, and has nothing to do with how routes are parsed and matched to handler and actions. If you dont’t want to rewrite your routes, you could try creating an onRequestCapture interceptor that fires BEFORE the SES interceptor that strips off the language and puts it elsewhere.

If it were me, I’d just create a base route for each language.

addRoute( pattern=":handler/:action?" language=“en” )

addRoute( pattern=“en/:handler/:action?” language=“en” )
addRoute( pattern=“fr/:handler/:action?” language=“en” )
addRoute( pattern=“sp/:handler/:action?” language=“en” )

This will create rc.language

You can still use your baseURL-setting interceptor to help create the right links, but that’s only half of it. The SES interceptor still needs to know how to parse them.

Would not the pathInfoProvider() method in the routes.cfm file help him accomplish what he is looking to do?

  • Ryan Hinton

Thank you for the suggestions.

@Ryan: The pathInfoProvider() function is not doing alot… How can I check it has been triggered? The path_info variable in CGI is not changed. It looks like a good solution though.

@Brad: The route changes would work too I think but with a generic solution like Jon suggested as we have dynamic languages.

I’ll try some things and let you know what I found out. :slight_smile:

Thanks.

Regards

Tim

Looks like the pathInfoProvider does nothing. I checked the Coldbox code and in SES.cfc it’s doing a structKeyExists(variables, ‘PathInfoProvider’) which is always false. Any solution there? Someone else also has this issue: Google Groups