<?php
	FNTH_import( "memcached" );

	function fo3_add_global_translation_needed( $s , $retval ) {	
		if (!isset( $GLOBALS["translation_needed"] ))
			$GLOBALS["translation_needed"]=array();
		$GLOBALS["translation_needed"][$s]=$retval;
	}

	function fo3_load_langdefs() {
		$retval = array();
		$query = sprintf( "select * from ldef_defs" );
		$ds = FNTH_db_get_resultset( $query );
		foreach ($ds as $row) {
			$langcode = $GLOBALS["backlang"][$row["site_language_id"]];
			if (!isset( $retval[$langcode] )) $retval[$langcode]=array();
			$retval[$langcode][$row["ldef_name"]]=$row["def"];
		}
		return $retval;
	}

	function fo3_global_gettext_function( $s , $plural = false , $count = 1 , $language = "" , $escape = "" ) {
		if (empty( $language )) $language = $GLOBALS["language"];

		$need_to_load = false;
		if (isset( $_SESSION["langdefs"] )) unset( $_SESSION["langdefs"] );
		if (isset( $_SESSION["langdefs_expire"] )) unset( $_SESSION["langdefs_expire"] );

		if (!isset( $GLOBALS["fo3_langdefs"] )) {
			$GLOBALS["fo3_langdefs"] = FNTH_memcached_get( "fo3_langdefs" );
			if (!$GLOBALS["fo3_langdefs"] ) {
				$GLOBALS["fo3_langdefs"] = fo3_load_langdefs();
				FNTH_memcached_set( "fo3_langdefs" , $GLOBALS["fo3_langdefs"] );
			}
		}

		/*

		if (!isset( $_SESSION["langdefs"] )) $need_to_load = true;
		if (!isset( $_SESSION["langdefs_expire"] )) $need_to_load = true;
		if (isset( $_SESSION["langdefs_expire"] ) && $_SESSION["langdefs_expire"]<fo3_time()-180) $need_to_load = true;
		if ($need_to_load) {
			$_SESSION["langdefs"] = fo3_load_langdefs(); // parse_ini_file( $GLOBALS["site_root"]."/lang.ini" , true );
			$_SESSION["langdefs_expire"]=fo3_time();
		}
		*/

		if (!isset( $GLOBALS["fo3_langdefs"][$language][$s] ))
			$retval = "@@@".$s."@@@";
		else
			$retval = $GLOBALS["fo3_langdefs"][$language][$s];
		if (fo3_get_language_editor_mode( true )) {
			switch ($escape) {
			case "":
			default:
				$url=sprintf( "/admin/langdefs/edit.php?mode=modify&amp;uid=%s",$s );
				return sprintf( "<a target=\"langdefedit\" class=\"langdef_edit\" href=\"%s\">%s</a>" ,$url , $retval );
				break;
			case "js":
			case "attr":
				fo3_add_global_translation_needed( $s , $retval );
				break;
			}
		}
		return $retval;
	}

	function fo3_global_escapetext_function( $escape ) {
		if (fo3_get_language_editor_mode( true )) return "noescape";	
		return $escape;
	}

	FNTH_set_gettext_function_callback( "fo3_global_gettext_function" );
	FNTH_set_escapetext_function_callback( "fo3_global_escapetext_function" );


	function fo3_add_js_langdef( $name ) {
		if (!isset( $GLOBALS["fo3_js_langdefs"] )) $GLOBALS["fo3_js_langdefs"]=array();
		$GLOBALS["fo3_js_langdefs"][$name]=__($name,"js");
	}

	function fo3_render_js_langdefs() {
		if (!isset( $GLOBALS["fo3_js_langdefs"] )) return false;
		$retval = "";
		$retval.="\naJSLangDefs = new Object();\n";
		foreach ($GLOBALS["fo3_js_langdefs"] as $k => $v)
			$retval.=sprintf( "aJSLangDefs.%s='%s';\n" , $k , $v );
		return $retval;
	}

	function __( $s , $escape = "" , $language = "" ) {
		return fo3_global_gettext_function( $s , false , 1 , $language , $escape );
	}

?>
