Transl8

Advanced full example

As it can be tedious to integrate with the full logic a Zend package, we provide you an exhaustive example.

    protected function _initPluginLiveTranslation()
    {
        //here we rely on the session for activating Transl8
        $session    = new Zend_Session_Namespace('Transl8');
        if (isset($session->onlineTranslationActivated)) {
            $activationMode = $session->onlineTranslationActivated;
        } else {
            $activationMode = true;
        }
        Zend_Registry::set('Transl8_Activated', $activationMode);

        $options    = $this->getOption('transl8');

        if (Zend_Registry::get('Transl8_Activated')) {
            // we get the existing locales from the ini file
            $locales        = array();
            foreach (explode(',', $options['locales']) as $locale) {
                $locales[trim($locale)] = new Zend_Locale($locale);
            }
            Transl8_Form::setLocales($locales);
            //we register the plugin, with the webservices hooks to work with.
            $plugin     = new Transl8_Controller_Plugin_Transl8();
            $plugin->setActionGetFormData($options['action']['getFormData']);
            $plugin->setActionSubmit($options['action']['submit']);

            $front      = Zend_Controller_Front::getInstance();
            $front->registerPlugin($plugin);
        }

        $writerDestinationFolder    = $options['writer']['destination']['folder'];
        Transl8_Translate_Writer_Csv::setDestinationFolder($writerDestinationFolder);
    }

    protected function _initI18n()
    {
        // Get default language from bootstrap config file
        Zend_Locale::setDefault('en_US');

        $session    = new Zend_Session_Namespace('Transl8');
        if (isset($session->currentLocale)) {
            $currentLocale  = $session->currentLocale;
        } else {
            $currentLocale = 'en_US';
        }

        $locale     = new Zend_Locale($currentLocale);

        $inlineTranslationFolder    = Transl8_Translate_Writer_Csv::getDestinationFolder();

        $configTranslation = array(
            'adapter'   => 'Transl8_Translate_Adapter_Csv',
            'scan'      => Zend_Translate::LOCALE_DIRECTORY,
            'content'   => APPLICATION_PATH . '/../data/languages/' . $locale,
            'locale'    => $locale
        );
        $configTranslationDiff = array(
            'adapter'   => 'Transl8_Translate_Adapter_Csv',
            'scan'      => Zend_Translate::LOCALE_DIRECTORY,
            'content'   => $inlineTranslationFolder . '/' . $locale,
            'locale'    => $locale
        );

        $csvTranslate = new Zend_Translate($configTranslation);
        $csvTranslate->addTranslation($configTranslationDiff);

        if (Zend_Registry::get('Transl8_Activated')) {
            foreach (Transl8_Form::getLocales() as $localeToLoad) {
                if ((string)$localeToLoad != (string)$locale) {
                    $configTranslation = array(
                        'adapter'   => 'Transl8_Translate_Adapter_Csv',
                        'scan'      => Zend_Translate::LOCALE_DIRECTORY,
                        'content'   => APPLICATION_PATH . '/../data/languages/' . $localeToLoad,
                        'locale'    => $localeToLoad
                    );
                    $configTranslationDiff = array(
                        'adapter'   => 'Transl8_Translate_Adapter_Csv',
                        'scan'      => Zend_Translate::LOCALE_DIRECTORY,
                        'content'   => $inlineTranslationFolder . '/' . $localeToLoad,
                        'locale'    => $localeToLoad
                    );
                    $csvTranslate->addTranslation($configTranslation);
                    $csvTranslate->addTranslation($configTranslationDiff);
                }
            }
            $csvTranslate->setLocale($locale);
        }

        Zend_Registry::set('Zend_Locale', $locale);
        Zend_Registry::set('Zend_Translate', $csvTranslate);

        date_default_timezone_set('Europe/Paris');
    }