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,
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);
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,
Hi Piotr,
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:
/
* 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:
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,
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,
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.