Monday, August 31, 2009
jQuery - Center a div or any control
Dependency could be jquery-1.3.2.js
$('#divid').css('top',$(window).height()/2 - $('#divid').height()/2 + $(window).scrollTop());
$('#divid').css('left',$(window).width()/2 - $('#divid').width()/2 + $(window).scrollLeft());
You can use animate to get some effect to the moving operation.
$('#divid').animate({top: $(window).height()/2 - $('#divid').height()/2 + $(window).scrollTop()},500);
$('#divid').animate({left: $(window).width()/2 - $('#divid').width()/2 + $(window).scrollLeft()},500);
Friday, August 28, 2009
Language Translation API ASP.NET
private string translateme(string stringToTranslate, string fromLanguage, string toLanguage)
{
const int bufSizeMax = 65536;
const int bufSizeMin = 8192;
try
{
// by default format? is text.
// so we don't need to send a format? key
string requestUri = "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=" + stringToTranslate + "&langpair=" + fromLanguage + "%7C" + toLanguage;
// execute the request and get the response stream
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
// get the length of the content returned by the request
int length = (int)response.ContentLength;
int bufSize = bufSizeMin;
if (length > bufSize)
bufSize = length > bufSizeMax ? bufSizeMax : length;
// allocate buffer and StringBuilder for reading response
byte[] buf = new byte[bufSize];
StringBuilder sb = new StringBuilder(bufSize);
// read the whole response
while ((length = responseStream.Read(buf, 0, buf.Length)) != 0)
{
sb.Append(Encoding.UTF8.GetString(buf, 0, length));
}
// the format of the response is like this
// {"responseData": {"translatedText":"¿Cómo estás?"}, "responseDetails": null, "responseStatus": 200}
// so now let's clean up the response by manipulating the string
string translatedText = sb.Remove(0, 36).ToString();
return translatedText.Substring(0, translatedText.IndexOf("\"},"));
}
catch
{
return "";
}
}