3

I am setting up a multi-language site so far so good - everything is pretty obvious what to do.

I am curious if there is a way/best practice to have the environmentVariables array configured so that it knows which key to use based on the current siteUrl key?

I realize the syntax is incorrect/wrong but this is just an example:

'.dev' => array(
    'siteUrl' => array(
        'en' => 'http://local.mysite.dev/',
        'fr' => 'http://local.mysite.dev/fr/',
    ),
    'environmentVariables' => array(
        'en' => array(
            'basePath' => '/path-to-mysite/public/',
            'baseUrl' => 'http://local.mysite.dev/',
         ),
        'fr' => array(
            'basePath' => '/path-to-mysite/public/fr/',
            'baseUrl' => 'http://local.mysite.dev/fr/',
        )
    )
)

Or, is that something I just append to any path(s) in my template given the current section/segment etc?

2 Answers 2

3

I also set up those two environment variables (totally custom variables, with the one specialty that they are passed to the CP) for my multilibgual sites.

But I have both basePath and baseUrl combined for all my locales!

'.dev' => array(
    'siteUrl' => array(
        'en' => 'http://local.mysite.dev/',
        'fr' => 'http://local.mysite.dev/fr/',
    ),
    'environmentVariables' => array(
        'basePath' => '/path-to-mysite/public/',
        'baseUrl' => 'http://local.mysite.dev/',
    )
)

I use these variables for two things:

  • Make assets from local asset sources available to all my locales. I set them absolutely, because relative paths don't do it in this case, as the path to the assets' folder and the URL is a different one from http://example.com/fr/ as it is from http://example.com/.

  • Build absolute URLs to static files from my templates, e.g. <link rel="stylesheet" href="{{ craft.config.environmentVariables.baseUrl }}stylesheets/site.css" /> Both, the url() function and the siteUrl variable don't work in this case, they always return the site's URL including the locale part.

So it's basically just a way to build absolute URLs to Craft assets and files for me.

I'm actually not totally sure how much of this could be solved with root relative paths, but in my case I've also at least one environment where the path from web root to the craft site is differently to my other environments.

(For testing sites on mobile devices where I can't set virtual domains, I have an environment to access the site via the local machine's IP address. As there's multiple sites on that machine, they are all in subfolders under web root.)

2
  • Thanks @carlcs! I've got everything set up & working fine. I was more curious than anything; wanted to see if there was a way I hadn't thought of.
    – Damon
    Commented Aug 24, 2015 at 18:21
  • Vote for this feature request I just posted and we might get a new way. :)
    – carlcs
    Commented Aug 24, 2015 at 18:36
1

Maybe it's an ugly solution but I wanted to keep using only the siteUrl variable, so I added this to my _layout.html

{% if '/fr/' in siteUrl %}
  {% set siteUrl = siteUrl|slice(length, -3) %}
{% endif %}

So the siteUrl will return the URL defined in the settings, without the "fr/".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.