Search code examples
phpconcrete5concrete5-8.x

How to get the default locale on early stage


On c5.7 the config variable concrete.multilingual.default_locale stored the default locale of an concrete5 installation. On c5-8 we can not access this variable anymore. How can I get this value on new c5-8 installations?


Solution

  • Since concrete5 provides multiple sites on the same installation, every site needs a specific default language. Therefore the default locale config variable has no meaning anymore and the default locale is stored in the db table sites.

    Access the default locale of all defined sites:

    $defaultLocales = [];
    $sl = new \Concrete\Core\Site\SiteList();
    $sites = $sl->get();
    foreach ($sites as $site) {
        $defaultLocales[$site->getSiteID()] = $site->getDefaultLocale();
    }
    

    The following will show, how to get this value for the first site found with backwards compatibility for c5.7.

    $defaultLocale = Config::get('concrete.multilingual.default_locale');
    if (!$defaultLocale) {
        if (class_exists('\Concrete\Core\Site\SiteList')) {
            $sl = new \Concrete\Core\Site\SiteList();
            $sites = $sl->get();
            foreach ($sites as $site) {
                $defaultLocale = $site->getDefaultLocale();
                break;
            }
        }
    }
    

    Src: concrete/src/Entity/Site/Site