Example #1
0
 static function translate($locale_code, $module_name, $key, $replace = null, $do_translation = true)
 {
     // DON'T EVER LEAVE THIS UNCOMMENTED
     // ob_clean();
     // can be useful for debugging since using dd() will dump out into the existing markup and be hard to see
     // but this clears out all the other markup so the debug data can be seen clearly
     $translation = $key;
     if ($do_translation) {
         if (Bolts_Registry::get('enable_localization') == '1' && !is_null($module_name) && trim($module_name) != "" && !is_null($key) && trim($key) != "") {
             $locale_code = Bolts_Translate::cleanZendLocaleCode($locale_code);
             $path_to_csv = Bolts_Registry::get('basepath') . "/modules/" . $module_name . "/languages/" . $locale_code . ".csv";
             if (file_exists($path_to_csv)) {
                 try {
                     $translate = new Zend_Translate("csv", $path_to_csv, $locale_code, array('delimiter' => ","));
                     $translation = $translate->_($key);
                     // this next bit will populate the locale file with untranslated terms
                     // so it's easier for someone to go through and translate them
                     if (Bolts_Registry::get('auto_populate_language_files') == '1') {
                         if (!$translate->isTranslated($key, true, $locale_code)) {
                             $key_no_quotes = str_replace('"', '"', $key);
                             $str = '"' . $key_no_quotes . '","' . $key_no_quotes . '"' . "\n";
                             file_put_contents($path_to_csv, $str, FILE_APPEND);
                         }
                     }
                 } catch (Exception $e) {
                     $translation = $key;
                 }
             } else {
                 // create the file
                 file_put_contents($path_to_csv, $key . ',' . $key);
             }
         }
     }
     $output = "";
     if (is_null($replace)) {
         // no replace, no sprintf
         $output = $translation;
     } else {
         if (is_array($replace)) {
             if (count($replace) > 1) {
                 // there are multiple indices, use vsprintf
                 $output = vsprintf($translation, $replace);
             } else {
                 // there's only one index, use the cheaper sprintf instead
                 $output = sprintf($translation, $replace[0]);
             }
         } else {
             // $replace is not an array, so try using it straight
             $output = sprintf($translation, $replace);
         }
     }
     return $output;
 }
Example #2
0
 function filter($url, array $params = null)
 {
     $this->_Bolts_plugin = Bolts_Plugin::getInstance();
     $out_url = $url;
     // LOCALIZATION
     if (Bolts_Registry::get('enable_localization') == '1') {
         if (stripos($url, "/") === 0) {
             // for virtual URLs
             $locale_code = $params['locale_code'];
             if (!is_null($locale_code)) {
                 // localize the URL by injecting the locale code at the root
                 if (Bolts_Translate::validateLocaleCode($locale_code)) {
                     $out_url = "/" . $locale_code . $url;
                 }
             }
         } else {
             // TODO - add other cases, such as absolute and relative URLs
         }
     }
     // SSL
     if (Bolts_Registry::get('enable_ssl') == '1') {
         $secure_urls = Bolts_Registry::get('secure_urls');
         if (!empty($secure_urls)) {
             $secure_urls = explode('|', $secure_urls);
             if (stripos($url, "/") === 0) {
                 // for virtual URLs
                 // $tmp_url = '/'.trim('/', $url); // get rid of the last slash
                 if (in_array($url, $secure_urls)) {
                     $out_url = str_replace('http://', 'https://', Bolts_Registry::get('site_url') . $out_url);
                 }
             } else {
                 // TODO - add other cases, such as absolute and relative URLs
             }
         }
     }
     // FORCE ABSOLUTE URL
     if (array_key_exists('absolute', $params) || in_array('absolute', $params)) {
         if (stripos($url, "/") === 0) {
             $out_url = Bolts_Registry::get('site_url') . $out_url;
         } else {
             $out_url = Bolts_Registry::get('site_url') . '/' . $out_url;
         }
     }
     $params = array('url' => $out_url);
     $params = $this->_Bolts_plugin->doFilter('url_post_filter', $params);
     // FILTER HOOK
     return $params['url'];
 }
Example #3
0
function smarty_block_translate($params, $content, $smarty, $repeat)
{
    $tpl_vars = $smarty->_tpl_vars;
    // only output on the closing tag
    if (!$repeat) {
        if (isset($content)) {
            $do_translation = true;
            if ($smarty->_tpl_vars['isAdminController'] && Bolts_Registry::get('enable_admin_localization', 'default') == '0') {
                $do_translation = false;
            }
            if ($params['replace']) {
                return Bolts_Translate::translate($tpl_vars['locale_code'], "default", $content, $params['replace'], $do_translation);
            } else {
                return Bolts_Translate::translate($tpl_vars['locale_code'], "default", $content, null, $do_translation);
            }
        }
    }
}
Example #4
0
function smarty_block_url($params, $content, $smarty, $repeat)
{
    $tpl_vars = $smarty->_tpl_vars;
    // only output on the closing tag
    if (!$repeat) {
        if (isset($content)) {
            $urlparams = array();
            $locale_code = $tpl_vars['locale_code'];
            $request_locale = $tpl_vars['request_locale'];
            if ($request_locale && $locale_code != $request_locale) {
                $urlparams['locale_code'] = strtolower($request_locale);
            } elseif (!is_null($locale_code) && Bolts_Translate::validateLocaleCode($locale_code)) {
                $urlparams['locale_code'] = strtolower($locale_code);
            }
            $url_filter = new Bolts_Url_Filter();
            return $url_filter->filter($content, $urlparams);
        }
    }
}
Example #5
0
 function setcookieAction()
 {
     // TODO maybe? - prevent people from viewing this page if localization is not enabled
     $request = new Bolts_Request($this->getRequest());
     if ($request->has("code") && $request->code != "") {
         $locale_code = $request->code;
         $time = Bolts_Registry::get('locale_cache_lifetime');
         if (Bolts_Translate::validateLocaleCode($locale_code)) {
             setcookie("locale_code", $locale_code, time() + $time, "/");
             if ($request->has("return_url")) {
                 $url_filter = new Bolts_Url_Filter();
                 header("Location: " . $url_filter->filter($request->return_url, array('locale_code' => $locale_code)));
             } else {
                 header("Location: /" . $locale_code);
             }
         }
     } else {
         $this->_redirect("/bolts/locale/choose/");
     }
 }
Example #6
0
 protected function _T($key, $replace = null)
 {
     return Bolts_Translate::translate($this->locale_code, 'default', $key, $replace);
 }
Example #7
0
 protected function _T($key, $replace = null)
 {
     // we're not actually doing the translation in the installer. we may some day.
     return Bolts_Translate::translate(null, "default", $key, $replace, false);
 }