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

Free Support Forum - aspose.com

Java MailMerge - locale used for formatting Dates and BigDecimals

Hi,


how do you set the locale used by MailMerge ?

We need this for formatting both dates and decimals.

Here is what we try:

Document doc = new Document(wordTemplate);
MailMerge mailMerge = doc.getMailMerge();
mailMerge.setCleanupOptions(MailMergeCleanupOptions.REMOVE_UNUSED_REGIONS | MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS
| MailMergeCleanupOptions.REMOVE_CONTAINING_FIELDS | MailMergeCleanupOptions.REMOVE_EMPTY_PARAGRAPHS);
mailMerge.executeWithRegions(genericDs);
File temporaryFile = File.createTempFile(wordTemplate, “.” + FilenameUtils.getExtension(wordTemplate));
doc.save(temporaryFile.getPath());

Thanks


Hi Hugo,

Thanks for your inquiry. Please set default locale for the specified category using Locale.setDefault to get the desired output.


Locale.setDefault(new Locale(“fr”, “FR”));

Document doc = new Document(MyDir + “in.docx”);



You may also use FieldOptions.FieldUpdateCultureSource property to specify what culture to use to format the field result. FieldUpdateCultureSource enumeration indicates what culture to use during field update.


CurrentThread : The culture of the current execution thread is used to update fields.

FieldCode : The culture specified in the field formatting properties via language setting is used.


Following code snippet shows how to use FieldUpdateCultureSource enumeration as FieldCode.


Document doc = new Document(MyDir + “in.docx”);

doc.getFieldOptions().setFieldUpdateCultureSource(FieldUpdateCultureSource.FIELD_CODE);

Thanks Tahir.


So we have to set this globally. This is executed server side, there is no way we can set this locally so we can provide different local depending on the user connected ?

Thanks

Hi Hugo,

Thanks for your inquiry. You need to set the Locale according to your template document. Locale.setDefault sets the default locale for the specified Category for this instance of the Java Virtual Machine. Please set the Locale where you are executing the Aspose.Words code.


You can also make use of FieldOptions.FieldUpdateCultureSource property to specify what culture to use to format the field result. Please note that by default, the culture of the current thread is used and this setting affects only date/time fields with \@ format switch.

For example, the following code snippet shows how to specify where the culture used for date formatting during field update and mail merge is chosen from.

// Set the culture used during field update to the culture used by the field.

doc.getFieldOptions().setFieldUpdateCultureSource(FieldUpdateCultureSource.FIELD_CODE);<o:p></o:p>

doc.getMailMerge().execute(new String[] { "quot_createddate" }, new Object[] { new java.util.Date() });


Hope this helps you. Please let us know if you have any more queries.

Thanks Tahir.


I don’t really understand how FieldUpdateCultureSource.FieldCode works : I have to set the locale in World ? How do I do that ?

Thanks again for your help.

Kind regards,
Hugo

Hi Hugo,

Thanks for your inquiry. Please execute the following code example with FieldUpdateCultureSource as CurrentThread and FieldCode to check the difference. We have attached sample input and output documents with this post for your kind reference. Please set the language of mail merge field according to your requirement from Review tab in Language section.


Document doc = new Document(MyDir + “in.docx”);<o:p></o:p>

// Set the culture used during field update to the culture used by the field.

doc.getFieldOptions().setFieldUpdateCultureSource(FieldUpdateCultureSource.FIELD_CODE);

doc.getMailMerge().execute(new String[] { "quot_createddate" }, new Object[] { new java.util.Date() });

If you still face problem, please share your input and expected output document here for our reference. We will then provide you more information about your query.