Home > Articles > Programming > Java

  • Print
  • + Share This
From the author of

Appendix B: One or two quotes?

How you plan to format your property file messages has an impact on the way that single quotes should be entered, that is, are they doubled or entered only once. To illustrate the problem, consider the code below:













package com.ibm.jumpstart.messageformattest;
import java.text.MessageFormat;

public class MessageFormatTest {
 public static void main(String[] args) {
  String msg1 = "This is a ''{0}'' program and it''s not ready for production.";
  String msg2 = "This is a beta program and it's not ready for production.";
  String msg3 = "This is a beta program and it's not ready for {0}.";

  System.out.println(MessageFormat.format(msg1, new String[] {"beta"}));
  System.out.println(MessageFormat.format(msg2, new String[0]));
  System.out.println(msg2);

  System.out.println(MessageFormat.format(msg3, new String[] {"production"}));
  System.out.println(msg3);
 }
}

The output is below:

          This is a 'beta' program and it's not ready for production.     
          This is a beta program and its not ready for production.     
          This is a beta program and it's not ready for production. 
          This is a beta program and its not ready for {0}.     
          This is a beta program and it's not ready for {0}.

The fourth line is particularly troubling, because not only is the apostrophe missing, but so is the substitution parameter! These differences stem from the fact that MessageFormat expects a quote to be entered twice if it is to be included in the result. No problem, but what if some programmers use MessageFormat, some write their own version for parameter substitution, and others don't use it at all unless there are substitution parameters? The translator obviously does not know what code will manipulate the translated text, so they must know how to enter required single quotes / apostrophes.

Here are the conventions that were adopted for required quotes during the Eclipse Workbench version 1.0 TVT:

  • If there are no substitution parameters, enter only one quote

  • If there are any substitution parameters (like {0}), enter two quotes

This is in acceptance of the observation that practically all programmers used MessageFormat only for those cases where there were substitution parameters. To avoid this programming inconsistency, version 2.0 of the Eclipse JDT includes an Externalize Strings wizard to help centralize message retrieval and parameter substitution. Consider adopting this strategy, or systematically using MessageFormat so your translators will only have one rule for required quotes.

This potential stumbling block is noted in the MessageFormat documentation:

"The rules for using quotes within message format patterns unfortunately have shown to be somewhat confusing. In particular, it isn't always obvious to localizers whether single quotes need to be doubled or not. Make sure to inform localizers about the rules, and tell them (for example, by using comments in resource bundle source files) which strings will be processed by MessageFormat. Note that localizers may need to use single quotes in translated strings where the original version doesn't have them."

  • + Share This
  • 🔖 Save To Your Account