Multilingual Website Interface on the Fly With jQuery

Applying a multilingual interface on your webpages and allowing the change at the client side without the need for a server postback has never been easier using the technique I’ll describe in this post.
While developing Bloginto, I came across the situation of implementing an English-Arabic-French interface with the possibility to change the language on the fly (on the client side), so I used a trick so close to culture resource files in the .NET.
My first priority was to write the minimum of code using jQuery, and as you will see, it is *really* minimal.

1. Preparing the resources for the language

We will encapsulate the language resources with Javascript arrays, here is an example:
  1. function getLanguageResources() {
  2.     var fr = new Array(); var en = new Array();
  3.  
  4.     fr['settings'] = "paramètres"; en['settings'] = "settings";
  5.     fr['default_feed'] = "Flux par défaut"; en['default_feed'] = "Default feed";
  6.     fr['hidden'] = "Masquer"; en['hidden'] = " Hidden";
  7.     fr['save_settings'] = "Enregistrer les paramètres"; en['save_settings'] = "Save settings";
  8.  
  9.     var resources = new Array();
  10.     resources['fr'] = fr;
  11.     resources['en'] = en;
  12.  
  13.     return resources;
  14. }
The getLanguageResources function returns an array that contains an associative key/value arrays of the desirable languages, notice how the same key is used for the different translations.

2. Preparing the HTML markup

We need to put placeholders for the multilingual text to show in the markup, I choose a <span> tag here, but obviously you can use whatever elements fits best. The trick is to have all these multilingual spans use the same name attribute (this is not a necessity neither, you can use whatever attribute you wish), and have them use another attribute, for example caption which value is the key of the text to show in the resource arrays.
We will also add two buttons to test the language change, here is the final markup :
  1. <input type="radio" id="radioEnglish" name="radio-language" value="en"/><label for="radioEnglish">English</label>
  2. <input type="radio" id="radioFrench" name="radio-language" value="fr"/><label for="radioFrench">Français</label><br/>
  3.  
  4. Text for : settings : <b><span name="lbl" caption="settings"></span></b><br/>
  5. Text for : default_feed : <b><span name="lbl" caption="default_feed"></span></b><br/>
  6. Text for : hidden : <b><span name="lbl" caption="hidden"></span></b><br/>
  7. Text for : save_settings : <b><span name="lbl" caption="save_settings"></span></b><br/>
Notice how the radio buttons for changing the language have values which are the keys of the resource language (the arrays).

3. jQuery magic

Now we will apply some jQuery code to associate the spans with their corresponding text from the language selected by the radio buttons
  1. function changeLanguage(lang) {
  2.     var langResources = getLanguageResources()[lang];
  3.  
  4.     $("span[name='lbl']").each(function (i, elt) {
  5.         $(elt).text(langResources[$(elt).attr("caption")]);
  6.     });
  7. }
  8.  
  9. $(document).ready(function () {
  10.     $("input[name='radio-language']").click(function () {
  11.         changeLanguage($(this).val());
  12.     });
  13. });
Here we attach a click event to the radio buttons, and we simply call the function changeLanguage whenever a button is clicked, we pass the value of the “value” attribute of that button to the function which represents the specified language (‘fr’ or ‘en’)
The changeLanguage function loads the corresponding array of the languages from the getLanguageResources function, then iterate through every element that has the attribute namelbl”, and change its text to the value in the resource language array which key is the caption of that specified element (span), pretty simple!
As you can see, all the magic is done with 2 lines of jQuery, if you guessed that the “.attr()” method will do the same instead of “.each()” then you’ve guessed wrong, attr() applies only to the first element in the selection set. See .attr() specification here.

4. Demo

Her is a working demo http://jsfiddle.net/uUgWD/.

Posted in , |

Bloginto 2.0 is here, with a lot of improvements

Bloginto is a Google Chrome extension that brings feeds from the Digg like websites called Bloginy for both the Algerian http://bloginy.com and the Moroccan http://bloginy.ma

Today I’m pleased to announce that the version 2.0 of the extension is here, you can install it from https://chrome.google.com/extensions/detail/jppmcmbnmodlmgbfdddmeopgagancoak or update it from the settings section in Google Chrome if you have already installed the previous version.

