Search in sources :

Example 11 with Locale

use of java.util.Locale in project tomcat by apache.

the class SessionUtils method guessLocaleFromSession.

public static Locale guessLocaleFromSession(final HttpSession in_session) {
    if (null == in_session) {
        return null;
    }
    try {
        Locale locale = null;
        // First search "known locations"
        for (int i = 0; i < LOCALE_TEST_ATTRIBUTES.length; ++i) {
            Object obj = in_session.getAttribute(LOCALE_TEST_ATTRIBUTES[i]);
            if (obj instanceof Locale) {
                locale = (Locale) obj;
                break;
            }
            obj = in_session.getAttribute(LOCALE_TEST_ATTRIBUTES[i].toLowerCase(Locale.ENGLISH));
            if (obj instanceof Locale) {
                locale = (Locale) obj;
                break;
            }
            obj = in_session.getAttribute(LOCALE_TEST_ATTRIBUTES[i].toUpperCase(Locale.ENGLISH));
            if (obj instanceof Locale) {
                locale = (Locale) obj;
                break;
            }
        }
        if (null != locale) {
            return locale;
        }
        // Tapestry 3.0: Engine stored in session under "org.apache.tapestry.engine:" + config.getServletName()
        // TODO: Tapestry 4+
        final List<Object> tapestryArray = new ArrayList<>();
        for (Enumeration<String> enumeration = in_session.getAttributeNames(); enumeration.hasMoreElements(); ) {
            String name = enumeration.nextElement();
            if (name.indexOf("tapestry") > -1 && name.indexOf("engine") > -1 && null != in_session.getAttribute(name)) {
                //$NON-NLS-1$ //$NON-NLS-2$
                tapestryArray.add(in_session.getAttribute(name));
            }
        }
        if (tapestryArray.size() == 1) {
            // found a potential Engine! Let's call getLocale() on it.
            Object probableEngine = tapestryArray.get(0);
            if (null != probableEngine) {
                try {
                    //$NON-NLS-1$
                    Method readMethod = probableEngine.getClass().getMethod("getLocale", (Class<?>[]) null);
                    // Call the property getter and return the value
                    Object possibleLocale = readMethod.invoke(probableEngine, (Object[]) null);
                    if (possibleLocale instanceof Locale) {
                        locale = (Locale) possibleLocale;
                    }
                } catch (Exception e) {
                    Throwable t = ExceptionUtils.unwrapInvocationTargetException(e);
                    ExceptionUtils.handleThrowable(t);
                // stay silent
                }
            }
        }
        if (null != locale) {
            return locale;
        }
        // Last guess: iterate over all attributes, to find a Locale
        // If there is only one, consider it to be /the/ locale
        final List<Object> localeArray = new ArrayList<>();
        for (Enumeration<String> enumeration = in_session.getAttributeNames(); enumeration.hasMoreElements(); ) {
            String name = enumeration.nextElement();
            Object obj = in_session.getAttribute(name);
            if (obj instanceof Locale) {
                localeArray.add(obj);
            }
        }
        if (localeArray.size() == 1) {
            locale = (Locale) localeArray.get(0);
        }
        return locale;
    } catch (IllegalStateException ise) {
        //ignore: invalidated session
        return null;
    }
}
Also used : Locale(java.util.Locale) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method)

Example 12 with Locale

use of java.util.Locale in project tomcat by apache.

the class StringManager method getManager.

/**
     * Retrieve the StringManager for a list of Locales. The first StringManager
     * found will be returned.
     *
     * @param packageName The package for which the StringManager is required
     * @param requestedLocales the list of Locales
     *
     * @return the found StringManager or the default StringManager
     */
public static StringManager getManager(String packageName, Enumeration<Locale> requestedLocales) {
    while (requestedLocales.hasMoreElements()) {
        Locale locale = requestedLocales.nextElement();
        StringManager result = getManager(packageName, locale);
        if (result.getLocale().equals(locale)) {
            return result;
        }
    }
    // Return the default
    return getManager(packageName);
}
Also used : Locale(java.util.Locale)

