Skip to main content

Software  > Globalization > 

Globalize your On Demand Business

Java Examples
Bidi.java This is a complete code sample for ComponentOrientation settings and changing dynamically in all Swing components. It also includes the minor differences between the Sun and IBM implementations.

BidiAWT.java This is another sample showing an AWT program that changes alignment according to the ComponentOrientation.

JDK 1.4 NumericShaper example:

Bidi text;
char[] input;
AttributedCharacterIterator Para;
AttributedString string;
Map map;
NumericShaper shaper;
String  Level;
String txt="english and Bidi text";
map = new HashMap();
map.put(TextAttribute.NUMERIC_SHAPING,            NumericShaper.getContextualShaper(NumericShaper.ARABIC));

input = new char[txt.length()+1];
input = txt.toCharArray();

string = new AttributedString(txt, map);
Para = string.getIterator();
txt = new Bidi(Para);

// shape European digits to ARABIC digits if preceding text is Arabic, or
// leave European digits alone if there is no preceding text, or
// preceding text is not Arabic
shaper = NumericShaper.getContextualShaper(NumericShaper.ARABIC,
NumericShaper.EUROPEAN);
shaper.shape(input, 0, txt.lengtht());
txt = txt.copyValueOf(input); //  

This is a fully working example for the NumericShaper class

And this is an example for the Bidi class usage

Example for how to query the locale information

import java.util.*;
import java.text.*;
import java.io.*;
import java.awt.*;
public class Locales extends Frame{
     static BufferedWriter out;

     static public void displayNumber(Locale currentLocale) {
      Integer quantity = new Integer(123456);
      Double amount = new Double(345987.246);
      NumberFormat numberFormatter;
      String quantityOut;
      String amountOut;
      numberFormatter = NumberFormat.getNumberInstance(currentLocale);
      quantityOut = numberFormatter.format(quantity);
      amountOut = numberFormatter.format(amount);
      System.out.println("Integer :  " + quantityOut + "   " );
      System.out.println("Float :  " + amountOut + "   " );
   }
   static public void displayCurrency(Locale currentLocale) {
      Double currency = new Double(9876543.21);
      NumberFormat currencyFormatter;
      String currencyOut;
      currencyFormatter = NumberFormat.getCurrencyInstance(currentLocale);
      currencyOut = currencyFormatter.format(currency);
      System.out.println("Currency :  " +currencyOut );
   }
   static public void displayPercent(Locale currentLocale) {
      Double percent = new Double(0.75);
      NumberFormat percentFormatter;
      String percentOut;
      percentFormatter = NumberFormat.getPercentInstance(currentLocale);
      percentOut = percentFormatter.format(percent);
      System.out.println("Percent :  " + percentOut + "   " + currentLocale.toString());
   }
   static public void displayDate(Locale currentLocale) {
      Date today;
      String dateOut;
      DateFormat dateFormatter;
      dateFormatter =
         DateFormat.getDateInstance(DateFormat.DEFAULT, currentLocale);
      today = new Date();
      dateOut = dateFormatter.format(today);
      System.out.println("Date :  " + dateOut + "   " );
   }
   static public void displayLongDateTime(Locale currentLocale) {
      Date today;
      String result;
      DateFormat formatter;
      formatter =
        DateFormat.getDateTimeInstance(DateFormat.LONG,
                                        DateFormat.LONG,
                                        currentLocale);
      today = new Date();
      result = formatter.format(today);
      System.out.println("LongDate&Time :  " + result );
   }
   static public void showDateStyles(Locale currentLocale) {
      Date today = new Date();
      String result;
      DateFormat formatter;
      int[] styles = {
         DateFormat.DEFAULT,
         DateFormat.SHORT,
         DateFormat.MEDIUM,
         DateFormat.LONG,
         DateFormat.FULL
      };
      String[] style = {
        "Default",
        "Short",
        "Medium",
        "Long",
        "Full"
      };
      System.out.println("Date Styles : ");
      for (int k = 0; k < styles.length; k++) {
         formatter =  DateFormat.getDateInstance(styles[k], currentLocale);
         result =  formatter.format(today);
         System.out.println(style[k] +" :  " + result);
      }
   }
   static public void showTimeStyles(Locale currentLocale) {
      Date today = new Date();
      String result;
      DateFormat formatter;
      int[] styles = {
         DateFormat.DEFAULT,
         DateFormat.SHORT,
         DateFormat.MEDIUM,
         DateFormat.LONG,
         DateFormat.FULL
      };
      String[] style = {
        "Default",
        "Short",
        "Medium",
        "Long",
        "Full"
      };
      System.out.println("Time Styles :  ");
      for (int k = 0; k < styles.length; k++) {
         formatter =
            DateFormat.getTimeInstance(styles[k], currentLocale);
         result = formatter.format(today);
      System.out.println(style[k] +" :  " + result);
      }
   }
   static public void main(String[] args) {
      Locale locales[] = DateFormat.getAvailableLocales();
      for (int i = 0; i < 20; i++) {
         System.out.println("           ----------------");
         System.out.println("Locale: " + locales[i]);
         System.out.println("\nLanguage :  " + locales[i].getDisplayLanguage());
         System.out.println("\nCountry :  " + locales[i].getDisplayCountry());
         System.out.println("\nMonths :");
         DateFormatSymbols symbols = new DateFormatSymbols(locales[i]);
         String[] months = symbols.getMonths();
         String[] smallmonth = symbols.getShortMonths();
         for(int j=0;j<12;j++){
             System.out.println("\n" + months[j] + " , " + smallmonth[j]);
         }
         System.out.println("\nDays of the week :");
         String[] week = symbols.getWeekdays();
         String[] smallweek = symbols.getShortWeekdays();
         for(int j=1;j<8;j++){
             System.out.println("\n" + week[j] + " , " + smallweek[j]);
         }
         displayDate(locales[i]);
         showDateStyles(locales[i]);
         showTimeStyles(locales[i]);
         System.out.println();
         displayLongDateTime(locales[i]);
         System.out.println();
         displayNumber(locales[i]);
         displayCurrency(locales[i]);
         displayPercent(locales[i]);
      }
   }
}
 