This new version $(‘was’).completely(‘jQuery’, ‘fied’), cleaner and reliable code has been introduced to make sure handling all the error cases by showing pretty informative messages to the user. Enough talking, here are the new features :

settings page

As you can see from the settings page :

  • You can choose the default view, the Algerian or the Moroccan Bloginy, this has been present in the previous versions, but it has an impact on the notifications of the unread and new arriving feeds, you will notified only when feeds from the default feed arrive.
  • The notification themselves notifications: the number shows the unread feeds count, it will refresh automatically and change when you read feeds or new ones become available.
  • Because you can vote on articles directly from the extension now, there is a section where you can save your user name and password for both, Bloginy.com and Bloginy.ma.
  • Timeouts for the request, you should make this bigger if you have slow internet, and the feeds timeout, this is the frequency of checking new articles on the website.
  • While reading feeds, you can mark them as read, and the extension offer you to hide read feeds, or just mark them with a different color.
  • And most of all, it is now multilingual, you can change the language from Arabic to French to English, and it works on the fly (God bless jQuery), if you need to know how this is done, here is the code that does it all :p

    $("span[name='lbl']").each(function(i, elt){
        $(elt).text(l[$(elt).attr("caption")]);
    });
       

    where l is the resources language;

ar settings[12]

The extension itself allows the marking/hiding of the read posts, live voting and twittering the posts directly from the extension. You can toggle hidden posts by clicking on the button at the top. To vote, you simply click on the “like” icon, to twitter… well you click on the twitter icon and to mark a post as read, you click on the description of the post.

Finally, I hope Bloginy will get a little active after making the voting available directly and eventually allowing multiple languages that may match the users tastes.

The code source is available on Google Code at this address : https://code.google.com/p/bloginto-chrome/

$(‘feed > back’).is({always: ‘welcome ;)’}) ;
Ramadan Mubarak for all, and that’s my present for you guys to spend more time “engaged” with the community.

main screenerror notifications

Posted in |

How Does Google Pack Know What Application is Installed On Your Computer

Google pack is a set of software made available free by Google including programs by Google like Chrome and Picasa or other vendors like Mozilla Firefox and Adobe Reader. What is strange about the Google pack is its webpage, where it shows the applications you have installed on your computer and the applications that are not installed already.

The question that pops first is, how does a webpage knows what software is installed on my computer? I decided to see how does it work, inspecting Google’s coming-from-hell javascript files and trying to figure out how things are put together. To be honest, my motivation was first to know if I can use Google’s technique to leverage any information about other installed software on someone’s computer or not, and the answer I simply: no you cant, don’t bother.

Google’s pack webpage http://pack.google.com/ links to a javascript file and have some application parameters initialized at the page loading, with a notable array of guid ‘s and application names:

  1. {'93613D9F-C440-475B-8379-E7B7E37DAAB7':'ci_ar',
  2. '71339EA2-A88C-11DE-8E3D-65F655D89593':'ci_avast',
  3. '8A69D345-D564-463C-AFF1-A69D9E530F96':'ci_chrome',
  4. '74AF07D8-FB8F-4D51-8AC7-927721D56EBB':'ci_earth',
  5. …}

It turns out that Google stores information of the applications in the Windows registry (not exactly the same technique on other systems) at the path HKEY_LOCAL_MACHINE\SOFTWARE\Google\Google Updater\AppData\. My research was mainly about how does the Javascript code figure out how to fetch these information from the registry and if it is possible to make it read information from other locations in the registry.

Using burp proxy to intercept and change the applications guid on the fly in the html page, I didn’t come to any result, and it turns out that the only information that is read from the registry was only from  inside HKEY_LOCAL_MACHINE\SOFTWARE\Google\Google Updater\AppData\.

So the only trick left was to analyze the javascript file http://pack.google.com/cominst.js?2 (go ahead, take a look), if you want to try the code yourself, you can use the console in firebug under firefox or the Developer Tools (Ctrl + Shit + I) under Google chrome.

firebug

