<?php
/**
 * Plugin to handle language shifts
 */
class Fiktiv_Controller_Plugin_Language extends Zend_Controller_Plugin_Abstract
{
    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
        $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
        $translate = $bootstrap->getResource('translate');

        $lang = $request->getParam('lang');

        // Set language.
        $router = $bootstrap->getResource('frontcontroller')->getRouter();
        $router->setGlobalParam('lang', $lang);

        // If lang is not supplied we use default.
        if (is_null($lang) || !in_array($lang, array('sv', 'en'))) {

            $config = $bootstrap->getApplication()->getOption('defaults');
            $lang = $config['lang'];

            //$baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
            //$url = $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('url');
            $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');

            $redirector->gotoUrlAndExit('/'.$lang);

        }

        if ($translate->isAvailable($lang)) {
            $translate->setLocale($lang);
        }

        // Set database locale
        $lookup = array(
            'sv' => 'sv_SE',
            'en' => 'en_US'
        );

        Zend_Registry::set('Zend_Locale', new Zend_Locale($lookup[$lang]));

        $database = $bootstrap->getResource('database');
        $database->query("SET lc_time_names = ?;", $lookup[$lang]);
    }

    protected function test()
    {
        $pos = strpos($mystring, $findme);


    }
}