Advanced Customization > Services and Infrastructure Customization > System Generation > Modeling Business Objects > Localized Text
  
Localized Text
We have already seen an example localized text; ComputerTypeRB.rbInfo contains the (default English) text for ComputerType. Beyond providing localized text for enumerated types, we will want to localize class and fields names as well as messages (like exception messages).
Class and attribute names
Introspection provides APIs to get the (localized) display name for classes and properties. Default display names are calculated when explicit values aren’t provided, as shown by the following example which produces “Simple Example.Name”:
Listing 22: Display values for SimpleExample
01 from wt.introspection import WTIntrospector
02 from com.acme.example import SimpleExample
03
04 snci = WTIntrospector.getClassInfo(SimpleExample)
05 print "%s.%s" % (snci.getDisplayName(), snci.getPropertyDisplayName
(snci.getPropertyDescriptor('name'), None))
Let’s say we want to display SimpleName as “Simple” and SimpleName.name as “aka”. You can do this by creating a MetadataResourceInfo in the same directory, as follows:
Listing 23: exampleModelRB.rbInfo
01 ResourceInfo.class=wt.tools.resource.MetadataResourceInfo
02
03 # Entry Format (values equal to default value are not included)
04 # <key>.value=
05 # <key>.category=
06 # <key>.comment=
07 # <key>.argComment<n>=
08 # <key>.constant=
09 # <key>.customizable=
10 # <key>.deprecated=
11 # <key>.abbreviatedDisplay=
12 # <key>.fullDisplay=
13 # <key>.shortDescription=
14 # <key>.longDescription=
15
16 # Entry Contents
17 SimpleExample.value=Simple
18 SimpleExample.name.value=aka
These bundles are similar to the bundle style used for enumerated types and are compiled in the same manner. Once compiled, our Jython script will produce “Simple.aka”.
Messages
Localizing class names and properties is not sufficient. Everything you wish to communicate to users should be localized (note: a developer is not, necessarily, a user). The rbInfo format is used for enumerated types as well as classes and their properties. For general messages (typically included in exception or status messages reported by services), Java source/class files ultimately extending java.util.ListResourceBundle are utilized. As the contract for ListResourceBundle is arcane, we’ll utilize WTListResourceBundle, which is more declarative. An example:
Listing 24: exampleResource.java
01 package com.acme.example;
02
03 import wt.util.resource.*;
04
05 /** This example blatantly plagiarized from the JavaDoc. **/
06 @RBUUID("com.acme.example.exampleResource")
07 public class exampleResource extends WTListResourceBundle {
08 @RBEntry("This is the localized text with a single substitution: \"{0}\".")
09 @RBComment("An example entry.")
10 @RBArgComment0("Any string...")
11 public static final String EXAMPLE_STRING = "0";
12 }
With this, we produce a message or an exception:
Listing 25: Messages utilizing exampleResource
01 from com.acme.example import exampleResource
02 from wt.util import WTException, WTMessage
03
04 print WTMessage(exampleResource.getName(), exampleResource.EXAMPLE_STRING,
['substitution']).getLocalizedMessage()
05
06 raise WTException(exampleResource.getName(), exampleResource.EXAMPLE_STRING,
['substitution'])
The first call will produce This is the localized text with a single substitution: ‘‘substitution’’. and the second will raise (throw) an exception with the message wt.util.WTException: (com.acme.example.exampleResource/0) wt.util.WTException: This is the localized text with a single substitution: ‘‘substitution’.
For more information, refer to the JavaDoc for the wt.util.resource package, particularly wt.util.resource.WTListResourceBundle.