The javascript code defines some objects used to manipulate all the operations from reading the list of applications available, to fetching the applications already installed etc…

First a script on the page creates a _CI_Pack object named pack, this pack object have a property named plugin that contains a Plugin object which is the main piece of code we are looking for. This plugin is an embed object installed on the page with the function Plugin.createCIObject that inserts the following code on the page :

  1. <embed id="CIPlugin_14" type="application/x-vnd.google.cominstctrl.14" width="0" height="0">

This plugin then exposes an attribute called ciobj which is the one reading information from the registry.

In fact this plugin preloads all the information from the registry found in HKEY_LOCAL_MACHINE\SOFTWARE\Google\Google Updater\AppData\ in advance, and the guids that are presented in the pack object have no effect on these information, that’s why we can’t inject or make the plugin read other keys outside this path.

You can read the guid of the applications loaded by the pluing using this code :

  1. var b = pack.plugin;
  2. var e = b.ciobj;
  3. alert(e.Applications.Length);
  4. for (i = 0;i<e.Applications.Length; i++)
  5.     alert(e.Applications.ElementAt(i).Id);

How the plugin discover the installed applications :

The plugin object uses a simple method to know if the application is installed or not, installed applications have a version number associated with their corresponding object, if the application is not installed, the version number have the value of null, Google checks if the version number is not null, and hence figures out if the application id already installed or not.

  1. function d(m) {
  2.     if (!m) return false;
  3.     var o = c[m.Id.toUpperCase()];
  4.     if (!o) return false;
  5.     if (b.ci_mimeNum >= 14) { //Here the script check for the installed application
  6.         m = m.Version;
  7.         if (!m) return false
  8.     }
  9.     return i[o] = true
  10. }

Note that every object in pack.plugin.ciobj.Applications have the two properties exposed for javascript which are Id and Version. The script is really complicated due to the shortening in the variable names, and this function is the one responsible for returning the installed apps function getInstalledList(b, c).

And that’s it, maybe when someone else wonders someday how Google Pack fetch the information of installed applications on the visitor’s computer, he won’t hopefully waste 3 or such days trying to figure out how that javascript file works.

Posted in , |

Circular Links in You Twitter/Facebook updates

Say you want to insert a link for your tweet inside that same tweet, or want to link a facebook update inside the update itself. The issue with this is that: first you can’t know what will be the link to your new tweet/update, and second, once you post something on these sites you can’t change it (unlike Google Buzz for example).

I am used to celebrate my tweets which rank is of the form xxxx where 0<x<10 (I expect some technical knowledge for my reader :p), for example my 1111th and 6666th tweet, and usually the form of the tweet was :

wohoo, my xxxxth tweet [and a link to this same tweet here]

The idea was to use a link shortening service and do the following, we can use bit.ly

  • Check if the url http://bit.ly/WHAT_EVER_YOU_WANT_HERE is not already taken, by typing it into the browser and checking that a Page Not Found error occurred which means the suffix WHAT_EVER_YOU_WANT_HERE is available.
  • post your tweet/update with the above link.
  • then take the link of the new tweet/update, shorten it with bit.ly and custom the shortened url using the same suffix of the link above.

And by doing this, you finally have a circular link, targeting the same tweet/update which contains the same link targeting the same… you get the point.

Here is an example of a tweet:

A test tweet with a circular linkhttp://bit.ly/WHAT_EVER_YOU_WANT_HERE

Posted in , |

Bloginto 1.1 is available

After few changes in the “Bloginy Algeria” and “Bloginy Marroc” API, the Bloginto Google Chrome extension was broken and doesn’t show the feeds anymore.
Today I released a new version with some changes, mainly technical changes due to the API change, also added a timeout option for those who have slow internet connections.
The extension is to be broken anytime since Bloginy Marroc is not using the same standard for the feeds as Bloginy Algeria which is pretty stable.
The source code is also updated and available here on Google Code (you are welcome to change it to ameliorate the extension) https://code.google.com/p/bloginto-chrome/source/browse/#svn/trunk.
Finally, to update the extension (it should be automatic) you go to extensions then choose update extensions, if you don’t already have it installed, you can install it from here : https://chrome.google.com/extensions/detail/jppmcmbnmodlmgbfdddmeopgagancoak.

