Thursday, June 26, 2014

How to Catch i18n Errors Early?

Internationalization (often abbreviated i18n) is the process of making an application localizable for users in different locales, while localization (often abbreviated L10n) is the process of actually localizing an application for a particular locale. In today’s globally connected world, it is rare that an application does not need to support users in many different locales.

Pseudolocalization is the automated generation of fake translations of a program's localizable messages. Using the program in a pseudolocale generated in this manner facilitates finding bugs and weaknesses in the program's internationalization. 

Google has been using this technique internally for some time and has used this tool for improving GMail, Google+ and many more. Google has now released an open-source Java library to provide this functionality at http://code.google.com/p/pseudolocalization-tool/.

Make your customers happy by better supporting i18n using Pseudolocalization tool.

Wednesday, June 18, 2014

Java 8 and Data Parallelism

By Definition, Parallelism reduces the runtime of a specific task by breaking it down into smaller components and performing them in parallel. Since we are already in Big Data age, Data Parallelism is going to play a big role. To achieve something meaningful from Big Data, we need to analyse a lot of data coming from different data sources. We need different algorithms that can process data faster and can consume the power of all computation units. Data Parallelism is one of those techniques which can analyse a lot of data quickly.

Let’s first define the term ‘Data Parallelism’. Data Parallelism means splitting up the data and assigning a single processing unit to each chunk of data. This works really well if you want to perform the similar operation on a lot of data.

What does Java provide to realize ‘Data Parallelism’?

Java 8 has come up with a lot of new libraries and “streams” library is one of them. Making an operation execute in parallel using the streams library is a matter of changing a single method call. You can execute streams in serial or in parallel. When a stream executes in parallel, the Java runtime partitions the stream into multiple substreams. Aggregate operations iterate over and process these substreams in parallel and then combine the results.

Streaming can be realized in two ways:
·        If you already have a Stream object, then you can call its parallel() method in order to make it parallel.
·        If you’re creating a Stream from a Collection, you can call the parallelStream method in order to create a parallel stream.

Following example calculates the total orders of a sequence of products. It transforms each product into its component orders, then gets into the price of each product, and then sums them:

public int getTotalPrice() {
return products.stream()
.flatMap(Product::getOrders)
.mapToInt(Order::getPrice)
.sum();
}

We can perform this operation in parallel by using parallelStream method:

public int getTotalPrice() {
return products.parallelStream()
.flatMap(Product::getOrders)
.mapToInt(Order::getPrice)
.sum();
}

Monday, June 16, 2014

How to Experiment with JavaScript Code inside your Browser?

Are you a JavaScript developer? Have you faced issues like how to readily test your JavaScript code? 

There are couple of things that can be done to solve this issue:

1. Use some online tool like Codepen
2. Use some editors

Using the first option requires to have always an internet connection and second option is slow. What if you get the facility offline right in your browser?

Scratchpad is such a tool available in Firefox browser. 

  • It provides an environment for experimenting with JavaScript code. 
  • You can write and test code ideas that interact with the contents of the web page, before switching to your usual development tools to finalize and clean up the end result.
To open the Scratchpad window, press Shift+F4, or go to the Web Developer menu (which is a submenu in the Tools menu on Mac OSX and Linux), then select Scratchpad. This will open up a Scratchpad editor window, which includes a comment that provides some brief information about how to use Scratchpad. From there you can immediately start writing some JavaScript code to try.

Tuesday, June 10, 2014

Creating an Embedded Video Streaming Server in Android

Different video streaming server provides different capabilities for streaming videos. What to do if you want to stream something inside your Android application.

Here is how you can create a simple streaming server:

NanoHttpd is an open-source, small-footprint web server that is suitable for embedding in Java applications. The code consists of single Java application file which can be used as a 
streaming server. Following is how to create using Android code:


public class VideoStreamingServer extends NanoHTTPD {

    public VideoStreamingServer() {
         // by default listening on port 8080
         super(8080);
    }

    @Override
    public Response serve(String URI, Method method,
        Map header, Map parameters,
        Map files) {

    FileInputStream fis = null;
    try {
        fis = new FileInputStream(EXTERNAL_STORAGE_FILE_LOCATION);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return new NanoHTTPD.Response(Status.OK, "video/mp4", fis);
  }
}


What happens when we try to use String.trim in IE 8?

Problem: 

var tempString = " my string ";

//in modern browsers
console.log(tempString ); //outputs " my string "
console.log(tempString .trim()); //outputs "my string"

//in IE 8
console.log(tempString.trim()); //error: Object doesn't support property or method\
'trim'

Solution:
We can use a better trim function which works in every browser:

Taken from JQuery.js

// Make sure we trim BOM and NBSP (here's looking at you, Safari 5.0 and IE)
var trimPattern = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g
var myTrim = function( text ) {
return text == null ? "" : ( text + "" ).replace( trimPattern, "" );
}
console.log(myTrim(tempString));

This should work in every browser including IE8 as well.

How to create local modules in JavaScript

The module pattern can be used to make your function private and save it from global effects. JavaScript does not give any error even if you override the functions.

For example, if you have a global function like

function global() {
     console.log('I am global function.');
}

and now i create a new module function and override global function like this:

function newModule() {
     global = function() {
          console.log('I am new global.');
    }
}

If you run the above functions by calling:

global()
newModule()
global()

The output will be:

I am global function.
I am new global.

You can use module pattern to stop global overriding.

var module = {};

(function(exports) {
    exports.local = function() {
        console.log('I am local not global.');
    }
}(module));

Now you can call local function using:

module.local()

which will print:

I am local not global.