Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6197800

Per-thread Locale

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Won't Fix
    • Icon: P4 P4
    • None
    • 5.0, 6
    • core-libs
    • None

      In some applications, the same text is accessed by different clients within the
      same JVM that have different locales. For example, in the JMX API a managed
      object can have a description. The managed object can be accessed by several
      remote clients, and those clients could have different locales. It would make
      sense for the thread handling a remote client to be associated with that
      client's locale, so that the managed object could tailor its description to
      that locale. Different clients then see different descriptions. This is much
      simpler than trying to push a Locale parameter through every method between the
      code that knows the client's locale and all code that needs it.

      This could be achieved by adding the following to java.util.Locale:

          import java.util.concurrent.Callable;

          private static final ThreadLocal<Locale> threadLocale =
              new InheritableThreadLocal<Locale>();

          public static Locale getThreadLocale() {
              return threadLocale.get();
          }

          public static <V> V doWithThreadLocale(Locale locale, Callable<V> task)
                  throws Exception {
              Locale oldLocale = getThreadLocale();
              try {
                  threadLocale.set(locale);
                  return task.call();
              } finally {
                  threadLocale.set(oldLocale);
              }
          }

      Then a client handler can do something like this:

          Object result =
              Locale.doWithThreadLocale(clientLocale,
                  new Callable<Object>() {
                      public Object call() {
                          return handleClientRequest();
                      }
                  });

            naoto Naoto Sato
            emcmanus Eamonn McManus
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: