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 (RivetyCore_Registry::get('enable_localization') == '1'
			 && !is_null($module_name) && trim($module_name) != ""
			 && !is_null($key) && trim($key) != "") {
				$locale_code = RivetyCore_Translate::cleanZendLocaleCode($locale_code);

				// TODO: account for core rivety module
				$path_to_csv = RivetyCore_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 (RivetyCore_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->_rivety_plugin = RivetyCore_Plugin::getInstance();
		$out_url = $url;

		// LOCALIZATION
		if (RivetyCore_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 (RivetyCore_Translate::validateLocaleCode($locale_code)) {
						$out_url = "/".$locale_code.$url;
					}
				}
			} else {
				// TODO - add other cases, such as absolute and relative URLs
			}
		}

		// SSL
		if (RivetyCore_Registry::get('enable_ssl') == '1') {
			$secure_urls = RivetyCore_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://', RivetyCore_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 = RivetyCore_Registry::get('site_url').$out_url;
			} else {
				$out_url = RivetyCore_Registry::get('site_url').'/'.$out_url;
			}
		}
		$params = array('url' => $out_url);
		$params = $this->_rivety_plugin->doFilter('url_post_filter', $params); // FILTER HOOK
		return $params['url'];
	}
Example #3
0
function smarty_block_t($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'] && RivetyCore_Registry::get('enable_admin_localization') == '0') {
				$do_translation = false;
			}
			if ($params['replace']) {
				return RivetyCore_Translate::translate($tpl_vars['locale_code'], "default", $content, $params['replace'], $do_translation);
			} else {
				return RivetyCore_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) && RivetyCore_Translate::validateLocaleCode($locale_code)) {
				$urlparams['locale_code'] = strtolower($locale_code);
			}
			$url_filter = new RivetyCore_Url_Filter();
			return $url_filter->filter($content, $urlparams);
		}
	}
}
Example #5
0
	protected function _T($key, $replace = null)
	{
		// we're not actually doing the translation in the installer. we may some day.
		return RivetyCore_Translate::translate(null, "default", $key, $replace, false);
	}
Example #6
0
	function setcookieAction() {
		// TODO maybe? - prevent people from viewing this page if localization is not enabled
		$request = new RivetyCore_Request($this->getRequest());
		if ($request->has("code") && $request->code != "") {
			$locale_code = $request->code;
			$time = RivetyCore_Registry::get('locale_cache_lifetime');
			if (RivetyCore_Translate::validateLocaleCode($locale_code)) {
				setcookie("locale_code", $locale_code, time() + $time , "/");
				if ($request->has("return_url")) {
					$url_filter = new RivetyCore_Url_Filter();
					header("Location: ".$url_filter->filter($request->return_url, array('locale_code' => $locale_code)));
				} else {
					header("Location: /".$locale_code);
				}
			}
		} else {
			$this->_redirect("/default/locale/choose/");
		}
	}
Example #7
0
	protected function _T($key, $replace = null)
	{
		return RivetyCore_Translate::translate($this->locale_code, 'default', $key, $replace);
	}