Posted in |

OCaml and Unix programming

If you are used to the Unix programming with C, then almost the same concepts apply to OCaml using the Unix module, with slightly different syntax for function calls and arguments.

It is pretty straight forward if you have already done some system programming in C before, to compile you should add unix.cma to the arguments of ocamlc.

Example 1: fork, getpid and getppid :

  1. open Unix;;
  2. open Printf;;
  3.  
  4. let a = fork () in
  5.   match a with
  6.     | 0 -> printf "child -- pid : %d ,ppid : %d\n" (getpid ()) (getppid ())
  7.     | -1 -> printf "%s" "error accured on fork\n"
  8.     | _ -> printf "parent process : %d\n" (getpid ())
  9. ;;

This example shows how to do a simple fork to create a new process, then each process prints its pId to the console, here we cannot ensure which one will print first, the parent or the child. Here we use the usual function fork, getpid to get the ID of the current process and getppid to get the parent process.

Compile :

  1. sky@sky-laptop:~/Desktop$ ocamlc unix.cma demo.ml -o demo

Output 1:

  1. sky@sky-laptop:~/Desktop$ ./demo
  2. parent process : 3249
  3. child -- pid : 3250 ,ppid : 3249

Output 2:

  1. sky@sky-laptop:~/Desktop$ ./demo
  2. child -- pid : 3250 ,ppid : 3249
  3. parent process : 3249

Example 2, wait:

To ensure that the child process finish before its parent we use the wait system call, which returns the id of the finished process and the status code returned from the process.

  1. open Unix;;
  2. open Printf;;
  3.  
  4. let a = fork () in
  5.   match a with
  6.     | 0 ->  printf "child -- pid : %d ,parent pid : %d\n" (getpid ()) (getppid ());
  7.         exit 13
  8.     | -1 -> printf "%s" "error accured on fork\n"
  9.     | _ -> let childid, returncode = wait () in
  10.         printf "parent process : %d\n" (getpid ());
  11.         printf "child %d closed with status code %d" childid (let WEXITED(code) = returncode in code)
  12. ;;

Output :

  1. sky@sky-laptop:~/Desktop$ ./demo
  2. child -- pid : 3268 ,parent pid : 3267
  3. parent process : 3267
  4. child 3268 closed with status code 13

As you see, the child exits with the code 13, the parent calls wait to wait for its unique child created with fork, then reads the returned code, note that OCaml will generate a warning here because we just check for WEXITED (we know it will exit normally) but we omit WSIGNALED and WSTOPPED. In C you don’t have this flexibility of warnings which can brings you a lot of trouble.

Exemple 3, exec*:

This example demonstrates how to use the exec* calls, these methods replaces the core of the process with the new code of the command to be executed thus the function never returns, when the function fails, an exception is thrown, check the specifications here.

  1. open Unix;;
  2. open Printf;;
  3.  
  4. let a = fork () in
  5.   match a with
  6.     | 0 -> (try
  7.           execvp "ls" [|"ls"; "-l"|]
  8.        with
  9.           _ -> printf "%s" "error while execv\n"; exit (-1))
  10.     | -1 -> printf "%s" "error accured on fork\n"
  11.     | _ -> ignore (wait ()); printf "%s" "parent exit...\n"
  12. ;;

Here we fork and replace the child code with the ls command’s, here is the output

  1. sky@sky-laptop:~/Desktop$ ./demo
  2. total 136
  3. -rwxr-xr-x 1 sky sky 82110 2010-04-20 00:26 demo
  4. -rw-r--r-- 1 sky sky   234 2010-04-20 00:26 demo.cmi
  5. -rw-r--r-- 1 sky sky   743 2010-04-20 00:26 demo.cmo
  6. -rw-r--r-- 1 sky sky   291 2010-04-20 00:26 demo.ml
  7. -rw-r--r-- 1 sky sky   410 2010-04-20 00:26 demo.ml~
  8. parent exit...

Posted in , |

Swedish Greys - a WordPress theme from Nordic Themepark. Converted by LiteThemes.com.