Language

Hi,

I have a problem with language locales, so

attached file have a Hindi(India) paragraph, I try to write some English text into this paragraph but when I open file in MS Word its recognized like a Hindi text, look following code and say me am I wrong or its your bug?

Document document = new Document("path+onesentence.docx");
DocumentBuilder builder = new DocumentBuilder(document);
Paragraph paragraph = (Paragraph) document.getSections().get(0)
    .getBody().getParagraphs().get(1);
builder.moveTo(paragraph.getFirstChild());
builder.getFont().setBidi(false);
builder.getFont().setLocaleId(2057);
builder.write("English text");
document.save(path + EnglishUK.docx);

I attach saved file too, EnglishUK.docx.

Thanks,
Bojan

Hi Bojan,

Thanks for your inquiry. I have not found any issue with your code and documents. Please manually create your expected Word output document using Microsoft Word and attach it here for our reference. We will investigate as to how you want your final Word output be generated like. We will then provide you more information on this along with code.

Hi,

Open attached file in MS Word and get the position of cursor in English text then you can see in Language bar this text are recognized as English, that is not the case when I do this with Aspose.Words.

Thanks,
Bojan

Hi Bojan,

Thanks for sharing the document. Please use the Font.setLocaleIdBi method to solve your issue as shown in following code snippt. You may use Run node to insert text in a Paragraph. Pelase see the following commented code. Hope this helps you. Please let us know if you have any more queries.

builder.getFont().setLocaleIdBi(2057);
// Run run = new Run(document, "English text");
// paragraph.insertBefore(run, paragraph.getFirstChild());
builder.write("English text");
document.save(MyDir + "out.docx");

Hi,

this is good for this example but this method “setLocaleIdBi(intvalue)” in your API is for right-to-left languagu, English is not. And when I have a document (docx file) which paragraph text is eg French(attached file France.docx) then try to do with your code:

builder.getFont().setLocaleIdBi(2057);
builder.write("English text");

saved file will not be good because now language stay the France, and one question more, when I need to use a “setLocaleIdFarEast(intvalue)”?

Thanks,
Bojan

Hi Bojan,

Please accept my apologies for your inconvenience. In your case, I suggest you please call Font.clearFormatting method before setting the LocaleId as shown in following code snippet.

Document document = new Document(MyDir + "onesentence.docx");
DocumentBuilder builder = new DocumentBuilder(document);
Paragraph paragraph = (Paragraph) document.getSections().get(0).getBody().getParagraphs().get(0);
builder.moveTo(paragraph.getFirstChild());
builder.getFont().clearFormatting();
builder.getFont().setBidi(false);
builder.getFont().setLocaleId(2057);
builder.write("English text");
document.save(MyDir + "out.docx");

The Font.setLocaleIdFarEast method is used to gets or sets the locale identifier (language) of the formatted Asian characters.
I have not found the France.docx in attachment. Please share this document if you still face issue.

Hi,

attached file(France.docx).

So what I want is to keep all formating and just to set other local(language) and write text in other language, what is the best way for this?

Many thanks,
Bojan

Hi Bojan,

Thanks for sharing the doucument. You are inserting the text with differnt LocaleId in the same Paragraph. In this case, I suggest you please use the Run node to insert text in the same Paragraph as shown in following code snippet.

Document document = new Document(MyDir + "France.docx");
Paragraph paragraph = (Paragraph) document.getSections().get(0).getBody().getParagraphs().get(0);
Run run = new Run(document, "English text");
run.getFont().setLocaleId(2057);
paragraph.insertBefore(run, paragraph.getFirstChild());
document.save(MyDir + "out.docx");

However, you may also use the code snippet share at this post (my previous post). In case you are using an older version of Aspose.Words, I would suggest you please upgrade to the latest version (v13.3.0.1) from here.

Hi,

thanks for help. This is not so good for me because I use the DocumentBuilder for writining text and setting a font of it. It will be so good that you can fix this for the DocumentBuilder class?

One question more, you suggest me that I use the following code for setting locale id: Font.setLocaleIdBi() for the file “onesentence.docx” to set the locale id.

I try this with the value 9(English LCID) but when open the out file(attached) in MS Word it reqognize as Arabic, what is the problem?

Document document = new Document(path + "onesentence.docx");
DocumentBuilder builder = new DocumentBuilder(document);
Paragraph paragraph = (Paragraph) document.getSections().get(0)
    .getBody().getParagraphs().get(0);
builder.moveTo(paragraph.getFirstChild());
builder.getFont().setBidi(false);
builder.getFont().setLocaleIdBi(9);
builder.write("English text");
document.save(path + "English.docx");

Many thanks,
Bojan

Hi Bojan,
Thanks for your inquiry. In case you are using an older version of Aspose.Words, I would suggest you please upgrade to the latest version (v13.4.0) from here and let us know how it goes on your side.

bjerinic: This is not so good for me because I use the DocumentBuilder for writining text and setting a font of it. It will be so good that you can fix this for the DocumentBuilder class?

Please use the code snippet shared at following forum link. This code snippet shared at following link uses DocumentBuilder.
https://forum.aspose.com/t/56664
Regarding your second query, following are the detail of setLocaleIdBi and setLocaleId methods.
setLocaleIdBi : Sets the locale identifier (language) of the formatted right-to-left characters.
setLocaleId : sets the locale identifier (language) of the formatted characters.
Please check the following code snippet for your kind reference. This code example also uses the DocumentBuilder to set locale.

Document document = new Document(MyDir + "France.docx");
DocumentBuilder builder = new DocumentBuilder(document);

Paragraph paragraph = (Paragraph)document.getSections().get(0).getBody().getParagraphs().get(0);
builder.moveTo(paragraph.getFirstChild());

builder.getFont().clearFormatting();
builder.getFont().setLocaleId(2057);

// Specify the locale so Microsoft Word
recognizes this text as Arabic - Saudi Arabia.
builder.getFont().setLocaleIdBi(1025);

builder.getFont().setBidi(false);
builder.write("English text");

builder.getFont().setBidi(true);
builder.write("مرحبًا");

document.save(MyDir + "out.docx");