We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How do I change the Locale for a Word document so numeric formatting works?

We have documents with tables and numbers with formulas in the tables. When the language of the document matches the langage that the JVM considers the default everything works. When we have Aspose words update the formulas in a document that uses formatting for Euro's with the comma and period switched for numeric formatting, Aspose.Words seems to get confused and calculates the formula wrong. If I change the JVM to be in a language that uses periods for the grouping and commas for the dicimal the formulas calculate correctly.

I would like to switch the local per document and not set the JVM default Locale because ths is a multi-user and multi-threaded system. The odds of multiple Locale switched stepping on each other is quite high. With things like NumberFormat I can set the Locale on the object without having to change the JVM default Locale. I can't seem to find anything similar to that with Aspose.Words.

Thanks.

Hi Sherter,

while building your document, you can set your documentBuilder font to the locale you need, something like:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getFont().setLocaleId();

here you can find all the locales word accepts.

if you already have a built document and just want to modify it, you’ll have to iterate through the whole document converting it:

NodeCollection collection = doc.getChildNodes(NodeType.RUN, true);
for(Run run:collection){
    run.getFont().setlocaleId();
}

Hi Scott,

Thanks for your inquiry. Please note that Aspose.Words mimics the same behavior as
MS Word does. To control the locale used during field calculation, just
set the Locale.setDefault property to a locale of your choice before
invoking field calculation.

Locale.setDefault(new Locale(“en”, “US”));
Document doc = new Document(MyDir + “in.docx”);

Aspose.Words supports multilingual documents. You can manipulate with such document or create them from scratch. You can use Font.LocaleId property to specify language that will be used by MS Word.

Unfortunately the code above did not make a difference in what Aspose.Words is doing. I am using version 14.10. I have attached a zip file with 4 files in it.

Original.rtf - The file that I am having Aspose.Words convert to PDF.

Test_it_IT_No_Change.pdf - The server was set to Locale it_IT, no other code changes were made.

Test_en_US_No_Changes.pdf - The server was set to the Locale en_US, no other code changes were made.

Test_en_US_Code_Changes.pdf - The server was set to Locale en_US, I made the code changes above for an existing document. I loaded the document, ran the code to get the node collection and set them to ID 1040 which according to the LOCID list was Italian - Italy.

The code does call updateFields to update the sums. The table in the document has three SUM(ABOVE) fields along the bottom with a format of €#.##0,00;(€#.##0,00). The three column fields at the end have a formula for E2-F2 with a format of €#.##0,00;(€#.##0,00). The three columns do increment the number from 2 to 4 through the three column values vertically.

This is a test document to figure out if we can generate documents with different Locale's without having to change the servers Locale for each document processed. Fonts and text formatting is not an issue, it is the formulas in tables we are having a problem with.

The problem with calling Locale.setDefault() is that it changes it for the JVM, so all threads in the JVM will now use the new Locale. This is being done on a JEE application server, I can’t be changing the default Locale constantly, it can mess up other users on other threads. It will also cause race conditions where thread 1 sets the default, then thread 2 sets the default to something different, then thread 1 does the conversion, then thread 2 gets to run. Thread 1 will get the wrong Locale. Depending on load you could get something approaching a truely random Locale based on how many conversions are going on with different Locales on different threads. The only way to make this work would be to start a different VM for each request that comes in or have a different server for each Locale supported and route requests to the correct server. That would require a way too many servers to work.

Hi Scott,

Thanks for your inquiry. Please note that Aspose.Words mimics the same behavior as MS Word does. If you update the formula fields under English culture, you will get the same output. However, under Italian culture, you will get the correct output using MS Word. This is not an issue in Aspose.Words.

To control the locale used during field calculation, just
set the Locale.setDefault property to a locale of your choice before
invoking field calculation. Please check the following code example for your kind reference.

Locale currentCulture = Locale.getDefault();
Locale.setDefault(new Locale("it", "IT"));
Document doc = new Document(MyDir + "Original.rtf");
doc.updateFields();
doc.save(MyDir + "Out.pdf");
Locale.setDefault(currentCulture);

A post was split to a new topic: Change the local value and Thread safe