MozNET Tutorial - Using MozStringBundleService To Localize Your Application UI

This simple tutorial will get you started with using the new MozStringBundleService to make localizing your application dead easy.
By the end of the tut, you'll be able to quickly start using the new class to make your program more language aware.
The class was first written for MozNET's internal use but, I when I started using it for the context menu a little light bulb light up in my head and it was like "Hey! This could be used to localize the host app too!" so, here we are ;)

Let's get started..

First off we need to create two files. The first one is the empty jar file and the second is a manifest with the same name that you give to your empty jar file. To create your jar file just make an empty zip file - make sure its a zip file, rar/7z won't work. Once you have your empty zip file rename it to have a jar extension instead of zip. Now you have a jar file and WinRAR/WinZip will both read/write to it.

Inside your new jar file you should add a folder or two, just to give it some structure, a good idea would be similar to:MyJar.jar -> locale -> en-US (of course you'll want to use whatever language code fits your language)Inside your language directory (en-US in the example above) you need to create the properties sheet containing the strings for your application. In the MozNET example application I named the properties sheet You can use any text editor to create your properties sheet. Once you're done just save the file with the .properties file extension and drag-drop it into your language folder in the jar. A simple rundown of how the properties sheet works is as follows:

Before we can get on with any code examples we need to discuss the manifest file. This file is what describes, to XulRunner, your file. Depending on the complexity of your jar file it can be as simple as one line to register it with XulRunner:


A description of the line above.. locale means that we're registering a properties sheet, myApp is a custom 'class' (can be anything you want) that 'separates' our jars from others, en-US is the language code we're using (your code for your translation), the final bit of the line states what jar file XulRunner should register and the directory our properties sheet is in. You'll want to change this to match your jar file, of course. Make sure the name of your jar file and the name of the manifest are the same.E.g. mozex.jar and mozex.manifest. Both of these files should be placed in the Chrome directory under your XulRunner runtime used by your program.

Now that you have an idea of how the properties sheet is put together we can start using it in our application. In the class which you want to display strings from the properties sheet (you can have more than one!) create a string variable as such..

Now, since we want to be as efficient as possible here we need to grab our strings in a bundle, MozStringBundle, and cache it.

Now, because this could lead to a lot of typing we'll want another little helper method to get strings from our bundle..

OK, presumably you already have a bunch of elements that contain strings displayed in the UI.If you are using WPF you can bind to the properties below. In Winforms, you can write a method that assigns the properties to your controls' text properties.

Notice that the strings used in the calls to GetLangString are the ones used in our properties file on the left side of the '=' sign.This is how we pull the translation up using the MozStringBundleService and the MozStringBundle class.

Aside from not showing how the above properties would be binded in WPF (not in the scope of this tutorial), you should be able to get this up and running in your own application with a minimum amount of trouble.