Introduction to IBM Classes for
Unicode
2.3 Remove Concatenation
If you haven't done much internationalization,
then you may not have heard the mantra: NEVER CONCATENATE STRINGS! Why is this a problem?
Well, the order of parts of a sentence is different in different languages; this
difference can easily lead you into trouble. For example, if you use the traditional
string concatenation mechanism offered by the language, the localization engineer
is limited to modifying only the string, and not the position of, say the date field. If
the language requires verbs to be at the end of the sentence, the localization
engineer is stuck.
You can replace concatenation by use of MessageFormats, which allow the localization
engineer to position the variable information appropriately:
MessageFormat Instead of Concatenation
Java |
| Old |
| display(MyResources.rb.getString("Deleting")
+ fileName); |
| New |
| MessageFormat mf = new
MessageFormat(MyResources.rb.getString("Deleting")); display(mf.format(new
Object[] {fileName})); |
Java Resource
Bundle |
| Old |
| {"Deleting",
"Deleting"}, |
| New |
| {"Deleting",
"Deleting {0}"}, |
C++ |
| Old |
| rb.getString("Deleting",uniString,err); display(uniString+fileName); |
| New |
| rb.getString("Deleting",uniString,err); MessageFormat mf(uniString, err);
mf.format(fileName,1,resultString,(FieldPosition)0,err);
display(resultString); |
C |
| Old |
| T_ResourceBundle_getString(rb,"Deleting",uniString,&err); T_Unicodestring_append(uniString,fileName);
display(uniString); |
| New |
| T_ResourceBundle_getString(rb,"Deleting",uniString,&err); MessageFormat *mf = T_MessageFormat_new(uniString,&err);
T_MessageFormat_format(mf,fileName,1,resultString,&((FieldPosition)0),&err);
display(resultString); |
C/C++
Resource Bundle |
| Old |
| Deleting {"Delete all files
before"} |
| New |
| Deleting {"Delete files
before {0}"} |
Java Note
The reason for using the array of objects for the parameter is to allow multiple
arguments. There will probably be convenience methods in the future to make this a bit
smoother. This new pattern string can then be localized, allowing rearrangement of the
position of the argument {0}. In Java, if you want to, you could combine this into a
single statement, using the static MessageFormat.format() p
Message formats can also be used to customize the precise format of dates, times,
numbers, or currencies. If you only specify the position of the argument, then a default
for the current locale will be chosen. However, you (for English) or the localization
engineer (for other languages) can also more precisely control the format if you
desire. Adding additional keywords or patterns after the argument number, as in the
following examples does this:
Argument
new Date(1997,6,13);
Pattern examples
Pattern |
Result |
| "Delete files before
{0}" |
Delete files before 6/13/97
1:00 AM |
| "Delete files before
{0,date,long}" |
Delete files before June 13,
1997 |
| "Delete files before
{0,date,yyyy.MMM.dd}" |
Delete files before
1997.Jun.13 |
|
|