Solved: Problem with locale opencms changed JVM (Possible bug in OpenCms set locale JVM)

View: New views
2 Messages — Rating Filter:   Alert me  

Solved: Problem with locale opencms changed JVM (Possible bug in OpenCms set locale JVM)

by Deiverson Silveira-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi List,

About problem with JVM, I believe it is a bug in opencms, when he goes to set a locale that has more than 2 parts (fr_CA, pt_BR), i have module pt_BR ok, but code dont reading pt_BR and set locale JVM, i change class CmsLocaleManager.java in package org.opencms.i18n:

/**
     * Sets the default locale of the Java VM to <code>{@link Locale#ENGLISH}</code> if the
     * current default has any other language then English set.<p>
     *
     * This is required because otherwise the default (English) resource bundles
     * would not be displayed for the English locale if a translated default locale exists.<p>
     *
     * Here's an example of how this issues shows up:
     * On a German server, the default locale usually is <code>{@link Locale#GERMAN}</code>.
     * All English translations for OpenCms are located in the "default" message files, for example
     * <code>org.opencms.i18n.message.properties</code>. If the German localization is installed, it will be
     * located in <code>org.opencms.i18n.message_de.properties</code>. If user has English selected
     * as his locale, the default Java lookup mechanism first tries to find
     * <code>org.opencms.i18n.message_en.properties</code>. However, this file does not exist, since the
     * English localization is kept in the default file. Next, the Java lookup mechanism tries to find the servers
     * default locale, which in this example is German. Since there is a German message file, the Java lookup mechanism
     * is finished and uses this German localization, not the default file. Therefore the
     * user get the German localization, not the English one.
     * Setting the default locale explicitly to English avoids this issue.<p>
     */
    private static void setDefaultLocale() {

        // set the default locale to english
        // this is required because otherwise the default (english) resource bundles
        // would not be displayed for the english locale if a translated locale exists

        Locale oldLocale = Locale.getDefault();
        if (!(Locale.ENGLISH.getLanguage().equals(oldLocale.getLanguage()))) {
            // default language is not English
            try {
                Locale.setDefault(Locale.ENGLISH);
                if (CmsLog.INIT.isInfoEnabled()) {
                    CmsLog.INIT.info(Messages.get().getBundle().key(
                        Messages.INIT_I18N_DEFAULT_LOCALE_2,
                        Locale.ENGLISH,
                        oldLocale));
                }
            } catch (Exception e) {
                // any Exception: the locale has not been changed, so there may be issues with the English
                // localization but OpenCms will run in general
                CmsLog.INIT.error(Messages.get().getBundle().key(
                    Messages.LOG_UNABLE_TO_SET_DEFAULT_LOCALE_2,
                    Locale.ENGLISH,
                    oldLocale), e);
            }
        } else {
            if (CmsLog.INIT.isInfoEnabled()) {
                CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_I18N_KEEPING_DEFAULT_LOCALE_1, oldLocale));
            }
        }

        // initialize the static member with the new default
        m_defaultLocale = Locale.getDefault();
    }




I change method for this and set JVM ok:



    private static void setDefaultLocale() {

        // set the default locale to english
        // this is required because otherwise the default (english) resource bundles
        // would not be displayed for the english locale if a translated locale exists

        Locale oldLocale = Locale.getDefault();
    Locale loc = new Locale("pt","BR");

            try {
            Locale.setDefault(loc);
                if (CmsLog.INIT.isInfoEnabled()) {
                    CmsLog.INIT.info(Messages.get().getBundle().key(
                        Messages.INIT_I18N_DEFAULT_LOCALE_2,
                        loc,
                        oldLocale));
                }
            } catch (Exception e) {
                // any Exception: the locale has not been changed, so there may be issues with the English
                // localization but OpenCms will run in general
                CmsLog.INIT.error(Messages.get().getBundle().key(
                    Messages.LOG_UNABLE_TO_SET_DEFAULT_LOCALE_2,
                    loc,
                    oldLocale), e);
            }

        // initialize the static member with the new default
        m_defaultLocale = Locale.getDefault();
    }


SUCESS! This Work others system/projects, opencms dont impact in jvm when pt_BR...
but the jvm will be forever by setting pt_BR! I Belive other people must have the same problem when more project in server aplication!

_______________________________________________
This mail is sent to you from the opencms-dev mailing list
To change your list options, or to unsubscribe from the list, please visit
http://lists.opencms.org/mailman/listinfo/opencms-dev

Re: Solved: Problem with locale opencms changed JVM (Possible bug in OpenCms set locale JVM)

by Claus Priisholm :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I haven't run into this problem, but I can see how it can be a problem
if you run other applications in the same JVM that rely on the JVMs
Locale-settng.

 From reading the comment the real problem is that there is  no
*_en.properties files to be found and the setDefaultLocale() is supposed
to work around that problem.

