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:
- First you should include Google API JavaScript source in your page:
[cc lang='html4strict' line_numbers='false'][/cc] - 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.
How to enable your iPhone voicemail for Iran MCI subscribers
If you live in Iran and are MCI carrier subscriber and also you have an iPhone, you might ask yourself how to enable voicemail on your iPhone. I give you some quick steps to enable voicemail on your iPhone:
- Click on Phone icon and then Keypad and dial *5005*86*09912#
- Press call and wait.
- Now you can access your voicemail box by tapping Voicemail in Phone.
- To enable forwarding calls in various situations go to step 1 and dial these commands instead:
- Forwarding all calls to voicemail: *21*09912#
- Forwarding calls when your phone is busy: *67*09912#
- Forwarding calls when no reply from you: *61*09912#
- Forwarding calls when your phone is unreachable: *62*09912#
Please note that voicemail service should have been already activated on your account. It’s not activated by default when you purchase your SIM Card.
How to receive BNO breaking news alerts for free on your iPhone via Push Notification
Right after Apple upgraded its iPhone OS to v.3.0 and introduced new Push Notification feature many companies and developers started to write new applications or update their existing apps to use this new cool feature. One of them was BNO News which sends out breaking news alerts in four different ways: 1- Twitter, 2- Email, 3- Friendfeed, 4- iPhone. First three services are free but the last one is not free. You have to pay for their application as well as a monthly subscription fee to receive alerts on your iPhone. I was thinking to find a way to receive these alerts for free and I find a tricky way that I want to share with you here. You have to follow these steps to enable BNO alerts on your iPhone without paying them a penny!
- Download and install TextFree Lite on your iPhone. It’s free.
- Create a TextFree account which gives you a unique @textfree.us email address to receive messages.
- Create a new GMail account at http://mail.google.com/mail/signup
- Go to BNO mail subscription page and subscribe using your new GMail address.
- Login to your new GMail account and wait until BNO confirms your subscription then go to GMail Settings > Forwarding and POP/IMAP
Now put your textfree.us email address in the text box labeled with “email address” (see above) and save your settings. - You’re done! Each time BNO sends an alert to your GMail address it is automatically forwarded to your textfree.us and therefore you receive a Push Notification alert on your iPhone!
Do you know why we used GMail account in the middle of our trick and didn’t use textfree.us address directly? BNO rejects subscription requests from all @textfree.us or similar services email addresses!
Hope you enjoy this trick ;)