These are some examples on using the TextLayout(extracted from Sun's documentation)
 

Constructing and drawing a TextLayout and its bounding rectangle:

       Graphics2D g = ...;
       Point2D loc = ...;
       Font font = Font.getFont("Helvetica-bold-italic");
       TextLayout layout = new TextLayout("This is a string", font);
       g.drawString(layout, loc.getX(), loc.getY());

       Rectangle2D bounds = layout.getBounds();
       bounds.setRect(bounds.getX()+loc.getX(),
                      bounds.getY()+loc.getY(),
                      bounds.getWidth(),
                      bounds.getHeight())
       g.draw(bounds);
 

Hit-testing a TextLayout (determining which character is at a
particular graphical location):

       Point2D click = ...;
       TextHitInfo hit = layout.hitTestChar(
                             (float) (click.getX() - loc.getX()),
                             (float) (click.getY() - loc.getY()));
 

Responding to a right-arrow key press:

   int insertionIndex = ...;
   TextHitInfo next = layout.getNextRightHit(insertionIndex);
   if (next != null) {
       // translate graphics to origin of layout on screen
       g.translate(loc.getX(), loc.getY());
       Shape[] carets = layout.getCaretShapes(next.getInsertionIndex());
       g.draw(carets[0]);
       if (carets[1] != null) {
           g.draw(carets[1]);
       }
   }
 

Drawing a selection range corresponding to a substring in the source
text. The selected area may not be visually contiguous:

   // selStart, selLimit should be relative to the layout,
   // not to the source text

   int selStart = ..., selLimit = ...;
   Color selectionColor = ...;
   Shape selection = layout.getLogicalHighlightShape(selStart, selLimit);
   // selection may consist of disjoint areas
   // graphics is assumed to be tranlated to origin of layout
   g.setColor(selectionColor);
   g.fill(selection);
 

Drawing a visually contiguous selection range. The selection range
may correspond to more than one substring in the source text. The
ranges of the corresponding source text substrings can be obtained
with getLogicalRangesForVisualSelection():

   TextHitInfo selStart = ..., selLimit = ...;
   Shape selection = layout.getVisualHighlightSelection(selStart, selLimit);
   g.setColor(selectionColor);
   g.fill(selection);
   int[] ranges = getLogicalRangesForVisualSelection(selStart, selLimit);
   // ranges[0], ranges[1] is the first selection range,
   // ranges[2], ranges[3] is the second selection range, etc.

 

For more information on this topic, please contact us at global@us.ibm.com


 
 


E-mail us
Easy ways to get the answers you need.
E-mail us