<p>My current setup for multilingual sites:</p>

<ul>
<li>as Brandon said, make sure there is a <code>vendor/</code> folder at project root level (there should be one in this case otherwise the site wouldn't run).</li>
<li>extract the <code>phpdotenv</code> part of the code from your base <code>index.php</code> and put it into an <code>env.php</code> that you create at the same level as your main <code>index.php</code> file.</li>
<li>require this new file at the top of your main <code>index.php</code> using <code>require_once 'env.php';</code></li>
<li>check that this more modular install is running fine (it should since you have only abstracted the <code>phpdotenv</code> part)</li>
<li>in your <code>fr/</code> and <code>es/</code> locales folders, just copy your main <code>index.php</code>, change the relative paths (the require once and the craft folder) and define your Craft locale.</li>
<li>you should be set</li>
</ul>

<p>Here is a rough draft of what you should get to</p>

<p><strong>public/env.php</strong></p>

<pre><code>// 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-&gt;load();
  $dotenv-&gt;required(['DB_HOST', 'DB_NAME', 'DB_USER', 'DB_PASS']);
} catch (Exception $e) {
  exit('Could not find a .env file.');
}
</code></pre>

<p><strong>public/index.php</strong></p>

<pre><code>&lt;?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 &lt;strong&gt;&lt;code&gt;$craftPath&lt;/code&gt;&lt;/strong&gt; is set correctly in '.__FILE__);
}

require_once $path;
</code></pre>

<p><strong>public/fr/index.php</strong></p>

<pre><code>&lt;?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 &lt;strong&gt;&lt;code&gt;$craftPath&lt;/code&gt;&lt;/strong&gt; is set correctly in '.__FILE__);
}

require_once $path;
</code></pre>

<p>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).</p>

<p>Hope it helps</p>

CC BY-SA 3.0
2018-01-21T13:48:36.280Z
Cheers mate, will test it out today. Just not a fan of adding a layer of complexity on top of things that work fine out of the box.
erwinheiser
CC BY-SA 3.0
2018-01-21T13:56:12.853Z
Layer of security, not complexity. Comoser is basically dependency management for PHP and phpdotenv behaves like an include for environment variables and is also a PHP standard. Both are standard in Craft3 https://github.com/craftcms/docs/blob/master/en/installation.md
Jérôme Coupé
CC BY-SA 3.0
2018-01-21T14:17:31.740Z
OK, old dog new tricks I get it. Couldn't get this to work either though but Quentin just pushed some new code that does the trick, apparently the paths have to be juuuust right or it simply fails. Beer's on me next time I'm in Brussels!
erwinheiser
CC BY-SA 3.0
2018-01-21T10:14:29.623Z
2018-01-21T10:22:22.973Z
2018-01-21T10:22:22.973Z
Jérôme Coupé
<p>Some troubleshooting steps:</p>

<ul>
<li><p>Make sure a <code>vendor/</code> folder exists two levels up from <code>es/</code>. If not, you need to run <code>composer install</code> in your terminal from the directory that contains a <code>composer.json</code> file. (If you don’t have Composer installed, see its <a href="https://getcomposer.org/doc/00-intro.md#installation-linux-unix-osx" rel="nofollow noreferrer">installation instructions</a>.)</p></li>
<li><p>Make sure a <code>.env</code> file exists two levels up from <code>es/</code>. It probably won’t, as you’re not supposed to include your <code>.env</code> file in the Git repo. You can copy it from production, or from <code>.env.example</code> if that exists. Then fill it in with working credentials for your local environment.</p></li>
</ul>

<p>Matt Stauffer wrote a great article explaining how to get phpdotenv running with Craft 2: <a href="https://mattstauffer.com/blog/environment-specific-configuration-for-craftcms-using-phpdotenv/" rel="nofollow noreferrer">https://mattstauffer.com/blog/environment-specific-configuration-for-craftcms-using-phpdotenv/</a> Probably a good idea to read through that just to get an idea of how it’s all supposed to work (and what the advantages are).</p>

