Q. When does Magento decide which store view to use, and when is the current locale set?
1. Index.php calls Mage::run()
2. It calls app/mage.php :: run()
3. run function has self::$_app->run(…) line of code which ultimatately calls Mage_Core_Model_App::run() function
4. Mage_Core_Model_App::run() includes $this->_initCurrentStore($scopeCode, $scopeType);
5. _initCurrentStore() :: Loads all websites, group and stores into website, group and store object using _initStores() method. this function checks whether the site is website or store group or store, if it is one these then there it sets current store. If scope is base, then it goes through $this->_checkCookieStore()
6. $this->_checkCookieStore() :: this gets the store type from cookie this->getCookie()->get(Mage_Core_Model_Store::COOKIE_NAME);, there it checks whether it is website, group or store and set current store in $this->_currentStore = $store; on the base of value returned by cookie.
7. Mage_Core_Model_App::_checkGetStore() , this checks current store with the xpath_of_store_url , updates cookie
FINALLY, Current locale is set when Mage_Core_Model_App::init() is called , init() function has $this->_initEnvironment(); which has a setting of locale