Java Preference Put setLocale(final Locale l)

Here you can find the source of setLocale(final Locale l)

Description

Sets the new default locale.

License

Open Source License

Parameter

Parameter Description
l The new default locale

Declaration

public static void setLocale(final Locale l) 

Method Source Code

//package com.java2s;
/*//  w  w w. j a v a 2s. co m
 * jGnash, a personal finance application
 * Copyright (C) 2001-2019 Craig Cavanaugh
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Locale;

import java.util.ResourceBundle;

import java.util.prefs.Preferences;

public class Main {
    /**
     * key for locale preference
     */
    private static final String LOCALE = "locale";
    /**
     * Historical path to the preference root
     */
    private static final String PREFERENCE_NODE = "/jgnash/util/Resource";
    private static ResourceBundle resourceBundle;

    /**
     * Sets the new default locale.  This must be called if overridden.
     *
     * @param l The new default locale
     */
    public static void setLocale(final Locale l) {
        Locale.setDefault(l);
        final Preferences p = Preferences.userRoot().node(PREFERENCE_NODE);
        p.put(LOCALE, encodeLocale(l));
        resourceBundle = null; // force a reload the resource bundle
    }

    private static String encodeLocale(final Locale locale) {
        final StringBuilder buf = new StringBuilder();

        buf.append(locale.getLanguage());
        if (!locale.getCountry().equals("")) {
            buf.append('.');
            buf.append(locale.getCountry());
            if (!locale.getVariant().equals("")) {
                buf.append('.');
                buf.append(locale.getVariant());
            }
        }
        return buf.toString();
    }
}

Related

  1. putBooleanDontOverwrite(Preferences prefs, String key, boolean value)
  2. putCLOB(Preferences prefs, String key, String clob)
  3. putList(String path, ArrayList list)
  4. putPrefsBoolean(Preferences prefs, String key, boolean value, boolean def)
  5. putPrefsStrings(Preferences prefs, String key, String[] strings)
  6. setPrefBoolean(Object context, final String key, final boolean value)
  7. setUserString(final String preferencesGroup, final String preferenceName, final String value)
  8. storeDiff(Preferences prefs, String key, int prev, int val, int defVal)
  9. storeNotDef(Preferences prefs, String key, boolean val, boolean defVal)