My current setup for multilingual sites:
- as Brandon said, make sure there is a
vendor/
folder at project root level (there should be one in this case otherwise the site wouldn't run).
- extract the
phpdotenv
part of the code from your base index.php
and put it into an env.php
that you create at the same level as your main index.php
file.
- require this new file at the top of your main
index.php
using require_once 'env.php';
- check that this more modular install is running fine (it should since you have only abstracted the
phpdotenv
part)
- in your
fr/
and es/
locales folders, just copy your main index.php
, change the relative paths (the require once and the craft folder) and define your Craft locale.
- you should be set
Here is a rough draft of what you should get to
public/env.php
// Composer autoload (assuming '/vendor' is at the root and this is in '/public'
require_once('../vendor/autoload.php');
// DotEnv
try {
$dotenv = new Dotenv\Dotenv(dirname(__DIR__));
$dotenv->load();
$dotenv->required(['DB_HOST', 'DB_NAME', 'DB_USER', 'DB_PASS']);
} catch (Exception $e) {
exit('Could not find a .env file.');
}
public/index.php
<?php
// require 'env.php' only once
require_once 'env.php';
// Path to your craft/ folder
$craftPath = '../craft';
// Tell Craft to serve the French content
define('CRAFT_LOCALE', 'en');
// Do not edit below this line
$path = rtrim($craftPath, '/').'/app/index.php';
if (!is_file($path))
{
if (function_exists('http_response_code'))
{
http_response_code(503);
}
exit('Could not find your craft/ folder. Please ensure that <strong><code>$craftPath</code></strong> is set correctly in '.__FILE__);
}
require_once $path;
public/fr/index.php
<?php
// require 'env.php' only once
require_once '../env.php';
// Path to your craft/ folder
$craftPath = '../../craft';
// Tell Craft to serve the French content
define('CRAFT_LOCALE', 'fr');
// Do not edit below this line
$path = rtrim($craftPath, '/').'/app/index.php';
if (!is_file($path))
{
if (function_exists('http_response_code'))
{
http_response_code(503);
}
exit('Could not find your craft/ folder. Please ensure that <strong><code>$craftPath</code></strong> is set correctly in '.__FILE__);
}
require_once $path;
phpdotenv and composer are an integral part of Craft3 as well as of all modern PHP apps so it's worth getting familiar with. Basically just dependency management for PHP (Composer) and putting all your sensitive info in a file not committed to repo (phpdotenv).
Hope it helps