CC BY-SA 3.0
2018-01-21T00:25:47.260Z
Thanks for the link to the article Brandon. Both the /vendor folder and the .env file are there though and in the correct spot.
erwinheiser
CC BY-SA 3.0
2018-01-21T01:08:34.067Z
@erwinheiser ok try removing the `try` and `catch` lines, so the actual exception will be allowed to surfaces what does it say?
Brandon Kelly
CC BY-SA 3.0
2018-01-21T13:56:44.837Z
If I do that it's just a blank screen.
erwinheiser
CC BY-SA 3.0
2018-01-20T23:39:14.473Z
2018-01-20T23:39:14.473Z
Brandon Kelly
<p>I inherited a Craft site which uses phpdotenv (which I'm unfamiliar with) and which I now have to make multi-lingual (<a href="https://craftcms.com/docs/localization-guide" rel="nofollow noreferrer">https://craftcms.com/docs/localization-guide</a>) with an added es and fr locale on top of the english language site.
The question is regarding the setup on MAMP which seems to never find the .env file.
On my production server in /es/index.php this works:</p>

<pre class="lang-php prettyprint-override"><code>// Composer autoload
require_once('../../vendor/autoload.php');

// DotEnv
try {
    $dotenv = new Dotenv\Dotenv('../../');
    $dotenv-&gt;required(['DB_HOST', 'DB_NAME', 'DB_USER', 'DB_PASS']);
} catch (Exception $e) {
    exit('Could not find a .env file.');
}

// Path to your craft/ folder
$craftPath = '../../craft';

// Tell Craft to serve the spanish content
define('CRAFT_LOCALE', 'es');
</code></pre>

<p>However, the same code on my local MAMP install will not find the .env file no matter what path I enter into the new dotenv statement....
Anyone have any idea or suggestion? Truly stumped here.</p>

<localization><multi-environment>
phpdotenv with multi-lang Craft setup on MAMP
# phpdotenv with multi-lang Craft setup on MAMP

(Asked by: username_0 on 2018-01-20)

I inherited a Craft site which uses phpdotenv (which I'm unfamiliar with) and which I now have to make multi-lingual (https://craftcms.com/docs/localization-guide) with an added es and fr locale on top of the english language site.
The question is regarding the setup on MAMP which seems to never find the .env file.
On my production server in /es/index.php this works:
// Composer autoload
require_once('../../vendor/autoload.php');

// DotEnv
try {
    $dotenv = new Dotenv\Dotenv('../../');
    $dotenv->required(['DB_HOST', 'DB_NAME', 'DB_USER', 'DB_PASS']);
} catch (Exception $e) {
    exit('Could not find a .env file.');
}

// Path to your craft/ folder
$craftPath = '../../craft';

// Tell Craft to serve the spanish content
define('CRAFT_LOCALE', 'es');

However, the same code on my local MAMP install will not find the .env file no matter what path I enter into the new dotenv statement....
Anyone have any idea or suggestion? Truly stumped here.

**Tags:** localization, multi-environment

---

## 2 answers

### Answer by username_1 on 2018-01-21. Score: 3

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
$craftPath is set correctly in '.__FILE__);
}

require_once $path;

public/fr/index.php
$craftPath 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

> **Comments:**
> **username_0**: Cheers mate, will test it out today. Just not a fan of adding a layer of complexity on top of things that work fine out of the box.  
> **username_1**: Layer of security, not complexity. Comoser is basically dependency management for PHP and phpdotenv behaves like an include for environment variables and is also a PHP standard. Both are standard in Craft3 https://github.com/craftcms/docs/blob/master/en/installation.md  
> **username_0**: OK, old dog new tricks I get it. Couldn't get this to work either though but Quentin just pushed some new code that does the trick, apparently the paths have to be juuuust right or it simply fails. Beer's on me next time I'm in Brussels!  

---

### Answer by username_2 on 2018-01-20. Score: 3

Some troubleshooting steps:

* Make sure a `vendor/` folder exists two levels up from `es/`. If not, you need to run `composer install` in your terminal from the directory that contains a `composer.json` file. (If you don’t have Composer installed, see its [installation instructions](https://getcomposer.org/doc/00-intro.md#installation-linux-unix-osx).)
* Make sure a `.env` file exists two levels up from `es/`. It probably won’t, as you’re not supposed to include your `.env` file in the Git repo. You can copy it from production, or from `.env.example` if that exists. Then fill it in with working credentials for your local environment.

Matt Stauffer wrote a great article explaining how to get phpdotenv running with Craft 2: <https://mattstauffer.com/blog/environment-specific-configuration-for-craftcms-using-phpdotenv/> Probably a good idea to read through that just to get an idea of how it’s all supposed to work (and what the advantages are).

> **Comments:**
> **username_0**: Thanks for the link to the article Brandon. Both the /vendor folder and the .env file are there though and in the correct spot.  
> **username_2**: @erwinheiser ok try removing the `try` and `catch` lines, so the actual exception will be allowed to surfaces what does it say?  
> **username_0**: If I do that it's just a blank screen.  

---

