<p>I just tried this and it works with Craft 2.3.2636. The only difference is this isn't using <a href="http://buildwithcraft.com/classreference/models/BaseModel#setAttributes-detail" rel="nofollow"><code>ContentModel::setAttributes()</code></a>, but I wouldn't have thought that would affect it.</p>

<pre><code>$entry = new EntryModel();
$entry-&gt;sectionId   = 2;
$entry-&gt;typeId      = 2;
$entry-&gt;authorId    = 1;
$entry-&gt;enabled     = true;
$entry-&gt;locale      = "en";

$entry-&gt;getContent()-&gt;title       = "English Title";
$entry-&gt;getContent()-&gt;description = "English description.";

if (craft()-&gt;entries-&gt;saveEntry($entry))
{
    $entryEs = craft()-&gt;entries-&gt;getEntryById($entry-&gt;id, "es");

    $entryEs-&gt;getContent()-&gt;title       = "Spanish Title";
    $entryEs-&gt;getContent()-&gt;description = "Spanish description.";

    craft()-&gt;entries-&gt;saveEntry($entryEs);
}
</code></pre>

CC BY-SA 3.0
2015-04-01T17:13:42.257Z
Thanks Mike. I'm unable to test this at the moment, but I'll take your word for it and mark as correct. Thanks.
Douglas McDonald
CC BY-SA 3.0
2015-06-01T15:49:46.217Z
You may also want to use `$entryEs->localeEnabled = true;` to make sure the entry is enabled in that language
Martin
CC BY-SA 3.0
2015-03-05T00:21:02.350Z
2015-03-05T00:21:02.350Z
Mike Pepper
<p>I'm trying to import data from another database using locales, but am having trouble saving the other locale after the primary locale is saved.</p>

<pre><code>foreach($row as $data) {

    $entry = new EntryModel();
    $entry-&gt;sectionId   = 1;
    $entry-&gt;typeId      = 1;
    $entry-&gt;authorId    = 1;
    $entry-&gt;enabled     = true;
    $entry-&gt;locale      = "en";

    $entry-&gt;getContent()-&gt;setAttributes(array(
        'description' =&gt; $data["description-en"],
    ));

    $success = craft()-&gt;entries-&gt;saveEntry($entry);

    if ($success) 
    {
        ImportPlugin::log('Successfully saved entry "'.$entry-&gt;id.'"', LogLevel::Info);

        $entryEs = craft()-&gt;entries-&gt;getEntryById($entry-&gt;id, "es");
        $entryEs-&gt;getContent()-&gt;setAttributes(array(
            'description'   =&gt; $data["description-es"],
        ));

        // **** The following doesn't work for some reason ****

        craft()-&gt;entries-&gt;saveEntry($entryEs);
    };
}
</code></pre>

<p>The primary entry saves fine and if I dump vars I can see that both <code>$entry.id</code> and <code>$entryEs</code> exist and are formatted as expected — but it doesn't save.</p>

<p>Is it perhaps being overwritten (or ignored) as the content from the primary locale is being copied to the other locales in the background? What's the recommended way to save multiple locales?</p>

CC BY-SA 3.0
2015-02-20T05:50:29.250Z
Not sure I have an answer but it looks like you are setting the locale to a hard coded 'en' in this line: `$entry->locale = "en";` and you are trying to retrieve an entry by asking for the id 'es' in this line `$entryEs = craft()->entries->getEntryById($entry->id, "es");`
Ben Parizek
CC BY-SA 3.0
2015-02-20T06:48:02.660Z
Thanks Ben. Yes.. confusing I know, but that was the point. I first create the entry in english, which generates an entry in all 3 locales. I then retrieve the spanish version (entryEs); set one of the Spanish fields; and save — but no luck. I can even confirm that entryEs exists, and that the content is set, but it just doesn't save. Very odd. I ended up doing it in two steps using a temporary field on the english version, that I then later copied over to the Spanish version in a second pass. Works, but not ideal.
Douglas McDonald
<plugin-development><localization><entry><entrymodel>
Save multiple locale data via plugin
# Save multiple locale data via plugin

(Asked by: username_0 on 2015-02-02)

I'm trying to import data from another database using locales, but am having trouble saving the other locale after the primary locale is saved.
foreach($row as $data) {

    $entry = new EntryModel();
    $entry->sectionId   = 1;
    $entry->typeId      = 1;
    $entry->authorId    = 1;
    $entry->enabled     = true;
    $entry->locale      = "en";

    $entry->getContent()->setAttributes(array(
        'description' => $data["description-en"],
    ));

    $success = craft()->entries->saveEntry($entry);

    if ($success) 
    {
        ImportPlugin::log('Successfully saved entry "'.$entry->id.'"', LogLevel::Info);

        $entryEs = craft()->entries->getEntryById($entry->id, "es");
        $entryEs->getContent()->setAttributes(array(
            'description'   => $data["description-es"],
        ));

        // **** The following doesn't work for some reason ****

        craft()->entries->saveEntry($entryEs);
    };
}

The primary entry saves fine and if I dump vars I can see that both $entry.id and $entryEs exist and are formatted as expected — but it doesn't save.
Is it perhaps being overwritten (or ignored) as the content from the primary locale is being copied to the other locales in the background? What's the recommended way to save multiple locales?

**Tags:** plugin-development, localization, entry, entrymodel

> **Comments:**
> **username_1**: Not sure I have an answer but it looks like you are setting the locale to a hard coded 'en' in this line: `$entry->locale = "en";` and you are trying to retrieve an entry by asking for the id 'es' in this line `$entryEs = craft()->entries->getEntryById($entry->id, "es");`  
> **username_0**: Thanks Ben. Yes.. confusing I know, but that was the point. I first create the entry in english, which generates an entry in all 3 locales. I then retrieve the spanish version (entryEs); set one of the Spanish fields; and save — but no luck. I can even confirm that entryEs exists, and that the content is set, but it just doesn't save. Very odd. I ended up doing it in two steps using a temporary field on the english version, that I then later copied over to the Spanish version in a second pass. Works, but not ideal.  

---

## 1 answers

### Answer by username_2 on 2015-03-05. Score: 2 | Accepted answer

I just tried this and it works with Craft 2.3.2636. The only difference is this isn't using ContentModel::setAttributes(), but I wouldn't have thought that would affect it.
$entry = new EntryModel();
$entry->sectionId   = 2;
$entry->typeId      = 2;
$entry->authorId    = 1;
$entry->enabled     = true;
$entry->locale      = "en";

$entry->getContent()->title       = "English Title";
$entry->getContent()->description = "English description.";

if (craft()->entries->saveEntry($entry))
{
    $entryEs = craft()->entries->getEntryById($entry->id, "es");

    $entryEs->getContent()->title       = "Spanish Title";
    $entryEs->getContent()->description = "Spanish description.";

    craft()->entries->saveEntry($entryEs);
}

> **Comments:**
> **username_0**: Thanks Mike. I'm unable to test this at the moment, but I'll take your word for it and mark as correct. Thanks.  
> **username_3**: You may also want to use `$entryEs->localeEnabled = true;` to make sure the entry is enabled in that language  

---

