By Ian Ebden · April 2017
© 2017 DesignKarma. All rights reserved.
This is not a definitive guide – just the way I did this project this time around.
Really easy to setup localized sites – see Craft’s Setting Up a Localized Site guide.
siteUrl
config setting, but there’s a more efficient and flexible way. More on that later.Static copy resides in translation files, but I don’t like ’em.
Thankfully there’s Bob Olde Hampsink’s Translate plugin. It grabs all Craft::t()
, Craft.t()
and ""|t()
instances so you can edit them in Craft, along with everything else.
* Used for any requests that use the server’s IP address or a domain that doesn’t belong to any of the other Apps.
Upload .htaccess and index.php (from Craft’s public/ folder) into rest of your Apps’ public directories, with these edits to index.php
// Path to your craft/ folder
$craftPath = '../../craft';
// Tell Craft to serve the German content
define('CRAFT_LOCALE', 'de');
0default/
craft/
public/
.htaccess
index.php
de/
public/
.htaccess
index.php
es/
public/
.htaccess
index.php
etc...
(Craft-Multi-Environment by nystudio107)
Craft’s own Multi-Environment setup works just fine but with multiple locales and environments you might get more efficiency and flexibility using nystudio107’s Craft-Multi-Environment (CME) for Craft 2 or Craft 3 instead.
Add the following to the top of each locale’s index.php.
// Load the local Craft environment
if (file_exists('../.env.php'))
require_once '../.env.php';
// Default environment
if (!defined('CRAFT_ENVIRONMENT'))
define('CRAFT_ENVIRONMENT', getenv('CRAFTENV_CRAFT_ENVIRONMENT'));
Now add a .env.php to each locale.
0default/
.env.php
craft/
public/
.htaccess
index.php
de/
.env.php
public/
.htaccess
index.php
es/
.env.php
public/
.htaccess
index.php
etc...
Add an .env.php to each locale, so each locale and locale environment can have its’ own settings e.g. Spanish/local, German/live, French/stage…
We can also set an environment variable based on subdomain e.g.
Setting an environment variable based on subdomain.
// 1. Determine the incoming protocol
if (isset($_SERVER['HTTPS']) && (strcasecmp($_SERVER['HTTPS'], 'on') === 0 || $_SERVER['HTTPS'] == 1)
|| isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strcasecmp($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') === 0
) {
$protocol = "https://";
} else {
$protocol = "http://";
}
// 2. Parse URL and set environment based on subdomain
$url = $protocol . $_SERVER['HTTP_HOST'];
$parsedUrl = parse_url($url);
$host = explode('.', $parsedUrl['host']);
$subdomain = $host[0];
if ($subdomain === 'local') {
$currentEnvironment = 'local';
} elseif ($subdomain === 'stage') {
$currentEnvironment = 'stage';
} else {
$currentEnvironment = 'live';
}
Scroll the code window for all the goodies.
Set a $craftEnvVars
array of globally-accessible values, including custom settings.
// The $craftEnvVars are all auto-prefixed with CRAFTENV_ -- you can add
// whatever you want here and access them via getenv() using the prefixed name
$craftEnvVars = array(
// Standard CME settings
'BASE_PATH' => realpath(dirname(__FILE__)) . '/public/',
'BASE_URL' => $protocol . $_SERVER['HTTP_HOST'] . '/',
'DB_HOST' => 'xxxxxxxxx',
'DB_NAME' => 'xxxxxxxxx',
'DB_USER' => 'xxxxxxxxx',
'DB_PASS' => 'xxxxxxxxx',
'SITE_URL' => $protocol . $_SERVER['HTTP_HOST'] . '/'
// Custom locale settings
'CRAFT_ENVIRONMENT' => $currentEnvironment, // local|stage|live
'IS_SYSTEM_ON_LIVE' => false, // Used for controlling on/off state for this locale
'SITE_NAME' => 'Ich Bin Ein Berliner',
'TIMEZONE' => 'Europe/Berlin'
);
// Set all of the .env values, auto-prefixed with `CRAFTENV_`
foreach ($craftEnvVars as $key => $value) {
putenv("CRAFTENV_{$key}={$value}");
}
Now we can access our .env.php $craftEnvVars
in general.php via getenv()
.
// All environments
'*' => array(
'craftEnv' => getenv('CRAFTENV_CRAFT_ENVIRONMENT'), // local|stage|live
'siteName' => getenv('CRAFTENV_SITE_NAME'),
'siteUrl' => getenv('CRAFTENV_SITE_URL'),
'timezone' => getenv('CRAFTENV_TIMEZONE')
etc...
),
// Live (production) environment
'live' => array(
'isSystemOn' => getenv('CRAFTENV_IS_SYSTEM_ON_LIVE') === '1' ? true: false,
etc...
),
// Staging (pre-production) environment
'stage' => array(
'isSystemOn' => true, // May as well be always on for staging and local
etc...
),
// Local (development) environment
'local' => array(
'isSystemOn' => true, // May as well be always on for staging and local
etc...
),
Scroll the code window for all the goodies.
Add any .env.php $craftEnvVars
into db.php too.
// All environments
'*' => array(
'tablePrefix' => 'craft',
'server' => getenv('CRAFTENV_DB_HOST'),
'database' => getenv('CRAFTENV_DB_NAME'),
'user' => getenv('CRAFTENV_DB_USER'),
'password' => getenv('CRAFTENV_DB_PASS'),
),
// Live (production) environment
'live' => array(
),
// Staging (pre-production) environment
'stage' => array(
),
// Local (development) environment
'local' => array(
'database' => 'xxxxxxxxxxxx',
'user' => 'xxxxxxxxxxxx',
'password' => 'xxxxxxxxxxxx'
)
Scroll the code window for all the goodies.
Header set Access-Control-Allow-Origin "*"
SetEnvIf Origin ":" IS_CORS
Header set Access-Control-Allow-Origin "*" env=IS_CORS
1. Tell browsers which language your pages are using.
<html lang="{{ craft.locale|replace('_','-') }}">
Note the |replace
filter to replace Craft’s locale underscores with the preferred dash of ISO language codes. For example en-gb instead of en_gb.
2. Prevent search engines from indexing your staging site.
Add alternate language links to your pages.
{% if entry is defined %}
{% for locale in craft.i18n.getSiteLocales() %}
{% endfor %}
{% endif %}
Outputs as…
etc...
Multi-Environment Config for Craft CMS
Localization & Multi-Environment Setup in Craft
https://designkarma.co.uk
ian@designkarma.co.uk
@designkarma