Example 13 with Locale

use of java.util.Locale in project cas by apereo.

the class MessageBundleAwareResourceResolver method resolveMessagesFromBundleOrDefault.

private String[] resolveMessagesFromBundleOrDefault(final String[] resolved, final Exception e) {
    final Locale locale = LocaleContextHolder.getLocale();
    final String defaultKey = Stream.of(StringUtils.splitByCharacterTypeCamelCase(e.getClass().getSimpleName())).collect(Collectors.joining("_")).toUpperCase();
    return Stream.of(resolved).map(key -> this.context.getMessage(key, null, defaultKey, locale)).toArray(String[]::new);
}
Also used : Locale(java.util.Locale) LocaleContextHolder(org.springframework.context.i18n.LocaleContextHolder) ReturnValueAsStringResourceResolver(org.apereo.inspektr.audit.spi.support.ReturnValueAsStringResourceResolver) Stream(java.util.stream.Stream) Locale(java.util.Locale) Autowired(org.springframework.beans.factory.annotation.Autowired) JoinPoint(org.aspectj.lang.JoinPoint) StringUtils(org.apache.commons.lang3.StringUtils) ApplicationContext(org.springframework.context.ApplicationContext) Collectors(java.util.stream.Collectors)

Example 14 with Locale

use of java.util.Locale in project android-betterpickers by code-troopers.

the class TimeZoneData method loadTzs.

void loadTzs(Context context) {
    mTimeZones = new ArrayList<TimeZoneInfo>();
    HashSet<String> processedTimeZones = loadTzsInZoneTab(context);
    String[] tzIds = TimeZone.getAvailableIDs();
    if (DEBUG) {
        Log.e(TAG, "Available time zones: " + tzIds.length);
    }
    for (String tzId : tzIds) {
        if (processedTimeZones.contains(tzId)) {
            continue;
        }
        /*
             * Dropping non-GMT tzs without a country code. They are not really
             * needed and they are dups but missing proper country codes. e.g.
             * WET CET MST7MDT PST8PDT Asia/Khandyga Asia/Ust-Nera EST
             */
        if (!tzId.startsWith("Etc/GMT")) {
            continue;
        }
        final TimeZone tz = TimeZone.getTimeZone(tzId);
        if (tz == null) {
            Log.e(TAG, "Timezone not found: " + tzId);
            continue;
        }
        TimeZoneInfo tzInfo = new TimeZoneInfo(tz, null);
        if (getIdenticalTimeZoneInTheCountry(tzInfo) == -1) {
            if (DEBUG) {
                Log.e(TAG, "# Adding time zone from getAvailId: " + tzInfo.toString());
            }
            mTimeZones.add(tzInfo);
        } else {
            if (DEBUG) {
                Log.e(TAG, "# Dropping identical time zone from getAvailId: " + tzInfo.toString());
            }
            continue;
        }
    //
    // TODO check for dups
    // checkForNameDups(tz, tzInfo.mCountry, false /* dls */,
    // TimeZone.SHORT, groupIdx, !found);
    // checkForNameDups(tz, tzInfo.mCountry, false /* dls */,
    // TimeZone.LONG, groupIdx, !found);
    // if (tz.useDaylightTime()) {
    // checkForNameDups(tz, tzInfo.mCountry, true /* dls */,
    // TimeZone.SHORT, groupIdx,
    // !found);
    // checkForNameDups(tz, tzInfo.mCountry, true /* dls */,
    // TimeZone.LONG, groupIdx,
    // !found);
    // }
    }
    // Don't change the order of mTimeZones after this sort
    Collections.sort(mTimeZones);
    mTimeZonesByCountry = new LinkedHashMap<String, ArrayList<Integer>>();
    mTimeZonesByOffsets = new SparseArray<ArrayList<Integer>>(mHasTimeZonesInHrOffset.length);
    mTimeZonesById = new HashMap<String, TimeZoneInfo>(mTimeZones.size());
    for (TimeZoneInfo tz : mTimeZones) {
        // /////////////////////
        // Lookup map for id -> tz
        mTimeZonesById.put(tz.mTzId, tz);
    }
    populateDisplayNameOverrides(mContext.getResources());
    Date date = new Date(mTimeMillis);
    Locale defaultLocal = Locale.getDefault();
    int idx = 0;
    for (TimeZoneInfo tz : mTimeZones) {
        // Populate display name
        if (tz.mDisplayName == null) {
            tz.mDisplayName = tz.mTz.getDisplayName(tz.mTz.inDaylightTime(date), TimeZone.LONG, defaultLocal);
        }
        // /////////////////////
        // Grouping tz's by country for search by country
        ArrayList<Integer> group = mTimeZonesByCountry.get(tz.mCountry);
        if (group == null) {
            group = new ArrayList<Integer>();
            mTimeZonesByCountry.put(tz.mCountry, group);
        }
        group.add(idx);
        // /////////////////////
        // Grouping tz's by GMT offsets
        indexByOffsets(idx, tz);
        // Skip all the GMT+xx:xx style display names from search
        if (!tz.mDisplayName.endsWith(":00")) {
            mTimeZoneNames.add(tz.mDisplayName);
        } else if (DEBUG) {
            Log.e(TAG, "# Hiding from pretty name search: " + tz.mDisplayName);
        }
        idx++;
    }
// printTimeZones();
}
Also used : Locale(java.util.Locale) ArrayList(java.util.ArrayList) Date(java.util.Date) TimeZone(java.util.TimeZone)

