Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Wednesday, June 6, 2012

Logging full objects via console.log in VS 2012

If you call console.log() with an object, Visual Studio 2012 RC will output [Object], nothing more!
But I'm used to seeing its properties and its values, too, like in the Google Chrome Developer Console.

So I wrote my own function:
/**
 * Returns a human readable presentation of an object, its properties and its values.
 * @param obj The object
 * @param attr You can use this parameter in order to specify a prefix for each property
 * @return Returns a string
 *
 * @author ComFreek
 * @license Public Domain
 */
function logObject(obj, attr) {
    var str = "";
    attr = attr || "";
    for (var attrname in obj) {
        if (typeof obj[attrname] == "object") {
            str += "\n" + logObject(obj[attrname], attr + "." + attrname) + "\n";
        }
        else {
            str += attr + "." + attrname + "=" + obj[attrname] + "\n";
        }
    }
    return str;
}
Some sample inputs and outputs:
var obj = {
    "foo": "bar",
    "bar": "foo",

    "fooObj": {
        "barProp": "fooVal",
        "fooProp": "barVal",

        "barUnderObj": {
            "test": "test"
        }
    }
};
console.log(logObject(obj));
This outputs:
.foo=bar
.bar=foo

.fooObj.barProp=fooVal
.fooObj.fooProp=barVal

.fooObj.barUnderObj.test=test
The same code but with second argument given:
console.log( logObject(obj, "fooObj") );
Output:
fooObj.foo=bar
fooObj.bar=foo

fooObj.fooObj.barProp=fooVal
fooObj.fooObj.fooProp=barVal

fooObj.fooObj.barUnderObj.test=test

Tuesday, June 5, 2012

5 steps for i18n in Windows 8 Metro style apps (JS)

  1. Create three folders in your project root:
    |
    --strings
      |
      --en-US
      |
      | 
      --de-DE (or another language and country code)
  2. Then create in your in each of your two subfolders a Resource File (*.resjson):
    |
    --strings
      |
      --en-US
      | |
      | --resources.resjson
      |
      --de-DE (or another language and country code)
        |
        --resources.resjson
    
  3. Enter your strings in JSON format.
  4. i18n in your HTML:
    1. <element data-win-res="{textContent: '[resStringName]'}"></element>
    2. Call WinJS.Resources.processAll() in your JavaScript, after the DOM was loaded, e.g.:
      var app = WinJS.Application;
      // ...
      app.onloaded = function (args) {
        WinJS.Resources.processAll();
      }
      
      
  5. i18n in your JavaScript:
    var str = WinJS.Resources.getString("[resStringName]").value;