Snowcore
Snowcore

Reputation: 1016

What is the best practice to setup multilanguage store (any sql upgrades)?

It's boring to add store views manually, especially when your store has many languages. Does anyone use sql upgrade script for adding store views and assigning appropriate locale? How can I do this via sql upgrade script?

Upvotes: 3

Views: 1402

Answers (3)

Roman Snitko
Roman Snitko

Reputation: 3655

The full code for adding multiple store views and assigning locale:

$defaultStore = Mage::getModel('core/store')->load(1);
$websiteId = $defaultStore->getWebsiteId();
$store = Mage::getModel('core/store_group')->load($defaultStore->getGroupId());

$storeViewsData = array(
    'es' => array(
        'name'   => 'Spanish',
        'locale' => 'es_ES',
    ),
    'fr' => array(
        'name'   => 'French',
        'locale' => 'fr_FR',
    ),
    'pt' => array(
        'name'   => 'Portuguese',
        'locale' => 'pt_PT',
    ),
    'it' => array(
        'name'   => 'Italian',
        'locale' => 'it_IT',
    )
    );

    foreach ($storeViewsData as $code => $data) {
    $view = Mage::getModel('core/store');
        $view->setData(array(
            'website_id'    => $websiteId,
            'group_id'      => $store->getId(),
            'name'          => $data['name'],
            'code'          => $code,
            'is_active'     => 1
        ));
        $view->save();

        Mage::getConfig()->reinit();
        Mage::app()->reinitStores();


        $groupsValue = array();
        $groupsValue['locale']['fields']['code']['value'] = $data['locale'];

        Mage::getModel('adminhtml/config_data')
            ->setSection('general')
            ->setWebsite('base')
            ->setStore($code)
            ->setGroups($groupsValue)
            ->save();
    }

Upvotes: 2

Roman Snitko
Roman Snitko

Reputation: 3655

You can set locale for your store view in this way:

$groupsValue = array();
$groupsValue['locale']['fields']['code']['value'] = 'uk_UA';

Mage::getModel('adminhtml/config_data')
    ->setSection('general')
    ->setWebsite('base')
    ->setStore($storeViewCode)
    ->setGroups($groupsValue)
    ->save();

Upvotes: 2

Jonathan Day
Jonathan Day

Reputation: 18702

Create yourself a custom module (use Module Creator extension if you wish) then add this code to your mysql4-install-0.1.0.php and modify as appropriate.

$installer = $this;

$installer->startSetup();
// Start with a root category

$root_cat = Mage::getModel("catalog/category");
$root_cat->setData(array(
    "name"              => "Store Name",
    "url_key"           => "root",
    "description"       => "Store Name root category",
    "display_mode"      => Mage_Catalog_Model_Category::DM_PRODUCT,
    "default_sort_by"   => Mage::getModel("catalog/category")->getDefaultSortBy(),
    "available_sort_by" => Mage::getModel("catalog/category")->getDefaultSortBy(),
    "is_active"         => 1,
    "is_anchor"         => 0,
    "include_in_menu"   => 0,
    "parent_id"         => 1,
    "path"              => Mage::getModel("catalog/category")->load(1)->getPath(),
    "attribute_set_id"  => Mage::getModel("catalog/category")->getDefaultAttributeSetId()
));

try {
    $root_cat->save();
} catch (Exception $e) {
    Mage::logException($e->getMessage());
    return;
}

//setup an array of our websites, stores and storeviews
$website_array = array(
    "store_us"  => array(
        "name"          => "Store Name - US",
        "store"         => "Store Name - US Store",
        "store_view"    => array(
            "name"      => "English (US)",
            "code"      => "store_us_en"
        )
    ),
    "store_au"  => array(
        "name"          => "Store Name - AU",
        "store"         => "Store Name - AU Store",
        "store_view"    => array(
            "name"      => "English (AU)",
            "code"      => "store_au_en"
        )
    ),
    "store_de"  => array(
        "name"  => "Store Name - UK",
        "store" => "Store Name - UK Store",
        "store_view"    => array(
            "name"      => "UK",
            "code"      => "store_uk_en"
        )
    ),
);

$i = 1;

foreach ($website_array as $site_code => $site_details) {

    try {

        // First, the website

        $site = Mage::getModel("core/website");
        $site->setData(array(
            "code"          => $site_code,
            "name"          => $site_details["name"],
            "is_default"    => (($i == 1) ? 1 : 0),
            "sort_order"    => $i
        ));
        $site->save();

        // then attach the store to the website

        $store = Mage::getModel("core/store_group");
        $store->setData(array(
            "website_id"        => $site->getId(),
            "root_category_id"  => $root_cat->getId(),
            "name"              => $site_details["store"]
        ));
        $store->save();

        // Set this store as default for the website

        $site->setDefaultGroupId($store->getId());
        $site->save();

        // Finally, the store view

        $view = Mage::getModel("core/store");
        $view->setData(array(
            "website_id"    => $site->getId(),
            "group_id"      => $store->getId(),
            "name"          => $site_details["store_view"]["name"],
            "code"          => $site_details["store_view"]["code"],
            "sort_order"    => $i,
            "is_active"     => 1
        ));
        $view->save();

        // Set this store view as default for the store

        $store->setDefaultStoreId($view->getId());
        $store->save();


    } catch (Exception $e) {
        Mage::logException($e->getMessage());
    }

    $i++;
}

HTH,
JD

Upvotes: 2

Related Questions