My favorites | English | Sign in

Google Desktop APIs (Labs)

Translating into different languages with strings.xml

This article was written and submitted by an external developer. The Google Desktop Team thanks Stefan Van Damme for his time and expertise.


Stefan Van Damme, Google Desktop Developer
July 2009

Introduction

When you've finished building your gadget, you should then think about internationalization for the many people across the world who might use your gadget. In this article, you'll see how to make your gadget understandable for all users. I will demonstrate this using my own Fast shutdown gadget as an example.

Language strings


Figure 1: Screenshot of gadget settings dialog in the Designer

When you first create a gadget you'll see a directory named en, and that name happens to be the language code for English. That directory contains resources such as strings.xml that are meant to be shown to English speakers. To support another language for your gadget, just make a copy of that directory and set its name to an official ISO639-1 language code. Here are examples of language codes:

English: en
Dutch: nl
German: de
French: fr

Within the directories, you can also specify different images, view layouts, or even JavaScript files for specific language users.

Multiple languages

All translatable strings that are visible in your gadget's user interface must be placed in a file named strings.xml. Each language directory contains its own version of this file. This file contains named XML elements representing translated strings for a particular language. Here is an example:

<strings>
<GADGET_NAME>Fast Shutdown</GADGET_NAME>
<PLUGIN_DESCRIPTION>With one click you can turn the computer off or lock it.</GADGET_DESCRIPTION>
<GADGET_ABOUT_TEXT>Fast Shutdown v4
Copyright 2009 Stefan vd
Official website http://www.stefanvd.net

A fast way to shutdown you computer or lock you computer.
</GADGET_ABOUT_TEXT>
</strings>

The strings can be referenced from XML files or within the JavaScripts for your gadget.. For example, in the gadget.gmanifest you usually see references to strings in the strings.xml for values such as the gadget name or about text. The string name is prepended with an ampersand and appended with a semicolon. So GADGET_NAME is referenced as &GADGET_NAME;. For an example of this, see Figure 1. Within scripts, the string can be referenced simply by its name. So for example you could execute a view.alert(GADGET_NAME); which pops up a dialog containing the gadget name. To be precise, the strings are actually properties of a global strings object. So technically, you could reference the strings via the strings object like this: view.alert(strings.GADGET_NAME);. However, the gadget engine brings in the strings object into global scope for your convenience.

One interesting technique is to use strings.xml values as parameters that change depending on the user's language. Let's say you want to determine whether the gadget should default to the metric system for length measurements. You could define a string called IS_METRIC that contains values of true or false.

<!-- Default to metric? -->
<IS_METRIC>false</IS_METRIC>

Conclusion

Please work to make your gadget understandable to more people in this world. I'd like to see about dialogs translated to more languages such as Dutch, French, or German. After doing so, you may even receive some friendly feedback from speakers of those languages.

Small plug: please also check out my Google Desktop tip about the most important items to place inside the about dialog.

Resources

Author Bio

I am a student from Belgium (Antwerp), and I like to making new gadgets for Google, programming new Windows applications and designing images for my products.
Also I am the author of popular Google Gadgets like the Windows Dock and Fast Shutdown gadgets. Be sure to visit my website to see more great projects and gadgets.

Creative Commons License This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.0 Belgium License.