Browsing articles from "November, 2010"
Nov
11

Managing string localization in javascript files

One thing that is really frustrating to deal with is localization. There is nothing funny about localizing text from your application. But this is a necessary step when you want to go for very a broad market in multiple languages. So while your there, why not having a localization infrastructure in all your front-end.

Most PHP frameworks and JS widget libraries can now be easily customized as far as localization is concerned. However your bound to have some string messages in your javascript modules. Not thinking about the localization of these little pesky messages leads to painful hours of looking through every files to find some strings that have not been localized.

Consolidating your localization architecture

Okay your widget has 2 or 3 files, each for one…


Nov
1

Managing undefined properties on multiple level in objects in javascript

When you’re working with objects you often need to know if a properties already exists in an object, for doing that there is 2 solutions that are already working fairly well. But when you look for undefined properties on multiple levels, it rapidly becomes a mess.

An example

  1. if(typeof object.props1.props2 == "undefined"){
  2. // do stuff
  3. }

If props1 is undefined, unfortunately your browser is going to throw you that object.props1 is undefined.

Rewinding a bit

First, let’s take a look at your options if your only working on one level, let’s say you want to know if prop1 exists in an object

First solution:

  1. if(object.hasOwnProperty("props1") ){
  2.  // Do stuff
  3. }

It is pretty self exploratory, it looks in the object if it’s got the property props1, however it is not going to…