Locale for number and date formatting

Hello!
As far as I have investigated it is possible to get correctly (by correctly I mean by using desired locale) formatted date or number values when I set a global VM argument or set implicitly Locale.setDefault() in my code.
Thread How do I change the Locale for a Word document so numeric formatting works? does not offer any other solution.
It is unacceptable for application servers. I cannot change the VM locale for all the dosens of threads running on the server.
I am sure there is a way to pass my defined locale to a Document object.
How can I achieve it?

Hi Piotr,


Thanks for your inquiry. Please see the following workflow.
Locale currentCulture = Locale.getDefault();
Locale.setDefault(new Locale(“it”, “IT”));
// Locale has been changed just before Document object creation
Document doc = new Document(getMyDir() + “Original.rtf”);
doc.save(getMyDir() + “Out.pdf”);
// We will not use doc object after this point
// so set the Locale to its old value
Locale.setDefault(currentCulture);
Hope, this helps.

Best regards,

Hi,

It will work. I have checked it before submitting my question.

The problem is this solution cannot be used in a multithreaded environment on an application server. Two reasons that come to my mind:
1. Race conditions. There can be another thread that can e.g. change the default locale back to the original one just a moment before my thread calls "save".
2. When I change the default locale for a moment users served by dozens of other threads running on the same application server might encounter unpredictable problems.

Do you plan to offer other solutions in future versions of Aspose? The most simple one - passing a locale variable to a Document object - seems to be the most straightforward.

Piotr Grubicki.

Hi Piotr,


Thanks for the additional information. We have logged your requirement in our issue tracking system. The ID of this issue is WORDSNET-12708. Our product team will further look into the details of this problem and we will keep you updated on the status of this issue. We apologize for your inconvenience.

Best regards,

Hi Piotr,


You should use ThreadLocal in multithread environment. It works perfectly in our multithread test units.

Regards,

Hi Konstantin,

Could you elaborate in more detail how it could help me?
I have to call

Locale.setDefault(currentCulture);
before I save the document anyway.
It will set the locale for the whole virtual machine. It doesn’t matter if the parameter passed is thread local or not.

Piotr.

It depends on your application. We use this utility class:



import java.util.Locale;


/
* FIX WORDSJAVA-771. Emulates .Net thread-isolated Locale settings.
*
* ThreadLocal is most effective for such things but… it can cause memory leaks
* in some web-server environments that use thread pulls and reusable threads
* since ThreadLocal variable is automatically released only after parent Thread died.
* (So launching Aspose.Words in such reusable thread will create new ThreadLocal variable
* in addition to existing (non-deleted) variables from former AW launches)
*
* Copyright © 2001-2015 Aspose Pty Ltd. All Rights Reserved.
* 03.12.2013 by Nail Islamov, Konstantin Sidorenko
*/

public class ThreadLocalLocale
{

/
* Returns thread-local locale if it is set.
* Otherwise, returns system default locale.
*/

public static Locale getDefault()

{

Locale locale = threadLocale.get();

if (locale != null)

return locale;

return Locale.getDefault();

}


/
* Set thread-local locale.
*/

public static void setDefault(Locale locale)

{

Locale defaultLocale = Locale.getDefault();


if (defaultLocale.getDisplayName().equals(locale.getDisplayName()))

reset();

else

threadLocale.set(locale);

}


/
* Reset thread-local locale.
*/

public static void reset()

{

threadLocale.remove();

}


private static final ThreadLocal threadLocale = new ThreadLocal();

}


ThreadLocal should be set inside try/finally block like this:


Locale oldLocale = ThreadLocalLocale.getDefault();
try
{
ThreadLocalLocale.setDefault(newLocale);

// do something...
}
finally
{
ThreadLocalLocale.setDefault(oldLocale);
}

Hi,

Could you explain me how it is supposed to work?
This class does only set locale on a local variable threadLocale. Nothing more. It will not affect niether the thread, nor the JVM locale.

Aspose java uses the default JVM locale (Locale.getDefault()).

To be 100% sure I have checked the code. It does not work. The only thing that works is setting Locale.setDefault(my_locale) before saving the document.

Hi Piotr,

We are in coordination with product team to get answer pertaining to your queries. Soon you will be updated with the required information.

Best regards,

Hi Piotr,


The code works well in our side. Anyway it is just a sample how to use ThreadLocal.
Please post here your code with ThreadLocal that do not work.

Regards,

Hi!

The code that works but cannot be used because of multithreading environment:
Document doc;

Locale.setDefault(new Locale(“pl”,“PL”));
doc.save(output, SaveFormat.PDF);

The code that does not work:
//class member
private static final ThreadLocal threadLocale = new ThreadLocal();


Document doc;

threadLocale.set(new Locale(“pl”,“PL”));
doc.save(output, SaveFormat.PDF);

Piotr.

Hi Piotr,

We are in coordination with product team to get answer pertaining to your queries. Soon you will be updated with the required information.

Best regards,

Hi Piotr,


Thanks for being patient. It is to update you that the fix of your issue WORDSNET-12708 (renamed as WORDSJAVA-1272) will be included in next 16.1.0 - Jan 2016 release. After when 16.1.0 version is published, please use new CurrentThreadSettings class to set ThreadLocal or/and ThreadLocal before open/save a document with non-default locale settings. Hope, this helps. We will inform you via this thread as soon as next version containing the fix of this issue is released.

Best regards,

The issues you have found earlier (filed as WORDSJAVA-1272) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.