But by doing so it effects the entire VM which sounds like a bad thing
(from the JavaDoc for Locale.setDefault(): "Since changing the default
locale may affect many different areas of functionality, this method
should only be used if the caller is prepared to reinitialize
locale-sensitive code running within the same Java Virtual Machine.")

It seems that the right solution is to add *_en.properties files to
OpenCms (maybe as a locale module) and thus avoid overriding the default
locale for the JVM. I think this would make OpenCms a nicer citizen
inside the JVM.

Anyway your solution basically nullifies the setDefaultLocale() hack as
far as I can see so if you JVM is already set to use pt_BR as default,
then a shorter version would be:

private static void setDefaultLocale() {
   m_defaultLocale = Locale.getDefault();
}

And as the result you now have the problem as described in the comment
for the method that you may get the pt_BR version of an localized
resource rather than the English (which of course may not be a problem
in your case).


Deiverson Silveira wrote:

> Hi List,
>
> About problem with JVM, I believe it is a bug in opencms, when he goes
> to set a locale that has more than 2 parts (fr_CA, pt_BR), i have module
> pt_BR ok, but code dont reading pt_BR and set locale JVM, i change class
> CmsLocaleManager.java in package org.opencms.i18n:
>
> /**
>      * Sets the default locale of the Java VM to <code>{@link
> Locale#ENGLISH}</code> if the
>      * current default has any other language then English set.<p>
>      *
>      * This is required because otherwise the default (English) resource
> bundles
>      * would not be displayed for the English locale if a translated
> default locale exists.<p>
>      *
>      * Here's an example of how this issues shows up:
>      * On a German server, the default locale usually is <code>{@link
> Locale#GERMAN}</code>.
>      * All English translations for OpenCms are located in the "default"
> message files, for example
>      * <code>org.opencms.i18n.message.properties</code>. If the German
> localization is installed, it will be
>      * located in <code>org.opencms.i18n.message_de.properties</code>.
> If user has English selected
>      * as his locale, the default Java lookup mechanism first tries to find
>      * <code>org.opencms.i18n.message_en.properties</code>. However,
> this file does not exist, since the
>      * English localization is kept in the default file. Next, the Java
> lookup mechanism tries to find the servers
>      * default locale, which in this example is German. Since there is a
> German message file, the Java lookup mechanism
>      * is finished and uses this German localization, not the default
> file. Therefore the
>      * user get the German localization, not the English one.
>      * Setting the default locale explicitly to English avoids this
> issue.<p>
>      */
>     private static void setDefaultLocale() {
>
>         // set the default locale to english
>         // this is required because otherwise the default (english)
> resource bundles
>         // would not be displayed for the english locale if a translated
> locale exists
>
>         Locale oldLocale = Locale.getDefault();
>         if
> (!(Locale.ENGLISH.getLanguage().equals(oldLocale.getLanguage()))) {
>             // default language is not English
>             try {
>                 Locale.setDefault(Locale.ENGLISH);
>                 if (CmsLog.INIT.isInfoEnabled()) {
>                     CmsLog.INIT.info
> <http://CmsLog.INIT.info>(Messages.get().getBundle().key(
>                         Messages.INIT_I18N_DEFAULT_LOCALE_2,
>                         Locale.ENGLISH,
>                         oldLocale));
>                 }
>             } catch (Exception e) {
>                 // any Exception: the locale has not been changed, so
> there may be issues with the English
>                 // localization but OpenCms will run in general
>                 CmsLog.INIT.error(Messages.get().getBundle().key(
>                     Messages.LOG_UNABLE_TO_SET_DEFAULT_LOCALE_2,
>                     Locale.ENGLISH,
>                     oldLocale), e);
>             }
>         } else {
>             if (CmsLog.INIT.isInfoEnabled()) {
>                 CmsLog.INIT.info
> <http://CmsLog.INIT.info>(Messages.get().getBundle().key(Messages.INIT_I18N_KEEPING_DEFAULT_LOCALE_1,
> oldLocale));
>             }
>         }
>
>         // initialize the static member with the new default
>         m_defaultLocale = Locale.getDefault();
>     }
>
>
>
>
> I change method for this and set JVM ok:
>
>
>
>     private static void setDefaultLocale() {
>
>         // set the default locale to english
>         // this is required because otherwise the default (english)
> resource bundles
>         // would not be displayed for the english locale if a translated
> locale exists
>
>         Locale oldLocale = Locale.getDefault();
>     Locale loc = new Locale("pt","BR");
>
>             try {
>             Locale.setDefault(loc);
>                 if (CmsLog.INIT.isInfoEnabled()) {
>                     CmsLog.INIT.info
> <http://CmsLog.INIT.info>(Messages.get().getBundle().key(
>                         Messages.INIT_I18N_DEFAULT_LOCALE_2,
>                         loc,
>                         oldLocale));
>                 }
>             } catch (Exception e) {
>                 // any Exception: the locale has not been changed, so
> there may be issues with the English
>                 // localization but OpenCms will run in general
>                 CmsLog.INIT.error(Messages.get().getBundle().key(
>                     Messages.LOG_UNABLE_TO_SET_DEFAULT_LOCALE_2,
>                     loc,
>                     oldLocale), e);
>             }
>
>         // initialize the static member with the new default
>         m_defaultLocale = Locale.getDefault();
>     }
>
>
> SUCESS! This Work others system/projects, opencms dont impact in jvm
> when pt_BR... but the jvm will be forever by setting pt_BR! I Belive
> other people must have the same problem when more project in server
> aplication!
>
>
> ------------------------------------------------------------------------
>
>
> _______________________________________________
> This mail is sent to you from the opencms-dev mailing list
> To change your list options, or to unsubscribe from the list, please visit
> http://lists.opencms.org/mailman/listinfo/opencms-dev

--
Claus Priisholm, CodeDroids ApS
Phone: +45 48 22 46 46
cpr (you know what) codedroids.com - http://www.codedroids.com
cpr (you know what) interlet.dk - http://www.interlet.dk
--
Javadocs and other OpenCms stuff:
http://www.codedroids.com/community/opencms

_______________________________________________
This mail is sent to you from the opencms-dev mailing list
To change your list options, or to unsubscribe from the list, please visit
http://lists.opencms.org/mailman/listinfo/opencms-dev