I have to print some text to printer. I needed to translate that text in Urdu language also. For that I used google translation API and all was set. But I am facing problem in order of output.

 // this is the code which is giving my desired output
 String text = "Honda head light large size"
 String translatedText = GoogleTranslate.translate("ur", text);
 String myString = "Saboor"

  String printString = String.format("%-15s %-15s 
  %-10s",text,translatedText,myString);

   graphics.drawString(printString,x,y)

  //Above code is giving me correct output as following

  Honda head light large size     urdu translation        Saboor

  // Now pasting the code which create problem:

   String text = "Honda head light large size"
   String translatedText = GoogleTranslate.translate("ur", text);
   int val = 10

   String printString = String.format("%-15s %-15s 
   %-10s",text,translatedText,val);

   graphics.drawString(printString,x,y);

  // now the output of this code is following(which i don't want):

 Honda head light large size   10       udru translation

// but i need following output:

  Honda head light large size     urdu translation     10

This question may already have an answer here:

How can I create table using ASCII in a console? 8 answers
I have to print some text to printer. I needed to translate that text in Urdu language also. For that I used google translation API and all was set. But I am facing problem in order of output.

// this is the code which is giving my desired output
String text = "Honda head light large size"
String translatedText = GoogleTranslate.translate("ur", text);
String myString = "Saboor"

String printString = String.format("%-15s %-15s
%-10s",text,translatedText,myString);

graphics.drawString(printString,x,y)

//Above code is giving me correct output as following

Honda head light large size urdu translation Saboor

// Now pasting the code which create problem:

String text = "Honda head light large size"
String translatedText = GoogleTranslate.translate("ur", text);
int val = 10

String printString = String.format("%-15s %-15s
%-10s",text,translatedText,val);

graphics.drawString(printString,x,y);

// now the output of this code is following(which i don't want):

Honda head light large size 10 udru translation

// but i need following output:

Honda head light large size urdu translation 10

As you see in case of code(correct output), after printing translated text, when i print a String , it prints correctly.

But after printing translated text,when i print a decimal or float number , alignment or order goes wrong. Please guide me, why is this happening?

Note: Urdu language has right to left direction.

Dani AI

Generated

Short diagnosis: the Unicode bidirectional (Bidi) rules are reordering your mixed English/Urdu line so the numeric run is resolving visually where the algorithm expects numbers to appear inside an RTL context. 's pointer to directional control characters is valid, but in Java it's more robust to hand the paragraph to the text-layout/Bidi engine or to render the columns separately so each field keeps its intended direction. (docs.oracle.com)

Preferred fix (safe, platform-aware): build an AttributedString for the RTL segment and give it a run-direction or embedding attribute, then draw that attributed iterator with Graphics2D so Java applies the proper reordering and shaping. Example pattern (not the same as the String.format approach you posted):

AttributedString as = new AttributedString(urduText);
as.addAttribute(TextAttribute.FONT, font);
as.addAttribute(TextAttribute.RUN_DIRECTION, TextAttribute.RUN_DIRECTION_RTL, 0, urduText.length());
AttributedCharacterIterator aci = as.getIterator();
graphics.drawString(aci, x, y);

This lets Java compute visual ordering rather than relying on manual markers. (docs.oracle.com)

If you need strict column layout for a printer, the simplest reliable approach is to render columns independently: measure each cell (FontMetrics or TextLayout), compute the x coordinate for right-aligned RTL cells as (columnRight - cellWidth), then draw the three fields separately (LTR English, RTL Urdu, numeric) so their visual positions never fight the bidi rules inside one combined string. TextLayout and FontMetrics give the measurements you need. (docs.oracle.com)

Advanced options: use java.text.Bidi to inspect runs or ICU4J (Bidi + ArabicShaping) for more complete shaping/reordering when you must pre-process text. As a rule, avoid inserting directional-control characters unless you need a quick hack—they can be brittle across fonts/printers. (docs.oracle.com)

Notes: verify the printer font supports Urdu glyphs and test the output at the device DPI. This approach should give stable column positions and correct visual order for mixed LTR/RTL content for .

Congratulations on a really interesting question!

maybe what’s going on is in the String.format you process a l-r string, then a r-l and after that the direction is still r-l when the third argument is processed.

There are Unicode chars to override the direction, but more than that I don’t know. They are

left-to-right mark: 0x200e
right-to-left mark: 0x200f

I think you insert them in the string, but I don’t know where exactly. Maybe that’s enough info for you to find more details?

Cheers
JC

commented: Yea this is one I've never seen before +11
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.