Friday, January 18, 2013

The missing ResWFileCodeGenerator custom tool for Visual Studio 2012

I've been doing quite a lot of Windows Store Apps on Windows 8 lately. Ever since I started I always seemed to miss the ResXFileCodeGenerator custom tool. This is because in WinRT when you want to load a localized string in code then you will have to do something like this:

var resources = new ResourceLoader();
var localizedString = resources.GetString("SomeResourceName");

This of course can easily lead to a lot of errors. In my case I got a little to eager while refactoring and forgot to notice that I was also changing hard coded strings. This broke the code quite a lot. Because of this frustration I decided to implement my own custom tool since Microsoft didn't provide one

The project is open source and is available at CodePlex. Here's a preview of the description which I took directly from my CodePlex project site.

Project Description
A Visual Studio 2012 Custom Tool for generating a strongly typed helper class for accessing localized resources from a .ResW file.

Features
- C# code generator
- VB.NET code generator


Visual Studio 2012 Custom Tool (C#)


Visual Studio 2012 Custom Tool (VB)


Resource File Contents


C# Usage
private string test1, test2, test3;

private void LoadLocalizedStrings()
{
    test1 = App1.LocalizedResources.Resources.Test1;
    test2 = App1.LocalizedResources.Resources.Test2;
    test3 = App1.LocalizedResources.Resources.Test3;
}

Visual Basic Usage
Dim test1, test2, test3

Private Sub LoadLocalizedStrings()
    test1 = AppVb.LocalizedStrings.Resources.Test1
    test2 = AppVb.LocalizedStrings.Resources.Test2
    test3 = AppVb.LocalizedStrings.Resources.Test3
End Sub

Generated C# Code
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.18010
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

// --------------------------------------------------------------------------------------------------
// <auto-generatedInfo>
//  This code was generated by ResW File Code Generator (http://reswcodegen.codeplex.com)
//  ResW File Code Generator was written by Christian Resma Helle
//  and is under GNU General Public License version 2 (GPLv2)
// 
//  This code contains a helper class exposing property representations
//  of the string resources defined in the specified .ResW file
// 
//  Generated: 11/08/2012 22:41:22
// </auto-generatedInfo>
// --------------------------------------------------------------------------------------------------
namespace App1.LocalizedResources
{
    using Windows.ApplicationModel.Resources;
    
    
    public partial class Resources
    {
        
        private static ResourceLoader resourceLoader = new ResourceLoader();
        
        /// <summary>
        /// Localized resource similar to "Test 1 value"
        /// </summary>
        public static string Test1
        {
            get
            {
                return resourceLoader.GetString("Test1");
            }
        }
        
        /// <summary>
        /// Localized resource similar to "Test 2 value"
        /// </summary>
        public static string Test2
        {
            get
            {
                return resourceLoader.GetString("Test2");
            }
        }
        
        /// <summary>
        /// Localized resource similar to "Test 3 value"
        /// </summary>
        public static string Test3
        {
            get
            {
                return resourceLoader.GetString("Test3");
            }
        }
    }
}

Generated Visual Basic Code
'------------------------------------------------------------------------------
' <auto-generated>
'     This code was generated by a tool.
'     Runtime Version:4.0.30319.18010
'
'     Changes to this file may cause incorrect behavior and will be lost if
'     the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------

Option Strict Off
Option Explicit On

Imports Windows.ApplicationModel.Resources

'--------------------------------------------------------------------------------------------------
'<auto-generatedInfo>
' This code was generated by ResW File Code Generator (http://reswcodegen.codeplex.com)
' ResW File Code Generator was written by Christian Resma Helle
' and is under GNU General Public License version 2 (GPLv2)
'
' This code contains a helper class exposing property representations
' of the string resources defined in the specified .ResW file
'
' Generated: 11/12/2012 21:30:52
'</auto-generatedInfo>
'--------------------------------------------------------------------------------------------------
Namespace AppVb.LocalizedStrings
    
    Partial Public Class Resources
        
        Private Shared resourceLoader As ResourceLoader = New ResourceLoader()
        
        '''<summary>
        '''Localized resource similar to "Test 1 value"
        '''</summary>
        Public Shared ReadOnly Property Test1() As String
            Get
                Return resourceLoader.GetString("Test1")
            End Get
        End Property
        
        '''<summary>
        '''Localized resource similar to "Test 2 value"
        '''</summary>
        Public Shared ReadOnly Property Test2() As String
            Get
                Return resourceLoader.GetString("Test2")
            End Get
        End Property
        
        '''<summary>
        '''Localized resource similar to "Test 3 value"
        '''</summary>
        Public Shared ReadOnly Property Test3() As String
            Get
                Return resourceLoader.GetString("Test3")
            End Get
        End Property
    End Class
End Namespace

Thursday, January 17, 2013

Parenthood

Once again I have taken a break from blogging. Parenting and my new family has been my absolute top priority and I've been having a hard time finding the time to write articles. I really enjoy spending time with my son and wife and I take advantage of every possible moment to do so.

I ended up at a schedule that starts around 5:30 AM in which my son wakes up, and wakes me up. I go and start preparing some breakfast for him. We then have a cozy morning playing around the house. I drop him off at day care as late possible (in my case 9:00 AM) and then drive to the office, or to a customer, or back home if I'm working from home. I pick my son up at between 3:00-4:00 PM and spend time with him and my wife until around 6:30 PM (this is his bed time and I'm very lucky that he has no trouble sleeping). From around 6:30 to 8:00-9:00 PM I spend some time with my wife discussing how our days went and do some planning for the following days. My wife goes to bed a bit early, and when that happens I go back to work. Since I drastically shortened by day, I need to do quite a bit of catching up at night. I usually work an extra 2-3 hours more at night, then I have an hour or 2 to give my mind a rest or work on a hobby project. My day usually ends between 12:30 AM and 2:00 AM. I need at least 4 hours of sleep to recharge for the next day, otherwise I won't be able to last a full week on my schedule.

Hence the break from blogging :)