Mahdi Taghizadeh I'm who I'm!

5Jul/115

Sencha Touch HTML5 Project Template for Visual Studio 2010

Sencha Touch is a powerful JavaScript framework for mobile which is based on HTML5 and CSS3 and let you create eye catching web applications for popular mobile platforms like Android, iOS and BlackBerry.

If you’re a Microsoft developer, most likely you prefer to work in your favorite IDE: Visual Studio. In order to create a Sencha Touch powered website in VS you can create an empty website, remove web.config, add necessary JavaScript and CSS files and create an HTML5 file to begin. This is easy but the better way you can use a pre-built project template to create such project in seconds. Today I created the same project and exported it as a Visual Studio Template which you can include in your templates. Let’s see how we can do this:

  1. Download Sencha Touch Project Template.
  2. Copy downloaded zip file to C:\Users\{USERNAME}\Documents\Visual Studio 2010\Templates\ProjectTemplates (I suggest you rename the file to Sencha Touch Project.zip after copying)
  3. Open Visual Studio.
  4. Select “File > New Website…” and then “Sencha Touch Project

2011-07-05_1640

Ok! You’re done. Now you have a project with a “content” folder containing required CSS and JavaScript files to start with Sencha Touch Framework as well as a sample HTML5ified “index.html” to begin your work.

19Nov/095

Translating your content title using Google Translate API to use in a URL

Following my previous posts on How to handle non-English characters in ASP.NET MVC routes, Build pretty clean URL for your dynamic pages using JavaScript and Non English Words in URL I decided to share another simple tip that let you translate what your user inserts as content’s title (that can be a blog post title, a page name or a news headline) automatically using Google Translate API.

Let me explain it with an example. Suppose that you have a news system in Persian language and your end user enters a Persian phrase like “این یک عنوان خبر است” as the headline of a news story. From SEO point of view it’s better we include original headline words in URL (like something that I explained in this post) instead of using an integer ID or GUID (e.g. /news/?id=1 or /news/1.aspx, etc.) but as we discussed on in that post, including non-English characters in URLs is not the best way! I just wanted to have something like /news/story/this-is-a-title and didn’t want to force user translate his words into English in order to use in URL field.

A few days ago I was thinking on this topic and decided to try a trick to utilize Google Translate API (which has recently started supporting Persian language as well) to translate such titles into meaningful English phrases and then clean them using the way I described in this post.

Fortunately, thanks to Google API simplicity it was so easy and straightforward. Here’s what I wrote:

  1. First you should include Google API JavaScript source in your page:
    [cc lang='html4strict' line_numbers='false'][/cc]
  2. Add this JavaScript code to your page:
    [cc lang='javascript' line_numbers='false']
    $(document).ready(function() {

    $("#contentPage_Title").blur(function() { parseUrl($("#contentPage_Title").val()) });

    });

    google.load("language", "1");

    function parseUrl(url) {

    $("#loadingImage").attr("style", function() { return "display: inline;" });

    google.language.translate(url, "fa", "en", function(result) {

    if (!result.error) {

    var url = (result.translation + " ").replace(/[^a-zA-Z0-9]+/g, "-");

    $("#contentPage_Url").val(url.slice(0, url.length - 1).toLowerCase());

    $("#loadingImage").attr("style", function() { return "display: none;" });

    }

    });

    }
    [/cc]

You’re done! Now, let me explain the code a little. First using jQuery, you tell the DOM to fire parseUrl() event as soon as you enter the title and go to the next field, then, we load Google Translate service into our script, call translate function to translate from “fa” (Persian) to “en” (English) and finally clean the result using our old trick. That’s all and no magic here!

Please note that we show and hide a “Loading” element (e.g.: a loading image) before and after we call AJAX translate function. So user can see something is happening behind the scene.

kick it on DotNetKicks.com

14Oct/093

Build pretty clean URL for your dynamic pages using JavaScript

Once again I want to talk about URLs and who doesn’t know that I’m a big fan of friendly, clean and pretty URLs in my applications! When you write an application (for example a blog engine) you have multiple choices to create unique URL for a single post; you can include an integer ID (myblog.com/458) or a GUID instead of integer or the best way, a friendly URL like myblog.com/hey-i-am-a-clean-url. This value should be unique and should not be changed if your blog post title is changed; so we should have a separate column in our database table to store this friendly URL. I wrote a simple JavaScript code that receives your blog post title, clean it from non alphanumeric characters and replaces them with a dash, change all characters to lower case and make it ready to be sent to your business logic handler:

<script type="text/javascript" language="javascript">
    function parseUrl(url) {
        var cleanUrl = (url + " ").replace(/[^a-zA-Z0-9]+/g, "-");
        return cleanUrl.slice(0, cleanUrl.length -1).toLowerCase();
</script>

and this two lines of code to fill URL field automatically as user types the post title (using jQuery:

<script type="text/javascript" language="javascript">
    $(document).ready(function() {
        $("#postTitle").keyup(function() { $("#postUrl").val(parseUrl($("#postTitle").val())) }
        );
    });
</script>

 

This is very useful if you’re building applications using ASP.NET MVC and have control over URLs using routing engine. This is also helpful for WebForms developers who use routing in WebForms or rewrite extension less URLs using IIS7, ISAPI_REWRITE and etc.

Let your URLs be more friendly against search engines ;)

kick it on DotNetKicks.com