Example 15 with Locale

use of java.util.Locale in project android-betterpickers by code-troopers.

the class TimeZonePickerUtils method getGmtDisplayName.

/**
     * Given a timezone id (e.g. America/Los_Angeles), returns the corresponding timezone display name (e.g. Pacific
     * Time GMT-7).
     *
     * @param context Context in case the override labels need to be re-cached.
     * @param id The timezone id
     * @param millis The time (daylight savings or not)
     * @param grayGmt Whether the "GMT+x" part of the returned string should be colored gray.
     * @return The display name of the timezone.
     */
public CharSequence getGmtDisplayName(Context context, String id, long millis, boolean grayGmt) {
    TimeZone timezone = TimeZone.getTimeZone(id);
    if (timezone == null) {
        return null;
    }
    final Locale defaultLocale = Locale.getDefault();
    if (!defaultLocale.equals(mDefaultLocale)) {
        // If the IDs and labels haven't been set yet, or if the locale has been changed
        // recently, we'll need to re-cache them.
        mDefaultLocale = defaultLocale;
        cacheOverrides(context);
    }
    return buildGmtDisplayName(timezone, millis, grayGmt);
}
Also used : Locale(java.util.Locale) TimeZone(java.util.TimeZone)

Aggregations

Locale (java.util.Locale)2214 Test (org.junit.Test)262 ArrayList (java.util.ArrayList)179 HashMap (java.util.HashMap)108 ResourceBundle (java.util.ResourceBundle)83 SimpleDateFormat (java.text.SimpleDateFormat)78 Date (java.util.Date)77 MissingResourceException (java.util.MissingResourceException)68 IOException (java.io.IOException)67 TimeZone (java.util.TimeZone)63 File (java.io.File)50 Map (java.util.Map)49 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)49 Calendar (java.util.Calendar)46 Configuration (android.content.res.Configuration)41 LocaleList (android.os.LocaleList)39 HashSet (java.util.HashSet)39 StudyBean (org.akaza.openclinica.bean.managestudy.StudyBean)39 HttpSession (javax.servlet.http.HttpSession)38 Resources (android.content.res.Resources)37