Tuesday, June 25, 2013

Localization of HTML Elements

When creating a SPA (Single Page Application), many times we like it to be localization-ready,
meaning it can be easily translated to other languages. 
Without getting into the many aspects of localization (and sometimes internationalization), 
I would like to present a simple way to localize HTML elements using HTML5 "data-" attribute.

The idea is to add a key attribute to the HTML element that we want to localize. 
For example:

<span data-key="txt1"></span>

We then create a JSON file holding all the application text as key-value pairs. For example:

{
"txt1": "translated text 1",
"txt2": "translated text 2",
"txt3": "translated text 3"
}

For each language we create a separate JSON file.
When the application loads, we use jQuery to load and parse the JSON file.
Then we access each HTML element by its data-key and set its HTML to the corresponding value from the JSON data file:

  var appNS;
  (function (appNS) {
      appNS.initApp = function(){
      // load language file
      $.getJSON('en_us.json', function(data) {
        // json loaded - save to object
        appNS.Lang = data;
        for (var key in appNS.Lang)
          $('[data-key="'+key+'"]').html(appNS.Lang[key]);
        });
      }
  } (appNS = appNS || {}));
  
  $(document).ready(function () {
    appNS.initApp();
  });

In this example, I've also used a name space and a JavaScript module.
A working demo can be seen in this plnkr.


Tuesday, June 11, 2013

JavaScript Name Spaces and Modules

An important concept of modular JavaScript is name spaces usage. 
Since any variable declared in the code automatically becomes a global variable residing on the main window object, 
accidental overriding of variables could occur. Further more, code maintenance and modular components are hard to implement. 
The solution is using name spaces and JavaScript modules. Nested modules can also be easily created.
  • Name spaces are implemented using JavaScript objects.
  • Modules are achieved using JavaScript immediate functions. 
Here is a simple example:

var myAppNS;
(function (myAppNS) {
   myAppNS.prop = value;
   myAppNS.func = function() { 
      // do some stuff...
   };
} (myAppNS = myAppNS || {}));

Check out this jsfiddle to see an example.

For nested modules and nested name spaces - check out this plnkr.


From Flex to JavaScript - "The Big Switcheroo"!

So, it took me a year. 
As the rich-media world made a giant leap (or fall, depends on your point of view) from Flex / Flash back to good old JavaScript, I didn't want to be left behind. 
I've spend the last year learning new JavaScript frameworks, HTML5 / CSS3 features, new UI libraries etc. 
My future posts will probably target these new technologies. 
Bye-bye Flash, hello JavaScript!

Monday, August 29, 2011

Extending MXML Components with MXML

I've recently needed to create an MXML component that enables users to add MXML
content into it. The solution is simple yet powerful and provides a way to easily create
extendable MXML components.
In the example below, the component holds a ButtonBar and a ViewStack.
The ViewStack itself is a property of the component that can be added in MXML
when using the component. Each user can add as many views as he needs and
the ButtonBar displays the views.


Code can be found here.

Monday, July 18, 2011

Enabling / Disabling drop menu items dynamically

Recently, I had to find a way to enable or disable drop down menu items according
to the current view. While searching for an elegant solution, I've stumbled across a
piece of code that changed my approach all together. It seems that you can add data
binding into the XML data itself. While Flex compiles the file, it parses the XML data and
assigns the correct data binding.
Take a look at the example below:


Code can be found here.

Sunday, February 13, 2011

Using Scale9Grid with Flex Image control

When using images with corner radius, we usually want to prevent the corner radius 
from distorting when the image is scaled. This is when Scale9Grid comes to the rescue... 
The implementation is not always clear, so here is a little example of Scale9Grid usage.
The Scale9Grid implementation can also be defined in the CSS style if needed.
Code can be found here.

Wednesday, February 9, 2011

AutoScroll Canvas Component

Recently, I had to come up with a generic component that can scroll anything within it, according
to the mouse location.

This component extends Canvas and has a "Speed" property and a "ScrollToPosition" method.
Check out the example below. Source can be downloaded here.





About Me

My photo
I've been developing Internet and rich-media application for 15 years. In my work I emphasis usability and best user experience as well as sleek design. My main expertise are JavaScript, Flex and Flash.

Followers